英文:
make a sound from live while saving as a file
问题
我正在制作一个音频录制器,必须在条件为真时录制,但我的代码只能录制有限的秒数。
from sounddevice import rec, wait
from scipy.io.wavfile import write
a = rec(10 * 44100, 44100, 2)
wait()
write('output.wav', 44100, a)
有什么建议吗?
英文:
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?
答案1
得分: 0
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论