如何同时录制 2 个音频提要?

How to record 2 audio feeds simultaneously?

我想录制一个音轨并将其保存为 2 个不同的 .wav 文件。音轨应延迟约 10 秒保存。 6 秒,每个 .wav 应为 12 秒长。

我试着用多处理和 pyaudio 来做,但我无法让它工作

请注意,我是 python 的初学者,这是我在 Whosebug 上的第一个 post!

def func1():
  #Record and save a 12 seconds long .wav 
def func2():
  #Record and save a 12 seconds long .wav 
if __name__ == '__main__':
  p1 = Process(target=func1)
  p1.start()
  p2 = Process(target=func2)
  p2.start()
  p1.join()
  p2.join()

#start func2 6 seconds after func1


I would expect a data structure like this:
|---1.wav---|---1.wav---|---1.wav---|
      |---2.wav---|---2.wav---|---2.wav---|
     6sec  12sec 18sec 24sec 30sec 36sec 42sec

编辑: 我想出了一些似乎工作得很好的代码。它有 0.144 秒的延迟。我很高兴改进这段代码。此代码使用线程而不是多处理。

import pyaudio
import wave
from threading import Thread
import time
from datetime import datetime

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
CHUNK1 = 1024
RECORD_SECONDS = 12
WAVE_OUTPUT_FILENAME1 = name = "outputs/output_1"+datetime.now().strftime("%m:%d:%Y-") 
WAVE_OUTPUT_FILENAME2 = name = "outputs/output_2"+datetime.now().strftime("%m:%d:%Y-") 


def func1():
    while 1==1:
        global FORMAT
        global CHANNELS
        global RATE
        global CHUNK
        global RECORD_SECONDS
        global WAVE_OUTPUT_FILENAME1
        WAVE_OUTPUT_FILENAME1 = name = "outputs/output1_"#+datetime.now().strftime("%m:%d:%Y-") 
        audio = pyaudio.PyAudio()
        stream = audio.open(format=FORMAT, channels=CHANNELS,
                        rate=RATE, input=True,
                        frames_per_buffer=CHUNK)
        print("recording...")
        frames = []
        WAVE_OUTPUT_FILENAME1 = WAVE_OUTPUT_FILENAME1+datetime.now().strftime("%H;%M;%S.%f--") 
        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)
        WAVE_OUTPUT_FILENAME1 = WAVE_OUTPUT_FILENAME1 + datetime.now().strftime("%H;%M;%S.%f")+".wav"
        print("finished recording")


        # stop Recording
        stream.stop_stream()
        stream.close()
        audio.terminate()



        waveFile = wave.open(WAVE_OUTPUT_FILENAME1, 'wb')
        waveFile.setnchannels(CHANNELS)
        waveFile.setsampwidth(audio.get_sample_size(FORMAT))
        waveFile.setframerate(RATE)
        waveFile.writeframes(b''.join(frames))
        waveFile.close() 

def func2():
    time.sleep(6)
    while 1==1:
        global FORMAT
        global CHANNELS
        global RATE
        global CHUNK1
        global RECORD_SECONDS
        global WAVE_OUTPUT_FILENAME2
        WAVE_OUTPUT_FILENAME2 = name = "outputs/output2_"#+datetime.now().strftime("%m:%d:%Y-") 
        audio = pyaudio.PyAudio()
        stream = audio.open(format=FORMAT, channels=CHANNELS,
                        rate=RATE, input=True,
                        frames_per_buffer=CHUNK1)
        print("recording...")
        frames = []
        WAVE_OUTPUT_FILENAME2 = WAVE_OUTPUT_FILENAME2+datetime.now().strftime("%H;%M;%S.%f--") 
        for i in range(0, int(RATE / CHUNK1 * RECORD_SECONDS)):
            data = stream.read(CHUNK1)
            frames.append(data)
        WAVE_OUTPUT_FILENAME2 = WAVE_OUTPUT_FILENAME2 + datetime.now().strftime("%H;%M;%S.%f")+".wav"
        print("finished recording")


        # stop Recording
        stream.stop_stream()
        stream.close()
        audio.terminate()

        waveFile = wave.open(WAVE_OUTPUT_FILENAME2, 'wb')
        waveFile.setnchannels(CHANNELS)
        waveFile.setsampwidth(audio.get_sample_size(FORMAT))
        waveFile.setframerate(RATE)
        waveFile.writeframes(b''.join(frames))
        waveFile.close() 

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

为什么你认为你需要 multiprocessing?我认为这只会使事情复杂化

如何只记录 6 秒(或更短)chunks/frames 并将正确的帧写入每个文件。

我有点得意忘形,写了一个很好的 class 来做到这一点:

import pyaudio
import wave
import time


class OverlappedRecorder:
    def __init__(
        self, secs_per_file, secs_between_file, *,
        num_channels=2, sample_rate=48000,
        sample_format=pyaudio.paInt16,
    ):
        # various constants needed later
        self.num_channels = num_channels
        self.sample_width = pyaudio.get_sample_size(sample_format)
        self.sample_rate = sample_rate
        self.frames_between_start = int(secs_between_file * sample_rate)
        self.frames_per_file = int(secs_per_file * sample_rate)

        # mutable state needed to keep everything going
        self.files = []
        self.frames_till_next_file = 0

        self.pa = pyaudio.PyAudio()
        self.stream = self.pa.open(
            format=sample_format, channels=num_channels,
            rate=sample_rate, frames_per_buffer=1024,
            input=True, start=False,
            stream_callback=self._callback,
        )

    def sleep_while_active(self):
        while self.stream.is_active():
            time.sleep(0.2)

    def begin_wave_file(self):
        "internal function to start a new WAV file"
        path = time.strftime(
            'recording-%Y-%m-%d-%H.%M.%S.wav',
            time.localtime()
        )
        file = wave.open(path, 'wb')
        file.setnchannels(self.num_channels)
        file.setsampwidth(self.sample_width)
        file.setframerate(self.sample_rate)
        self.files.append(file)

    # context manager stuff, recording starts when entered using "with"
    def __enter__(self):
        self.stream.start_stream()
        return self

    # exiting shuts everything down
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stream.stop_stream()
        self.stream.close()
        self.pa.terminate()

        for file in self.files:
            file.close()

    # called by pyaudio when a new set of frames are ready
    def _callback(self, data, frame_count, time_info, status):
        self.frames_till_next_file -= frame_count
        # see if we need to start a new file
        if self.frames_till_next_file < 0:
            self.frames_till_next_file += self.frames_between_start
            self.begin_wave_file()

        # can't remove from lists while iterating
        # keep a list of files to close and remove later
        done = []
        for file in self.files:
            remain = self.frames_per_file - file.getnframes()

            # add appropriate amount of data to all open files
            if frame_count < remain:
                file.writeframesraw(data)
            else:
                remain *= self.sample_width * self.num_channels
                file.writeframesraw(data[:remain])
                done.append(file)

        # close anything that finished
        for file in done:
            file.close()
            self.files.remove(file)

        # tell pyaudio to keep going
        return (None, pyaudio.paContinue)

基本用法是:创建一个对象,使用with进入它,它会开始记录,当你退出时,它会停止并清理。

rec = OverlappedRecorder(12, 6)
with rec:
    time.sleep(30)

让它 运行 持续 30 秒,或者你可以这样做:

with OverlappedRecorder(12, 6) as rec:
    rec.sleep_while_active()

让它 运行 直到你按下 Ctrl+C 来终止程序,或者你可以在那里调用 input() 让它在你按 enter 或其他任何东西时停止你喜欢

对您发布的代码的一些评论:

  • 如果要修改它们,您只需要声明 global 个变量
  • 为什么你有单独的功能?为什么不只有一个函数,而只是延迟 start() 第二个 Thread
  • 为什么设置WAVE_OUTPUT_FILENAME1这么多次?只需保存 start_timeend_time,然后一次性格式化字符串
  • 您不必 read() 成块,如果您知道它会适合内存,只需一次读取所有内容即可
  • 您不需要一直开始和停止录音,只需在每个线程中打开一次,如果您幸运的话,在将 wav 文件写入磁盘时样本会累积在缓冲区中

类似于:

import pyaudio
import wave
import time
from datetime import datetime
from threading import Thread

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 12


def recorder(prefix):
    audio = pyaudio.PyAudio()
    stream = audio.open(
        format=FORMAT, channels=CHANNELS,
        rate=RATE, input=True,
    )
    try:
        while True:
            start_time = datetime.now()
            print("recording started", start_time)

            data = stream.read(RATE * RECORD_SECONDS, False)

            end_time = datetime.now()
            print("finished", end_time)

            name = f'{prefix}{start_time:%Y-%m-%d-%H-%M-%S.%f}-{end_time:%H-%M-%S.%f}.wav'
            print("writing", name)
            with wave.open(name, 'wb') as waveFile:
                waveFile.setnchannels(CHANNELS)
                waveFile.setsampwidth(audio.get_sample_size(FORMAT))
                waveFile.setframerate(RATE)
                waveFile.writeframes(data)
    finally:
        stream.stop_stream()
        stream.close()
        audio.terminate()


if __name__ == '__main__':
    Thread(target=recorder, args=('outputs/output_1-',)).start()
    time.sleep(6)
    Thread(target=recorder, args=('outputs/output_2-',)).start()

一些差异:

  • 使用线程的版本代码少得多!
  • 我的版本允许任意数量的文件,而无需为每个文件使用多个 OS 线程(有 Python 线程和 pyaudio 有一个内部线程负责处理音频缓冲区)
  • 我的版本保存了部分文件

希望所有的帮助/有意义!