There are many libraries for processing sounds in Python, such as:
-
PyAudio: A Python library for recording and playing audio data.
-
Wave: A module in the Python standard library for reading and writing WAV files.
-
NumPy: A Python library for processing numerical data, including audio data.
-
SciPy: A Python library for scientific computing, including audio signal processing.
-
librosa: A Python library for audio signal processing and analysis.
Using these libraries you can do a lot of things, such as:
-
Record sound and save as WAV file.
-
Read WAV files and play sounds.
-
Analyze audio signals, such as calculating spectrum, time domain features, and frequency domain features.
-
Perform filtering, noise reduction, gain and other processing on audio signals.
-
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.