Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

make a sound from live while saving as a file

i’m actually making an audio recorder that must record while a condition is True but my code can record limited seconds

from sounddevice import rec,wait
from scipy.io.wavfile import write
a=rec(10*44100,44100,2)
wait()
write('output.wav',44100,a)

any advice?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The rec(…) function in the line a=rec(10*44100,44100,2) returns a NumPy array.
As the NumPy reference states, numpy.append(…) function appends two arrays together. So you can create an empty NumPy array at the top of your code and call rec(…) function for short periods of time then append returning array to it until your while condition is valid.

Edit:

Here is one way to do it. Not tested.

from sounddevice import rec,wait
from scipy.io.wavfile import write
import numpy as np

initial_rec = rec(1*44100,44100,2) #this gets the shape of np array returned from record function. this data is not used.
wait()

record = np.empty_like(sample_rec) #creating an empty np array in the same shape of example initial recording.

while True: #insert your condition here 
    sample = rec(1*44100,44100,2)
    wait()
    np.append(record, sample)

write('output.wav',44100,record)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading