-1

I generaed a beep sound in Python, that exists for 5ms, and repeats after every 1s, for next 10s.

The codes are as such:

## Import modules
import time
import sys
import winsound
import soundfile as sf 


## Set frequency and duration of beep sound
frequency = 37  
duration  =  5   # duration of beep sound is 5ms

for i in range(1,10):                                   # create beep sound for 10s

    ## Create beep sound for 5 ms, after every 1 s
    sys.stdout.write('\r\a{i}'.format(i=i))
    sys.stdout.flush()
    winsound.Beep(frequency, duration)
    time.sleep(1)                                       # Repeat beep sound after 1s

Now, I want to save this pattern in a .wav file. Hence, I changed the codes as such:

## Import modules

import time
import sys
import winsound
import soundfile as sf 


## Set frequency and duration of beep sound
frequency = 37  
duration  =  5    # duration of beep sound is 5ms 

for i in range(1,10):  # create beep spund for 10 s

    ## Create beep sound for 5 ms, after every 1 s
    sys.stdout.write('\r\a{i}'.format(i=i))
    sys.stdout.flush()
    winsound.Beep(frequency, duration)
    time.sleep(1)

    
## Save the beep spund as a .wav file
sf.write("Beep.wav", winsound.Beep(frequency, duration))

However, I keep geeting errors.

Can somebady please let me know how do I save this in a .wav file ?

LearnerABC
  • 51
  • 1
  • 1
  • 7

1 Answers1

0

The error is this:

write() missing 1 required positional argument: 'samplerate'

from the docs here: https://pysoundfile.readthedocs.io/en/latest/

we can see that the function requires 3 arguments:

sf.write('new_file.flac', data, samplerate)

The code in the question has provided only two arguements.

The 3 arguements are:

  1. file (str or int or file-like object)
  2. data (array_like)
  3. samplerate (int)

It samplerate is missing.

With the docs given, this works:

## Import modules

import time
import sys
import numpy as np
import winsound
import soundfile as sf 


## Set frequency and duration of beep sound
frequency = 37  
duration  =  5    # duration of beep sound is 5ms 

for i in range(1,10):  # create beep spund for 10 s

    ## Create beep sound for 5 ms, after every 1 s
    sys.stdout.write('\r\a{i}'.format(i=i))
    sys.stdout.flush()
    winsound.Beep(frequency, duration)
    time.sleep(1)

    
## Save the beep spund as a .wav file
file = 'G:\\My Drive\\darren\\02_programming\python\\tests\\Beep.wav'
with sf.SoundFile(file, 'w', 44100, 2, 'PCM_24') as f:
    f.write(np.random.randn(10, 2))
D.L
  • 4,339
  • 5
  • 22
  • 45
  • Hi, I applied the given codes. However, the 'Beep.wav' file comes out to be an empty file. Could you please let me know where am I going wrong – LearnerABC Mar 29 '22 at 04:13
  • the best place for a fill implementation would be in the docs in the answer (https://pysoundfile.readthedocs.io/en/latest/). If you struggle from there, it would be better to open a new question as this one is answered. – D.L Mar 29 '22 at 06:15
  • a quick note: it is helpful to you to ask questions in this format: https://stackoverflow.com/help/minimal-reproducible-example – D.L Mar 29 '22 at 06:17