[Python about sound operations] Simple understanding

There are many libraries for processing sounds in Python, such as:

  1. PyAudio: A Python library for recording and playing audio data.

  2. Wave: A module in the Python standard library for reading and writing WAV files.

  3. NumPy: A Python library for processing numerical data, including audio data.

  4. SciPy: A Python library for scientific computing, including audio signal processing.

  5. librosa: A Python library for audio signal processing and analysis.

Using these libraries you can do a lot of things, such as:

  1. Record sound and save as WAV file.

  2. Read WAV files and play sounds.

  3. Analyze audio signals, such as calculating spectrum, time domain features, and frequency domain features.

  4. Perform filtering, noise reduction, gain and other processing on audio signals.

  5. Identify speech content in audio signals.

Here is a simple example that demonstrates how to use PyAudio to record sound and save it as a WAV file:

import pyaudio
import wave

# 设置参数
CHUNK = 1024
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("* 录制开始")

# 录制音频数据
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* 录制结束")

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

# 保存音频数据为WAV文件
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()

In this example, we use PyAudio to open an audio stream and set the parameters. We then loop through the audio data and save it into a list. Finally, we turn off the audio stream and save the data as a WAV file using the Wave module.

Guess you like

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