📜  pyaudio mic 流 - Python (1)

📅  最后修改于: 2023-12-03 15:18:44.387000             🧑  作者: Mango

Pyaudio Mic Stream - Python

Pyaudio is a Python library for working with audio. It provides a way to easily access and manipulate audio data. With Pyaudio, you can record and play audio, create audio effects, and manipulate audio streams.

One of the powerful features of Pyaudio is its ability to access microphone input. This allows you to create applications that can capture and process audio input in real-time.

In this tutorial, we will explore how to use Pyaudio to stream microphone input in Python.

Prerequisites

Before we begin, let's make sure that Pyaudio is installed on your system. You can install it using pip:

pip install pyaudio
Streaming Microphone Input

To stream microphone input, we first need to create a Pyaudio stream object. This object will be used to read audio data from the microphone.

import pyaudio

# create an input stream object
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)

This code creates a Pyaudio object and opens an input stream with the following parameters:

  • format: The format of the audio data. In this case, it is 16-bit integer samples.
  • channels: The number of audio channels. In this case, it is 1 (mono).
  • rate: The sampling rate of the audio data. In this case, it is 44.1 kHz.
  • input: Specifies that this stream is an input stream (i.e., we are capturing audio).
  • frames_per_buffer: The number of audio frames per buffer. In this case, it is set to 1024.

Next, we can start streaming microphone data by reading audio data from the stream in a loop.

while True:
    data = stream.read(1024)
    # do something with the audio data

This code reads 1024 audio frames from the stream and stores them in a variable called data. We can then process this data in some way.

Processing Audio Data

Once we have captured audio data from the microphone, we can process it in various ways using Python. For example, we might want to apply audio effects or perform speech recognition.

Let's look at an example of processing audio data using the Python wave module. This module allows us to write audio data to a WAV file.

import wave

# create a WAV file
wf = wave.open("audio.wav", "wb")
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
wf.setframerate(44100)

# read audio data from the microphone and write it to the WAV file
while True:
    data = stream.read(1024)
    wf.writeframes(data)

This code sets up a WAV file with the same parameters as our input stream. It then reads audio data from the microphone and writes it to the WAV file in a loop.

Conclusion

In this tutorial, we have explored how to use Pyaudio to stream microphone input in Python. We have also seen how to process audio data in various ways using Python modules such as wave.

With Pyaudio, you can create powerful audio applications that capture and manipulate audio data in real-time.