简体   繁体   English

使用PyAudio将多层添加到音频录制

[英]Add multiple Layers to audio recording with PyAudio

Im been experimenting with audio recording and playback using pyaudio . 我一直在尝试使用pyaudio进行音频记录和播放。

I'm able to record a file but I need to be able to add layers on top of it. 我能够记录文件,但是我需要能够在文件之上添加图层。 What I'm trying to do is record a 10 second file then start playing. 我正在尝试录制10秒的文件,然后开始播放。

While its playing I want to continue to record additional 10 sec layers and add them over top of the prior recording. 在播放的同时,我想继续录制其他10秒的图层,并将其添加到先前的录音之上。

Is there a way to do this with Python? 有没有办法用Python做到这一点?

import pyaudio
import wave

CHUNK = 2
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print(int(RATE / CHUNK * RECORD_SECONDS))

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

I found a module called "PyDub" which does this easily. 我找到了一个名为“ PyDub”的模块,该模块可以轻松实现这一目的。

http://pydub.com/ http://pydub.com/

from pydub import AudioSegment

track1 = AudioSegment.from_file(track1wav)
track2 = AudioSegment.from_file(track2wav)

combined = track1.overlay(track2)
print ("overlaying recording")
combined.export(track1wav, format='wav')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM