gpt4 book ai didi

python - Mac os El Capitan 用 python 录制声音

转载 作者:行者123 更新时间:2023-12-05 07:47:47 29 4
gpt4 key购买 nike

我正在尝试录制来自麦克风的声音。首先使用 PyAudio,然后使用 sounddevice,但都失败了。

这是 PyAudio 的代码:

import pyaudio


def _recording_loop(samples_queue, running, stream, chunk_size):
stream.start_stream()

while running.is_set():
samples_queue.put(stream.read(chunk_size))

stream.stop_stream()


class Recoder:

def __init__(self, frame_rate, period):
self.proc = None
self.running = Event()
self.samples_queue = Queue()
self.frame_rate = frame_rate
self.chunk_size = (frame_rate*period) / 1000
self.channels = 1

self._pa = pyaudio.PyAudio()
self._stream = None

def start(self):
if self.proc is None:
self._stream = self._pa.open(format=pyaudio.paInt8,
channels=self.channels,
rate=self.frame_rate,
input=True,
frames_per_buffer=self.chunk_size)

self.running.set()
self.proc = Process(target=_recording_loop, args=[self.samples_queue, self.running, self._stream,
self.chunk_size])
self.proc.start()

def stop(self):
if self.proc is not None:
self.running.clear()
self.proc.join()

self._stream.close()
self._pa.terminate()

def empty(self):
return self.samples_queue.empty()

def read(self):
res = []
while not self.samples_queue.empty():
res.append(self.samples_queue.get())
return res

它给了我一个警告:Python[21648:645093] 13:42:01.242 警告:140:此应用程序或其使用的库正在使用已弃用的 Carbon 组件管理器来托管音频单元。在未来的版本中将删除对此的支持。此外,这使得主机与版本 3 音频单元不兼容。请转换到 AudioComponent.h 中的 API。
并且没有任何记录。

据我了解,这是 El Capitan 的问题,尚未解决。但也许我错了?

所以我决定将库切换到声音设备:

from multiprocessing import Process, Queue, Event
import sounddevice as sd


def _recording_loop(samples_queue, running, frame_rate, chunk_size):
while running.is_set():
samples_queue.put(sd.rec(chunk_size, samplerate=frame_rate, channels=1,
dtype='int8', blocking=True))


class Recoder:

def __init__(self, frame_rate, period):

self.proc = None
self.running = Event()
self.samples_queue = Queue()
self.frame_rate = frame_rate
self.period = period

self.chunk_size = (frame_rate * period) / 1000

def start(self):
if self.proc is None:
self.running.set()
self.proc = Process(target=_recording_loop, args=[self.samples_queue, self.running, self.frame_rate,
self.chunk_size])
self.proc.start()

def stop(self):
if self.proc is not None:
self.running.clear()
self.proc.join()

def empty(self):
return self.samples_queue.empty()

def read(self):
res = []
while not self.samples_queue.empty():
res.append(self.samples_queue.get())

return res

它说:

||PaMacCore (AUHAL)|| Warning on line 530: err=''who?'', msg=Audio Hardware: Unknown Property
||PaMacCore (AUHAL)|| Warning on line 534: err=''who?'', msg=Audio Hardware: Unknown Property
||PaMacCore (AUHAL)|| Warning on line 445: err=''who?'', msg=Audio Hardware: Unknown Property

再次没有任何记录。我做错了什么?

最佳答案

sounddevice.rec()不应该那样使用。您只需使用要记录的帧数调用它即可(请参阅 example from the docs ):

import sounddevice as sd

fs = 44100
duration = 10 # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,
blocking=True)

就是这样。录制一些声音不需要半页代码。

顺便说一句,您现在可以忽略警告,请参阅 https://github.com/spatialaudio/python-sounddevice/issues/10 .

关于python - Mac os El Capitan 用 python 录制声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39285145/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com