1

someone made a script who is supposed to record a wave file and send it at an email address.

I want instead of send it at an email address, save the file.

Here is the code :

import sounddevice as sd
import wave

fs = 44100
seconds = 60
obj = wave.open('sound.wav', 'w')
obj.setnchannels(1)
obj.setsampwidth(2)
obj.setframerate(fs)
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
obj.writeframesraw(myrecording)
sd.wait()

After launching the script, I have an empty wave file. Is there something to save the file or something like that obj.save(r'XX')?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Askaat
  • 11
  • 1
  • You forgot to close the file(`obj.close()`) – Tehnorobot Aug 31 '21 at 13:11
  • Did you even take a look at [the docs](https://docs.python.org/3/library/wave.html)? It specifically recommends to use a `with` statement so that the file is automatically closed... – Tomerikoo Aug 31 '21 at 15:08
  • There is no need to cast `int(seconds * fs)` since `seconds` and `fs` are already `int` themselves. Also, post the original code (the one sending emails) if possible. – accdias Aug 31 '21 at 15:10

1 Answers1

0

U need to close the wave object

import sounddevice as sd
import wave

fs = 44100
seconds = 60
obj = wave.open('sound.wav', 'w')
obj.setnchannels(1)
obj.setsampwidth(2)
obj.setframerate(fs)
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
obj.writeframesraw(myrecording)
sd.wait()
obj.close()