Niki
Niki

Reputation: 66

How to write every x seconds a sound into an audio file (e.g. .mp3) in Python?

Is there a way to write for example every 2 seconds a sound in a (e.g. .mp3 or .wav) file in python? For example if I have a loop like this

x=0
while x<10:
       #write sound.wav into an audio file (file.mp3)
       time.sleep(2)
       x=x+1

So as a result I want to have an audio file where you hear a sound every 2 seconds (and that for example 10 times)

Thanks in advance

Upvotes: 2

Views: 361

Answers (1)

Jussi Nurminen
Jussi Nurminen

Reputation: 2408

It doesn't work like that. If you sleep(2), the Python interpreter will just idle for 2 seconds and not do anything, i.e. it will pause any file writes and resume after the sleep is over. What you need to do is to write your sound data, then write two seconds of silence (zero-valued samples), then write the sound data again etc.

Upvotes: 1

Related Questions