简体   繁体   English

Python-录制和播放麦克风输入

[英]Python - recording and playing microphone input

I am working on an app that receives audio from the user (with a microphone) and plays it back. 我正在开发一个可从用户(使用麦克风)接收音频并进行播放的应用程序。 Does anyone have a way/module that can store audio as an object (not as a .wav/.mp3) from a microphone? 有没有人可以通过麦克风将音频存储为对象(而不是.wav / .mp3)的方式/模块?

Btw, it's on Windows, if it matters. 顺便说一句,如果重要的话,它在Windows上。

Thank you all for your help! 谢谢大家的帮助!

pyaudio can be used to store audio as an stream object. pyaudio可用于将音频存储为流对象。

On windows you can install pyaudio as python -m pip install pyaudio 在Windows上,您可以将pyaudio安装为python -m pip install pyaudio

Here is an example taken from pyaudio site which takes audio from microphone for 5 seconds duration then stores audio as stream object and plays back immediately . 这是从pyaudio站点获取的示例, 该站点从麦克风获取音频持续5秒钟,然后将音频存储为流对象并立即播放。

You can modify to store stream object for different duration, manipulate then play it back. 您可以修改以存储流对象不同的持续时间,进行操作然后再播放。 Caution: Increase in duration will increase memory requirement. 注意:持续时间的增加将增加内存需求。

"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
"""

import pyaudio

CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5

p = pyaudio.PyAudio()

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

print("* recording")

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)  #read audio stream
    stream.write(data, CHUNK)  #play back audio stream

print("* done")

stream.stop_stream()
stream.close()

p.terminate()

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

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