从现场录音并保存为文件时发出声音

huangapple go评论73阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年2月27日 10:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定