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 = wave.open('sound.wav', 'w')
obj.setnchannels(1)
obj.setsampwidth(2)
obj.setframerate(sampleRate)
obj.close()
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.