Reputation: 11
I have to write an echo code. So I wrote this code. I want to know how to add each of these in a separate wav file. Can someone provide me with an answer. Thanks in advance.
import sounddevice as sd
from scipy.io import wavfile
import numpy as np
from scipy.io.wavfile import write
fs,x=wavfile.read('hello.wav')
amp=1
for i in range (2,6):
nx=(amp/i**3)*x
sd.play(nx,fs)
sd.wait()
write('hello[i]',fs,myrecording)
Upvotes: 1
Views: 309
Reputation: 391
There are two things you need to do here:
replace myrecording
(which isn't defined in your code snippet) with the sound data array that you want to write out to file. Inspection of your code makes me think it is actually nx
that you want to write out.
you'll need a different filename for each file written out or else you will just write over the same file and you'll only be left with the one written out last. There are many ways to generate/structure the string for this filename but, based on what you had, I used one that will label the files things like "hello_2.wav" etc.
I have integrated both those things into the code here:
import sounddevice as sd
from scipy.io import wavfile
from scipy.io.wavfile import write
fs, x = wavfile.read('hello.wav')
AMP = 1
for i in range(2, 6):
nx = (AMP/i**3) * x
sd.play(nx, fs)
sd.wait()
filename = f'hello_{i}.wav'
write(filename, fs, nx)
Upvotes: 1