set sound file duration on every repeat

357 views
Skip to first unread message

Maddie

unread,
Apr 15, 2014, 7:45:52 AM4/15/14
to psychop...@googlegroups.com
Hi,
 
I am currently using the builder in StandalonePsychoPy-1.80.01-win32.exe (159.2 MB).
 
I am trying to set up an experiment where there is a standard tone (100ms) and variable isi (50, 250, 500, 750, 1000 ms) and a comparison tone (100ms +/- X according to an adaptive staircase). The standard and comparison tone are then judged on which is longest.
 
My initial issue has been setting the duration of my sound file on every repeat (even in a simple random loop scenario).
 
For simplicity sake, I set up a basic experiment to test this issue. The routine includes a sound file "TestTone.wav" with duration(s) set to read from an xlsx file "$sDur". One column in the xlsx file reads: sDur  0.25   0.5   0.75   1   1.25.
 
From reading past posts (albeit related to previous versions of psychopy) I understand that the builder doesn't necessarily handle setting the duration of a stimulus to every repeat but that this could be overcome with the following code...which I have implemented:
 
sound1 = sound.Sound('float(TestTone.wav)', secs=float(sDur))
mySound.play()
 
When I run my experiment the following error appears:
 
sound1 = sound.sound(u'TestTone.wav', secs=sDur)
NameError: name 'sDur' is not defined
 
I do not understand this error as I have defined sDur in the duration tab of my sound file as $sDur and within the first column of my xlsx file.
 
I guess I therefore have 2 questions:
 
1. Is it possible to set the duration of a sound file on every repeat
2. In addition is this possible in an interleaved staircase scenario, where the duration of the sound file changes according to the adaptive staircase method.
 
I have attached copies of the experiment and xlsx file for further clarification.
 
Any comments at all, however simple, would be greatly appreciated as I am fairly new to psychopy and am keen to gain more knowledge.
 
Many thanks!
 
 
 
 
 
 
soundtest.psyexp
testlist.xlsx

Samarth Varma

unread,
Apr 15, 2014, 10:05:43 AM4/15/14
to psychop...@googlegroups.com
Hi Maddie,

I've read somewhere that this used to be an error with PsychoPy but may be it has been fixed now. But I'm not aware of it as I'm quite new here. My Python 1.79 gives the same issue as you encountered.

The reason is  - the sound event attempts to initialize the sound with an sDur seconds duration BEFORE the excel sheet is read. At that stage, sDur doesn't exist, right? That's why you get that error.

An inelegant but robust solution is to create a routine before the sound trial (say, 'setup' routine) and initialize sDur=1.0 in the "Begin Experiment" section. This will take care of the error. 
But still, the sound would just play for 1 second in this case and will remain silent for the rest of the time the sound event is ON. Basically the "sound event" duration is separate from the duration of the sound itself. So since we set the sound duration to 1sec in the beginning, it always plays for 1sec despite however long the ''sound event'' may be. But there is a solution to that .. 

So, the next thing to do would be to reinitialize the sound duration with each routine using "sound_1 = sound.Sound(u'A', secs=sDur)" in the "Begin Routine" section in your sound riall (not 'setup' routine).

That way, the sound gets initialized to a non zero duration at the beginning and before the excel is read, and it gets reinitialized to new durations after excel is read.

I'm sure someone else will have a better solution than this, or may be its already incorporated somewhere I don't know. But I hope this works. You can do the interleaved staircase thing from here on I guess.

See the attachment for exact details.

Samarth
soundtest.psyexp

Maddie

unread,
Apr 15, 2014, 12:25:54 PM4/15/14
to psychop...@googlegroups.com


Hi Samarth,

Thank you very much for your help and for your example attachment! This certainly works for sounds generated by psychopy so it's good to know that there is a way around the issue, thank you very much!
 
Unfortunately, I think I was being optimistic in thinking that I could manipulate the duration of an imported sound file (created in a programme called audacity) using the same method. I was hoping that psychopy would compress/expand the sound file to a list of durations given in psychopy. Trying this out on your example I have realised that this is probably a very inaccurate way to go about things as the file becomes quite distorted. I probably need to draw from a list of individually created sound files (each being one of the different durations I require).
 
However, this seems to create a separate issue for me when it comes to implementing the more complicated interleaved staircase design I had in mind...
 
I want the comparison tone (100ms +/- X) to increase/decrease in steps [0.1,0.1,0.01] via a 3-down, 1-up staircase based on whether it was judged as longer or shorter than a standard tone (100ms). There will be 5 interleaved staircases, each implementing one of the following ISI's (50ms, 250ms, 500ms, 750ms, 1000ms). 
 
Since (as described above) I don't think it is very accurate to manipulate the duration of a single sound file (in this case the tone is comprised of: 15ms 1khz tone + 85ms silence + 15ms 1khz tone) the only other option I can see is to create all of the potential sounds that could be generated by the staircase (ie: a 100ms tone, 110ms tone, 120ms tone etc...) for the staircase to draw upon. However, from my experience so far, I'm not sure whether psychopy has the capacity to implement a staircase in this way, calling upon different sound files dependent upon where it is in the staircase at any given time.
 
Does anybody know if this is remotely possible?
Also, there is a great chance that I have missed a far simpler alternative... if so, then I am very open to any other suggestions. 
 
Many thanks again for your help so far!
I hope that my comments above make sense , if not then please do let me know so that I can clarify them.
 
Many thanks,
Maddie
 
 
 
 
 
 
 
 

ShoinExp

unread,
Apr 15, 2014, 11:25:43 PM4/15/14
to psychop...@googlegroups.com
Hi,
    One solution that you might consider, though a little tricky, is to modify the sound file using a code component that calls a Praat script.  In case you're not familiar with it, Praat is a free software used by linguists to analyze and modify sounds.  It has a scripting feature, so you can write a script that will modify durations (extremely accurately).  Within PsychoPy you could have a code component that calls the Praat script on each step of the staircase and so modify the recorded sound as needed.
    I have already done this for one of my experiments and it works perfectly, though it does require the ability to write a Praat script and there are some "gotchas" that you have to consider.  If you want to consider going this route I can send you a small working example of calling a Praat script from PsychoPy.
     Mark

ShoinExp

unread,
Apr 15, 2014, 11:41:39 PM4/15/14
to psychop...@googlegroups.com
Hi again,
    Sorry I should have mentioned this alternative in my last post.  Yes, the staircase can call on different sound files depending on where it is.  That's quite easy.  You just need to have a code component that takes the output of the staircase (the variable "level") and uses it to specify the name of a sound file.
    Here's a simple example of that sort of code:

sound_value = level
target = ("SoundFileFolder/"+ str(sound_value)+".wav")

    Then just specify the variable "target" as the name of your sound file in your sound event and this will play the sound appropriate to that step on the staircase.  I find that you need to put this code call in a routine BEFORE the routine that plays the sound (otherwise errors result).  So you might want to add a routine (within the staircase loop, but before the routine that presents your stimulus) that only has a code component.
    Good luck,
    Mark

Maddie

unread,
Apr 16, 2014, 7:02:06 AM4/16/14
to psychop...@googlegroups.com
Hi Mark,
 
Thank you very much for your suggestion, it is very helpful! I have tried to implement your second solution above but the experiment seems to pause just before the first comparison tone is played (there is no error message). No doubt I have done something silly but I can't seem to figure out what...
 
As suggested I have put the code component
 
sound_value = level
target = ("SoundFileFolder/"+ str(sound_value)+".wav")
in a routine prior to the routine where the comparison sound is played and have specified the name of the sound file as $target. My sound files are labelled "1.wav" "2.wav" "3.wav" "4.wav" "5.wav" in a folder called "SoundFileFolder".
 
I have attached the experiment and the conditions file. If you can spot any mistakes or have any ideas as to why the experiment pauses that would great.
 
Many thanks again for all your help!  
stair_practice.psyexp
stairconditions.xlsx

ShoinExp

unread,
Apr 16, 2014, 9:50:22 AM4/16/14
to psychop...@googlegroups.com
Hi,
    I downloaded your script and it works fine when I try it.  I think I may know what happened:  your first sound is specified just as a sound file "3.wav" with no folder specified.  When no folder is mentioned PsychoPy looks in the same folder as the script and if it can't find it, the experiment will crash.  So, I suspect you just forgot to put the sound files in the same folder as your script.  To fix this you could either copy "3.wav" from your SoundFileFolder to the outer folder would work, or you could specify SoundFileFolder in sound 1 (that way you only need 1 set of sounds).
     Good luck,
     Mark

Maddie

unread,
Apr 16, 2014, 10:16:39 AM4/16/14
to psychop...@googlegroups.com

Hi Mark,
 
Many thanks for your reply. Unfortunately, I don't think that this is the issue as the experiment crashes after the first sound ("3.wav"), not before. I have tried both of your suggestions above just to double check and unfortunately the experiment still crashes. I wonder if the problem lies with my stairconditions list? I have set the startVal to 3 in hopes of the first sound file being played in the "comparison" routine being "3.wav". I assumed that because level = sound_value   and sound_value = the file name (ie: 3 in 3.wav) then this would define the first value of the staircase?
 
Is it therefore possible that the experiment is crashing because the start value of the staircase is not defined correctly? If so, then I'm not sure how else I should define it and am again open to any suggestions.
 
Many thanks! 

ShoinExp

unread,
Apr 16, 2014, 10:21:22 AM4/16/14
to psychop...@googlegroups.com
Hi,
    Your script is working perfectly on my computer, so I don't think it's a coding problem.  It seems like it's a problem with your computer or your installation and there we hit the end of my knowledge.  I'm running this on a Mac 10.8.4 - don't know if that's the difference.  At least you can take comfort in the knowledge that your experiment works, just not on your computer :)
    Good luck,
    Mark

Maddie

unread,
Apr 16, 2014, 11:24:38 AM4/16/14
to psychop...@googlegroups.com

Hi Mark,
 
Many thanks for all of your help up until this point. I'll try running the experiment on another computer and on an earlier version of psychopy maybe, and hopefully this will solve the problem.
 
Many thanks,
Maddie
 
Reply all
Reply to author
Forward
0 new messages