Just another "ReferenceError: Can't find variable: os"

URL of experiment: https://run.pavlovia.org/roberto.petrosino/mh_capstone2022/?__pilotToken=c51ce410c124a10e0db5e4b97fc2af39&__oauthToken=aaca384de17143ecc90f7c7932851480360e477fffe77e815f2e11e9a6f4ccd8

Description of the problem:

I create a simple auditory priming experiment on the Builder and upload it to pavlovia according the guideline provided in this forum regarding the experiment settings (blank output path, export HMTL set on Sync, uploaded additional resources). However, when turned on piloting stage, the following error pops up:

ReferenceError: Canā€™t find variable: os

which seems to refer to this line of the js code:

psychoJS.experiment.dataFileName = (("." + "/") + (("data" + os.path.sep) + `${expInfo["participant"]}_${expInfo["date"]}`));

I figured that os is not a js module, so I just substitute os.path.sep with a simple "/", and that issue is resolved. However, if I test the script again, a similar error pops up:

ReferenceError: Canā€™t find variable: secs

which refers to this line of the code:

playSound.setSound(audio, secs=-1)

In the API reference of PsychoJS setSound() does not have a secs argument, so I just removed it. I re-tested the experiment, and finally this new error popped up:

Unfortunately we encountered the following error:

  • when replacing the current sound
  • invalid input, need an instance of the Sound class.
    Try to run the experiment again. If the error persists, contact the experiment designer.

And here I got stuck. Any solutions? Thanks!

I find itā€™s easiest to create sounds using Builder components with 0 volume (using a sound file not a frequency or note) in an earlier routine.

Have a look at my crib sheet for more details

Hi @wakecarter, thank you for your suggestion! I have checked your crib sheet and tried to implement what you are saying here, but when I try to run the code locally this error pops up:

NameError: name ā€˜thisSoundConditionā€™ is not defined

I am attaching the .psyexp file below, in case it helps. (In the settings of the sound component I replaced $thisSound with $audio, as it is the name of the condition in the conditions files.

Thanks!
aldt_pilot.psyexp (37.4 KB)

I canā€™t see any reference to thisSoundCondition in your psyexp file.

However, in code_SoundMute you have

if t > 1:
    continueRountine = False

in Begin Routine when you should have

if t > 1:
    continueRoutine = False

in Each Frame

and (if I understand your experiment correctly) in the playVoice routine you should remove playSound.

Your code from EndRoutine in soundMute should be in Begin Routine of playVoice

if audioCondition != "Silence":
    sound_1.setVolume(1)
    sound_1.play()

What I meant is. In your example, thisSoundCondition was named liked that because you added the variable $thisSound in the settings of the sound component. I had set $audio because thatā€™s what is called the column in my condition files, hence I assumed that the variable audioCondition was automatically created by PsychoPy.

However, in code_SoundMute you have

if t > 1:
    continueRountine = False

in Begin Routine when you should have

if t > 1:
    continueRoutine = False

in Each Frame

and (if I understand your experiment correctly) in the playVoice routine you should remove playSound.

Your code from EndRoutine in soundMute should be in Begin Routine of playVoice

if audioCondition != "Silence":
    sound_1.setVolume(1)
    sound_1.play()

I did what you said here, but again the variable audioCondition is not defined. Am I missing/misreading something in your working example?

audioCondition is not created automatically. If the word Silence is in your audio column then the could would be if audio != ā€œSilenceā€:

Oook, I get what you mean now. And everything seems to work now, thank you! :slight_smile:

Now, the issue is ā€“ I need the sound to be play after a 1s fixation cross. The fixation cross is within the playVoice routine. How do I do that if the sound is preloaded in a routine preceding it?

The easiest option is to put the fixation cross in the routine with the silent sound.

The alternative is to play the sound using an Each Frame code component set to trigger at 1 second but using a flag variable to stop it triggering multiple times.

Hey @wakecarter,

I am trying to implement this suggestion of yours in my code, but I am facing some issues. In my soundMute routine, I now have the fixation cross, the sound and the code component. However, if I use the code component as before in the Each Frame tab of the soundMute routine:

if t > 1:
    continueRoutine = False

the fixation cross will not be presented at all. So I tried to replace the code above with this instead:

if t > 1:
   sound_1.setVolume(0) #mute the sound

Eventually the volume of sound_1 is reset to 1 and played in the following routine as shown above.

Locally, this works as intended. However, Iā€™m back to square 1 when the experiment is run online, as a number of errors pop up (ie., regarding the secs argument of the function setSound).

Do you have any suggestion on how to solve this? I tried to use pause() and stop() but it did not do the trick.

Thanks!

Why do you need to set the volume to 0 after 1 second of a routine with a sound component with zero volume?

I normally leave the duration of sound components blank and have the sound file of the desired length.

What code do you have related to your error?

I need to set the volume to 0 in the soundMute routine because otherwise the sound will automatically play in the routine. In the soundMute routine, I want (a) the fixation cross to be printed on the screen, but (b) the sound not to be played. In your demo, your soundMute routine had just the sound component in it, so you just did:

if t>1:
    continueRoutine=False

In my case, the code above will prevent both the fixation cross and the sound from being presented. So, I thought to adapt the code so that it selectively does not play the sound but prints the fixation cross:

if t>1:
    playSound.setVolume(0) # or playSound.stop() or playSound.pause()

This solution works on locally, but it does not work online. When I test it, it gives me the usual error as above:

ReferenceError: Canā€™t find variable: secs

Regarding this:

I normally leave the duration of sound components blank and have the sound file of the desired length.

I donā€™t understand your point here. I do leave the duration of sound components blank, but still the error above pops up (online only, though). Your solution did solve the error, I just canā€™t seem to find a solution when implementing a pre-stimulus fixation cross.

If your sound starts with 0 volume then you donā€™t need to set it to 0 volume after 1 second.

If you start a fixation cross at 0 seconds and want it to last for one second then one option is to end the routine in code when t > 1

1 Like

I am still having trouble getting the experiment to work online. The experiment works now as intended locally (thanks for the last tip, @wakecarter!), but it gets a number of errors online. First, the usual error pops up:

ReferenceError: Canā€™t find variable: secs

The JavaScript console suggests that the error above refers to the argument secs of the setSound() function. If I try to delete that argument across all setSound() instances, the following error pops up:

Unfortunately, we encountered the following error:

  • when replacing the current sound
  • invalid input, need and instance of the Sound class

So, basically I am back to my first message of this post. I canā€™t seem to understand why this is happening. Is this a known issue?

Are you trying to set the contents of a sound component in code?

My crib sheet recommends using a sound component.

No, I created a workflow on PsychoPy with a few code components as you recommended in your crib sheet, and then push it to pavlovia. I am attaching the psyexp file.

auditory_longDistancePriming.psyexp (84.8 KB)

Hey there, Iā€™m trying everything, but the experiment still gives me the usual error:

ReferenceError: Canā€™t find variable: secs

and I donā€™t really know what else to do. I tried to delete the previous project on pavlovia and redo the whole experiment on the Builder from scratch (according to @wakecarterā€™s crib sheet). But that did not make much of a difference.

Could anyone advise on what to do? Hereā€™s the URL of experiment: auditory_longDistancePriming [PsychoPy]

Thanks!