Set Volume Variable During Task Then Carry Forward To Future Trials

OS (Windows 10):
PsychoPy version (2022.2.4):

What are you trying to achieve?:
I’m trying to create an initial segment for an experiment where participants adjust the volume of the sound files, then their adjusted values are carried forwards for future trials where these sound files are used. I can get the adjustments to work, but I’m struggling to get the volume level to carry forward.

What did you try to make it work?:
I have a routine where a sound file plays, and the volume can be adjusted up and down with buttons. I have a variable ‘fivehundredVol’ initially set (Before Experiment) to 0.6. The buttons end the routine and adjust the value of fivehundredVol up or down by +/- 0.01 with each routine. I use the following code component to do this:

maxVol = 1
minVol = 0
stepSize = 0.01
if mouse.clicked_name[0]=='VolUp1':
    if fivehundredVol + stepSize >= maxVol:
        fivehundredVol = maxVol
    else:
        fivehundredVol += stepSize
elif mouse.clicked_name[0]=='VolDown1':
    if fivehundredVol - stepSize <= minVol:
        fivehundredVol = minVol
    else:
        fivehundredVol -= stepSize
elif mouse.clicked_name[0]=='Back1':
    VolRepeats.finished = True # end this loop
elif mouse.clicked_name[0]=='Next2':
    FiveHundredAdjust.finished = True # end this loop
    VolRepeats.finished = True

This code is in the ‘Begin Routine’ window. ‘VolUp1’ and ‘VolDown1’ refer to the button components, and VolRepeats is the name of the loop (set to N=9999 to allow many adjustments). Participants then click ‘Next2’ to end the loop.

What I then want to do is carry forward the value that participants set ‘fivehundredVol’ to. So next I have a routine where a sound file plays, and I have the volume of my sound component as a variable called ‘CustomVol’. ‘CustomVol’ is initially set (Before Experiment) to an arbitrary value of 0.2, and I then try to set the value of ‘CustomVol’ as equivalent to fivehundredVol with a code component:

if SoundFilePoint == 'FiveHundred'
    CustomVol = fivehundredVol

This code is in the ‘Begin Routine’ window. Here ‘SoundFilePoint’ is a column within my conditions file, and the first row contains the text ‘FiveHundred’.

What specifically went wrong when you tried that?:
The variable ‘CustomVol’ is not being set as equivalent to ‘fivehundredVol’. I have a text component on the second routine with ‘$CustomVol’ on screen, and the value it shows is 0.2 which suggests the second code component I’ve mentioned does not work. Any help with a better way to do this would be much appreciated!

PerceptualMatching.psyexp (40.3 KB)
ProcedureCalib.xlsx (8.3 KB)

Hello and welcome to the forum. Here are some tips from me (and the probable error).

  1. Most of your code in Before Experiment would be better in Begin Experiment
  2. It’s generally good practice to put code components at the top of the routine, so that anything set “Each Repeat” will be applied after the code.
  3. I would recommend setting the volume Each Repeat instead of Each Frame unless you are changing the volume using Each Frame code.
  4. Even if you aren’t running your experiment online I would recommend using Auto → JS for your code components whenever possible. Had you done this, you would have discovered a syntax error in code_3
if SoundFilePoint == 'FourThousand'
    CustomVol = fivehundredVol

You are missing the colon

if SoundFilePoint == 'FourThousand':
    CustomVol = fivehundredVol

I haven’t checked whether this correctly identifies the row in your spreadsheet.

Thank you for your general advice! I did not know that point about moving code components to the top but that makes a lot of sense.

Adding in the colon seems to have fixed the issue, I can’t believe I didn’t see that :rofl: thank you!

1 Like