Can't run simple sound loop, ptb backend

OS (e.g. Win10):
MacOS 13.4
PsychoPy version (e.g. 1.84.x):
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:
Testing just the sound component in a loop because it wasn’t working. I would like all the sounds to be played, as specified by the CSV, but only the first one plays.
What did you try to make it work?:
I’ve tried so many things that it’s hard to remember anything specific.
What specifically went wrong when you tried that?:
There is no error message. Exit code is 0. The second sound file has never played, but the correct sound file name will print. The length of the .wav files are all under the specified sound component duration.

code that was generated by builder:
#!/usr/bin/env python

-- coding: utf-8 --

“”"
This experiment was created using PsychoPy3 Experiment Builder (v2023.1.2),
on Sun Nov 12 15:01:14 2023
If you publish work using this script the most relevant publication is:

Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019) 
    PsychoPy2: Experiments in behavior made easy Behav Res 51: 195. 
    https://doi.org/10.3758/s13428-018-01193-y

“”"

— Import packages —

from psychopy import locale_setup
from psychopy import prefs
from psychopy import plugins
plugins.activatePlugins()
prefs.hardware[‘audioLib’] = ‘ptb’
prefs.hardware[‘audioLatencyMode’] = ‘3’
from psychopy import sound, gui, visual, core, data, event, logging, clock, colors, layout
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)

import numpy as np # whole numpy lib is available, prepend ‘np.’
from numpy import (sin, cos, tan, log, log10, pi, average,
sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle, choice as randchoice
import os # handy system and path functions
import sys # to get file system encoding

from psychopy.hardware import keyboard

Ensure that relative paths start from the same directory as this script

_thisDir = os.path.dirname(os.path.abspath(file))
os.chdir(_thisDir)

Store info about the experiment session

psychopyVersion = ‘2023.1.2’
expName = ‘sound_loop_test’ # from the Builder filename that created this script
expInfo = {
‘participant’: f"{randint(0, 999999):06.0f}",
‘run’: ‘1’,
}

— Show participant info dialog –

dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
if dlg.OK == False:
core.quit() # user pressed cancel
expInfo[‘date’] = data.getDateStr() # add a simple timestamp
expInfo[‘expName’] = expName
expInfo[‘psychopyVersion’] = psychopyVersion

Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc

filename = thisDir + os.sep + u’data/%s%s_%s’ % (expInfo[‘participant’], expName, expInfo[‘date’])

An ExperimentHandler isn’t essential but helps with data saving

thisExp = data.ExperimentHandler(name=expName, version=‘’,
extraInfo=expInfo, runtimeInfo=None,
originPath=‘/Users/cnrlab/Desktop/sound_loop_test_lastrun.py’,
savePickle=True, saveWideText=True,
dataFileName=filename)

save a log file for detail verbose info

logFile = logging.LogFile(filename+‘.log’, level=logging.EXP)
logging.console.setLevel(logging.WARNING) # this outputs to the screen, not a file

endExpNow = False # flag for ‘escape’ or other condition => quit the exp
frameTolerance = 0.001 # how close to onset before ‘same’ frame

Start Code - component code to be run after the window creation

— Setup the Window —

win = visual.Window(
size=[1512, 982], fullscr=True, screen=0,
winType=‘pyglet’, allowStencil=False,
monitor=‘testMonitor’, color=[0,0,0], colorSpace=‘rgb’,
backgroundImage=‘’, backgroundFit=‘none’,
blendMode=‘avg’, useFBO=True,
units=‘height’)
win.mouseVisible = False

store frame rate of monitor if we can measure it

expInfo[‘frameRate’] = win.getActualFrameRate()
if expInfo[‘frameRate’] != None:
frameDur = 1.0 / round(expInfo[‘frameRate’])
else:
frameDur = 1.0 / 60.0 # could not measure, so guess

— Setup input devices —

ioConfig = {}
ioSession = ioServer = eyetracker = None

create a default keyboard (e.g. to check for escape)

defaultKeyboard = keyboard.Keyboard(backend=‘ptb’)

— Initialize components for Routine “trial” —

just_sound = sound.Sound(‘A’, secs=1.9, stereo=False, hamming=True,
name=‘just_sound’)
just_sound.setVolume(1.0)

Create some handy timers

globalClock = core.Clock() # to track the time since experiment started
routineTimer = core.Clock() # to track time remaining of each (possibly non-slip) routine

set up handler to look after randomisation of conditions etc

trials = data.TrialHandler(nReps=1.0, method=‘sequential’,
extraInfo=expInfo, originPath=-1,
trialList=data.importConditions(“./2run/NWRep_run” + expInfo[‘run’] + “.csv”),
seed=None, name=‘trials’)
thisExp.addLoop(trials) # add the loop to the experiment
thisTrial = trials.trialList[0] # so we can initialise stimuli with some values

abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)

if thisTrial != None:
for paramName in thisTrial:
exec(‘{} = thisTrial[paramName]’.format(paramName))

for thisTrial in trials:
currentLoop = trials
# abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
if thisTrial != None:
for paramName in thisTrial:
exec(‘{} = thisTrial[paramName]’.format(paramName))

# --- Prepare to start Routine "trial" ---
continueRoutine = True
# update component parameters for each repeat
just_sound.setSound('./Scott-2019-Nonwords/' + Condition + '_' + Stimulus_File + '.wav', secs=1.9, hamming=True)
just_sound.setVolume(1.0, log=False)
# Run 'Begin Routine' code from code
# In the Begin Routine tab of a new Code Component within the routine
routine_timer = core.Clock()

# In the Each Frame tab of the same Code Component within the routine
if just_sound.status == NOT_STARTED:
    current_condition = trials.trialList[trials.thisN]
    sound_file = './Scott-2019-Nonwords/' + current_condition['Condition'] + '_' + current_condition['Stimulus_File'] + '.wav'
    just_sound.setSound(sound_file)
    just_sound.play()
    routine_timer.reset()  # Reset the timer when sound starts

elif just_sound.status == STARTED and not just_sound.isPlaying:
    continueRoutine = False  # or add a break statement

# In the End Routine tab of the same Code Component within the routine
just_sound.stop()  
# keep track of which components have finished
trialComponents = [just_sound]
for thisComponent in trialComponents:
    thisComponent.tStart = None
    thisComponent.tStop = None
    thisComponent.tStartRefresh = None
    thisComponent.tStopRefresh = None
    if hasattr(thisComponent, 'status'):
        thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
frameN = -1

# --- Run Routine "trial" ---
routineForceEnded = not continueRoutine
while continueRoutine and routineTimer.getTime() < 2.05:
    # get current time
    t = routineTimer.getTime()
    tThisFlip = win.getFutureFlipTime(clock=routineTimer)
    tThisFlipGlobal = win.getFutureFlipTime(clock=None)
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    
    # if just_sound is starting this frame...
    if just_sound.status == NOT_STARTED and tThisFlip >= 0.150-frameTolerance:
        # keep track of start time/frame for later
        just_sound.frameNStart = frameN  # exact frame index
        just_sound.tStart = t  # local t and not account for scr refresh
        just_sound.tStartRefresh = tThisFlipGlobal  # on global time
        # add timestamp to datafile
        thisExp.addData('just_sound.started', tThisFlipGlobal)
        # update status
        just_sound.status = STARTED
        just_sound.play(when=win)  # sync with win flip
    
    # if just_sound is stopping this frame...
    if just_sound.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > just_sound.tStartRefresh + 1.9-frameTolerance:
            # keep track of stop time/frame for later
            just_sound.tStop = t  # not accounting for scr refresh
            just_sound.frameNStop = frameN  # exact frame index
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'just_sound.stopped')
            # update status
            just_sound.status = FINISHED
            just_sound.stop()
    # update just_sound status according to whether it's playing
    if just_sound.isPlaying:
        just_sound.status = STARTED
    elif just_sound.isFinished:
        just_sound.status = FINISHED
    
    # check for quit (typically the Esc key)
    if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
        core.quit()
        if eyetracker:
            eyetracker.setConnectionState(False)
    
    # check if all components have finished
    if not continueRoutine:  # a component has requested a forced-end of Routine
        routineForceEnded = True
        break
    continueRoutine = False  # will revert to True if at least one component still running
    for thisComponent in trialComponents:
        if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
            continueRoutine = True
            break  # at least one component has not yet finished
    
    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

# --- Ending Routine "trial" ---
for thisComponent in trialComponents:
    if hasattr(thisComponent, "setAutoDraw"):
        thisComponent.setAutoDraw(False)
just_sound.stop()  # ensure sound has stopped at end of routine
# using non-slip timing so subtract the expected duration of this Routine (unless ended on request)
if routineForceEnded:
    routineTimer.reset()
else:
    routineTimer.addTime(-2.050000)
thisExp.nextEntry()

completed 1.0 repeats of ‘trials’

— End experiment —

Flip one final time so any remaining win.callOnFlip()

and win.timeOnFlip() tasks get executed before quitting

win.flip()

these shouldn’t be strictly necessary (should auto-save)

thisExp.saveAsWideText(filename+‘.csv’, delim=‘auto’)
thisExp.saveAsPickle(filename)
logging.flush()

make sure everything is closed down

if eyetracker:
eyetracker.setConnectionState(False)
thisExp.abort() # or data files will save again on exit
win.close()
core.quit()

Try starting the sound at 0.5 seconds

I got it to work by setting _isFinished to false in a end of routine code component!

1 Like

I’m having an audio issue in one of my experiments which wasn’t solved by changing the start time. Please could you clarify how you set _isFinished to false? Was it sound_1._isFinished = False ? (for a sound component called soubd_1)

I copied the endExpNow = False and sound._isFinished = False from this Sound component with empty duration won't play after first iteration of loop—changed behavior in 2023.2.0. The _isFinished code was added as a code component set to end of routine for the routine containing the sound component. And the endExpNow part was in the same code component but in the beginning of routine part.