📜  pydub audiosegment 到 numpy 数组 - Python (1)

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

Pydub AudioSegment to Numpy Array - Python

Pydub is a powerful audio processing library for Python that makes it easy to work with audio files. One common task is converting an AudioSegment from Pydub into a numpy array for further processing.

Here is an example of how to read an audio file using Pydub, convert it into a numpy array, and plot it using matplotlib:

import numpy as np
import matplotlib.pyplot as plt
from pydub import AudioSegment

# Load audio file using pydub
audio_file = AudioSegment.from_file("my_audio_file.wav")

# Convert audio file to numpy array
audio_array = np.array(audio_file.get_array_of_samples())

# Plot audio waveform
plt.plot(audio_array)
plt.xlabel("Time (samples)")
plt.ylabel("Amplitude")
plt.show()

In this example, we first load an audio file using Pydub's AudioSegment.from_file() function. We then convert the audio file into a numpy array using audio_file.get_array_of_samples(). Finally, we plot the audio waveform using matplotlib.

Note that the resulting numpy array will have one channel for mono audio, or two channels for stereo audio. If you have a multi-channel audio file, you can split the channels into separate numpy arrays using slicing:

import numpy as np
import matplotlib.pyplot as plt
from pydub import AudioSegment

# Load audio file using pydub
audio_file = AudioSegment.from_file("my_audio_file.wav")

# Convert audio file to numpy array
audio_array = np.array(audio_file.get_array_of_samples())

# Split audio array into left and right channels
left_channel = audio_array[::2]
right_channel = audio_array[1::2]

# Plot left and right audio waveforms
plt.subplot(2, 1, 1)
plt.plot(left_channel)
plt.xlabel("Time (samples)")
plt.ylabel("Amplitude")
plt.title("Left channel")

plt.subplot(2, 1, 2)
plt.plot(right_channel)
plt.xlabel("Time (samples)")
plt.ylabel("Amplitude")
plt.title("Right channel")

plt.show()

In this example, we first load an audio file using Pydub's AudioSegment.from_file() function. We then convert the audio file into a numpy array using audio_file.get_array_of_samples(). We split the audio array into left and right channels using slicing. Finally, we plot the left and right audio waveforms using matplotlib.

By using Pydub and numpy together, we can easily read and manipulate audio files in Python.