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?
>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)