[Python about sound operations] First introduction to PyAudio

Pyaudio is a very popular audio processing library in Python. It can be used to record, play, process audio data, etc. Installing and using Pyaudio on Windows requires the following steps:

  1. Install Python

If you haven't installed Python yet, you can download and install it from the official website: https://www.python.org/downloads/windows/

  1. Install Pyaudio

To install Pyaudio on Windows, you can use the pip command, open the command line window, and enter the following command:

pip install pyaudio

If you encounter installation failure, you can try the following command:

pip install pipwin
pipwin install pyaudio
  1. Test Pyaudio

After the installation is complete, we can test whether Pyaudio is working properly. Enter the following code in the command line window:

import pyaudio

p = pyaudio.PyAudio()
print(p.get_device_count())

If the number of devices is output, Pyaudio has been installed successfully.

  1. Record audio

Recording audio with Pyaudio requires the following steps:

import pyaudio
import wave

# 设置录音参数
'''
在音频处理中,chunk(也称为帧)是指音频信号中的一小段连续采样数据。每个chunk通常包含几毫秒到几百毫秒的音频数据,具体取决于采
样率和帧率。在数字音频中,chunk是数字信号的基本单位,它们被用于压缩、存储、传输和处理音频数据。在音频处理中,通常需要对每个
chunk进行分析、处理或转换,以实现各种音频效果和功能。
'''
CHUNK = 1024
'''
`pyaudio.paInt16` 是 PyAudio 库中的一个常量,表示采样格式为 16 位整型的音频数据。在 PyAudio 中,采样格式是指每个采样点的编
码方式,常见的采样格式还有 `pyaudio.paFloat32`(32 位浮点型)和 `pyaudio.paInt32`(32 位整型)等。采样格式的选择取决于应用
场景和硬件设备的支持情况
'''
FORMAT = pyaudio.paInt16

CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

# 初始化Pyaudio
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("* 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()

This code will record 5 seconds of audio and save it to the output.wav file.

  1. Play audio

Playing audio using Pyaudio requires the following steps:

import pyaudio
import wave

# 打开音频文件
wf = wave.open("output.wav", 'rb')

# 初始化Pyaudio
p = pyaudio.PyAudio()

# 打开音频流
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

# 播放音频
data = wf.readframes(1024)
while data != b'':
    stream.write(data)
    data = wf.readframes(1024)

# 关闭音频流
stream.stop_stream()
stream.close()
p.terminate()

This code will play the audio in the output.wav file.

  1. extended use

Pyaudio can also be used for audio processing, real-time audio analysis, etc. If you want to learn more about the use of Pyaudio, you can refer to the official documentation: https://people.csail.mit.edu/hubert/pyaudio/docs/

Guess you like

Origin blog.csdn.net/weixin_43958438/article/details/130626645