psychopy.org | Reference | Downloads | Github

Generating Sound

sound

#1

Hello!

I have some questions regarding generating sound using Psychopy Standalone. I’ve got a fair amount of experience generating visual stimuli, and using some very basic code in code components, but I have zero experience working with sound, so I have a couple questions!

I’m working on developing an acoustic startle fear learning task, using some basic visual stimuli. This is all set up in Psychopy, aside from the important part: sound. I’m trying to have a white noise ongoing throughout the task, and when a certain visual stimulus is presented, the white noise should simultaneously increase in volume.

My understanding is that Psychopy can generate white noise, though I have not had luck in finding out how. Does anyone know how to do this?

My follow-up question is would it be best to generate this in a Code Component in order to play it throughout the task, or would it be better to use a Sound Component?

Finally, how do I adjust the sound?

Apologies, for not having more substance about what I have tried, but I am very unsure of how to proceed. Any assistance would be much appreciated!

Thank you!
Danielle


#2

Hi There!

I use Brown noise throughout my task so I have a few pointers that might help.

  1. Generating the sound/noise. I found it easiest to generate noise outside of psychopy using Audacity (free software available - http://www.audacityteam.org/). Here you can generate most kinds of noise (pink/white/brown) . I then use psychopy to play/stop/set the intensity of this.

  2. I found it better/easier to control the presentation of noise using a code component.

  3. You can adjust the sound following a response using something like the below.

BEFORE LOOKING AT THE CODE NOTE:
you will see that I set my sound using the equation
Vol=((0.34/0.1)/(10**(111.8/20)))*(10**(thisVol/20))

this is because I callibrated my headphones to obtain the following values

Vol=((a/0.1)/(10**(b/20)))*(10**(thisVol/20))

where a is the Psychopy unit level required to output 0.1vRMS (measured with voltmeter), b is the dBSPL output from 0.1vRMS and thisVol is the output required in dBSPL. If you do not have a voltmeter and SPL meter to hand usually this info can be obtained for your speakers/heaphones in the manual. (For your initial set up the code will still work with my values - but its definitely worth calibrating your set up to obtain these values before you begin your experiment)

from __future__ import division
from psychopy import sound, event, visual, core

#set the sound
Noise=sound.SoundPyo('BrownNoiseT.wav')#change this path to your sound/wav file

#What volume do you want to start at in dB.
thisVol =  50
#how much do you want the volume to increase by following a response?
increase=1
#how many trials you want 
trials=10

for i in range(trials):
    Vol=((0.34/0.1)/(10**(111.8/20)))*(10**(thisVol/20))#Changes the volume of the sound in line with current intensity
    Noise.setVolume(Vol)
    print 'sound playing at',thisVol, 'db' 
    Noise.play()
    event.waitKeys()
    Noise.stop()
    thisVol=thisVol+increase

core.quit()

Hopefully this helps/works for you!! let me know if you have any further questions

Becca


#3

Sorry I also realised I didnt state how this should be implemented in builder view.

Simply add a code component in your routine.
add:

#set the sound
Noise=sound.SoundPyo('BrownNoiseT.wav')#change this path to your sound/wav file
#What volume do you want to start at in dB.
thisVol =  50
#how much do you want the volume to increase by following a response?
increase=1

to the “begin experiment” tab.
Then add:

Vol=((0.34/0.1)/(10**(111.8/20)))*(10**(thisVol/20))
Noise.setVolume(Vol)
Noise.play()

to the “begin routine” tab". and then add:

Noise.stop()
if Key_Resp==what_you_want:
        thisVol=thisVol+increase

to the “end routine” tab.


#4

I don’t know exactly how pyo is implemented inside psychopy but it has three buitlin noise generators (white, pink and brown), as simple as:

w = Noise(mul=0.1)
p = PinkNoise(mul=0.1)
b = BrownNoise(mul=0.1)

“mul” argument can be an ADSR envelope:

env = Adsr(attack=0.01, decay=0.05, sustain=0.707, release=0.1, dur=0, mul=1).play()
w = Noise(mul=env).out()

Olivier


#5

Thank you all for the helpful suggestions! I’m going to play around with this a bit and see how I can fit this in with my experiment. I’ll reply back in the near future with what I end up doing, in case this can help any others out!

Best,
Danielle


#6

Hi Becca,

I know this has been some time since you were assisting me with this coding problem. I am trying to implement the code you have, but I am a little confused.

For this code, is it to be added to the “each frame” tab?:

#set the sound
Noise=sound.SoundPyo(‘BrownNoiseT.wav’)#change this path to your sound/wav file
#What volume do you want to start at in dB.
thisVol = 50
#how much do you want the volume to increase by following a response?
increase=1

Most of this is making sense, but I am having difficulties with which tab each code goes into.

Thank you again for your assistance!


#7

Hi All,

I have been trying to resolve this issue using a number of different tactics, and although I seem to be getting closer to a solution, I am still having major issues. It also seems that I did not provide much background before though:

I am using a Windows 7 32-bit computer, and have recently updated Psychopy to v1.85.2.

I’ve gone through and programmed my experiment sans sound to this point, and it’s basically an S1S2 task at this point. Everything works well and runs without error.

Now, to add sound in, I’ve started to run into problems. I tried to add sound in (as per Becca’s recommendation), and was quite unsuccessful. At this point I am not sure where each line of code goes into my code components/how many code components I need to make this run successfully. I have made a whitenoise.wav file using Audacity >> Thank you for the suggestion! @lpxrh6

Next, I have tried to implement this as a simple Sound component. Essentially, I need white noise for the duration of the task at 70db, and then for the S2 the sound should increase to 115db. I’ve inserted the S2 sound component, and my experiment runs without error. However, my visual stimuli are not presenting. After the onset of the first S2, the screen goes blank and no sound plays. This remains until the experiment is over and the final screen presents, “Thank you for completing the task. Please wait for the experimenter.”

Without changing anything, my experiment began crashing too. After terminating the program, my output reads “Fatal Python error: (pygame parachute) Segmentation Fault”.

If there are any ideas on what I might do to address these issues, input would be greatly appreciated!

Thank you,
Danielle


#8

Hi There

Sorry for the delayed response, correct me if I have misunderstood your query but I think I said after each bit of code which tab it should go into?:slight_smile: that bit of code should go in the “begin experiment” tab the next bit in the “begin routine” tab and the final bit in the "end routine tab!

Sorry if i just repeated information and this wasn’t what you were meaning!

Thanks,
Becca


#9

Also this error can sometimes be fixed if you go into preferences, click on audio you should see an option in which it reads:

u’pygame’,u’pyo’

try swapping these around - sometimes this worked for me


#10

Hello Becca,

Thank you for your continued help! I’ve done as suggested (swapping u’pygame’,u’pyo’ in preferences). Unfortunately, I run into problems either way.

When my preferences read:
[u’pyo’, u’pygame’]

I get the following error:

image

When my preferences read:
[u’pygame’, u’pyo’]

I get this much more lengthy error:

image

I’ve also tested this with a simple test experiment just using a sound component. I have switched the sound component too in case the one I developed in audacity had some problem.

Has anyone run into these problems before??

Thank you as always!
Danielle


#11

And (sorry to ask) are you certain that you have the file whitenoise.wav in the correct folder (Desktop\DCC)

Is it really also the case that you’ve installed PsychoPy into a folder on the desktop rather than Program Files?!


#12

Hello Jon,

Great Questions!

The sound file is in the correct location. The error does seem to imply that it cannot find the file though. Also, I only have it placed on a file there for temporary reasons. We currently have other paradigms running on the old version of psychopy and I placed it there so as to not cause problems with our others tasks.