| Reference | Downloads | Github

Wav file not working in Builder

I’m trying to make an audio file of 500Hz play for 50ms in my experiment.

I have previously made this experiment with the code-viewer instead of Builder. The following code works in the code-viewer:

sd.default.samplerate = 44100

time = 0.050
frequency = 500

samples = np.arange(44100 * time) / 44100.0  # Generate time of samples between 0 and 50ms
wave = 10000 * np.sin(2 * np.pi * frequency * samples) # Recall that a sinusoidal wave of frequency f has formula w(t) = A*sin(2*pi*f*t)
wav_wave = np.array(wave, dtype=np.int16) # Convert it to wav format (16 bits)

Then, I simply call the wav_wave function when needed.

However, I’m now trying to build this experiment in the Builder, because I want to put it on Pavlovia. When I insert that above code into my trial “Begin Experiment” component and create a sound component that I enter as “$wav_wave”, it doesn’t work.

After looking around, I have also tried the following code in the same spot, and then called “sound.wav” in the sound component.

import wave, struct, math, random
sampleRate = 44100.0
duration = 0.05
frequency = 500
obj ='sound.wav', 'w')

I’ve noticed that if I create this audio file in the shell, I get the “sound.wav” file, but it does not play properly. Is it something to do with a corrupted wav file?

Thanks in advance.


Please search this forum for crib sheet.

There are several reasons why your code won’t work in Pavlovia.

I be found that it’s best to define sound with a Builder component but a delayed start time so it never starts. Then start and stop in code.

Best wishes


Hi Wakefield, thanks for your quick response! To clarify, my code doesn’t work in Psychopy itself. I am not bothering with the javascript conversion in Pavlovia yet…just trying to get it to work on my own computer first.

It’s up to you but if you don’t design with online in mind you may find that you are either trying to do something that isn’t possible or that needs lots of rewriting.

For example, imports won’t work.

Also saving files for later reuse during the same experiment won’t work.

Varying the frequency of a tone every few frames should be fine