1

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)        
AlexK
  • 2,855
  • 9
  • 16
  • 27
  • Just added an answer that I think addresses your question. But I have this remaining suspicion, since you mentioned echos, that you also wanted to combine all of these clips of increasingly diminished amplitude into one combined clip? – skumhest Aug 11 '22 at 20:20

1 Answers1

1

There are two things you need to do here:

  1. 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.

  2. 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)
skumhest
  • 401
  • 2
  • 10