如何从pydub AudioSegment创建一个numpy数组?

时间:2022-10-26 21:45:30

I'm aware of the following question: How to create a pydub AudioSegment using an numpy array?

我知道以下问题:如何使用numpy数组创建pydub AudioSegment?

My question is the right opposite. If I have a pydub AudioSegment how can I convert it to a numpy array?

我的问题恰恰相反。如果我有一个pydub AudioSegment怎么能把它转换为numpy数组?

I would like to use scipy filters and so on. It is not very clear to me what is the internal structure of the AudioSegment raw data.

我想使用scipy过滤器等。我不太清楚AudioSegment原始数据的内部结构是什么。

2 个解决方案

#1


4  

Pydub has a facility for getting the audio data as an array of samples, it is an array.array instance (not a numpy array) but you should be able to convert it to a numpy array relatively easily:

Pydub有一个用于将音频数据作为样本数组获取的工具,它是一个array.array实例(不是numpy数组),但你应该能够相对容易地将它转换为numpy数组:

from pydub import AudioSegment
sound = AudioSegment.from_file("sound1.wav")

# this is an array
samples = sound.get_array_of_samples()

You may be able to create a numpy variant of the implementation though. That method is implemented pretty simply:

您可能可以创建实现的numpy变体。该方法非常简单:

def get_array_of_samples(self):
    """
    returns the raw_data as an array of samples
    """
    return array.array(self.array_type, self._data)

Creating a new audio segment from a (modified?) array of samples is also possible:

从(修改的?)样本数组创建新的音频片段也是可能的:

new_sound = sound._spawn(samples)

The above is a little hacky, it was written for internal use within the AudioSegment class, but it mainly just figures out what type of audio data you're using (array of samples, list of samples, bytes, bytestring, etc). It's safe to use despite the underscore prefix.

以上是有点hacky,它是为AudioSegment类内部使用而编写的,但它主要只是确定你正在使用什么类型的音频数据(样本数组,样本列表,字节,字节串等)。尽管有下划线前缀,但使用它是安全的。

#2


2  

You can get an array.array from an AudioSegment then convert it to a numpy.ndarray:

您可以从AudioSegment获取array.array,然后将其转换为numpy.ndarray:

from pydub import AudioSegment
import numpy as np
song = AudioSegment.from_mp3('song.mp3')
samples = song.get_array_of_samples()
samples = np.array(samples)

#1


4  

Pydub has a facility for getting the audio data as an array of samples, it is an array.array instance (not a numpy array) but you should be able to convert it to a numpy array relatively easily:

Pydub有一个用于将音频数据作为样本数组获取的工具,它是一个array.array实例(不是numpy数组),但你应该能够相对容易地将它转换为numpy数组:

from pydub import AudioSegment
sound = AudioSegment.from_file("sound1.wav")

# this is an array
samples = sound.get_array_of_samples()

You may be able to create a numpy variant of the implementation though. That method is implemented pretty simply:

您可能可以创建实现的numpy变体。该方法非常简单:

def get_array_of_samples(self):
    """
    returns the raw_data as an array of samples
    """
    return array.array(self.array_type, self._data)

Creating a new audio segment from a (modified?) array of samples is also possible:

从(修改的?)样本数组创建新的音频片段也是可能的:

new_sound = sound._spawn(samples)

The above is a little hacky, it was written for internal use within the AudioSegment class, but it mainly just figures out what type of audio data you're using (array of samples, list of samples, bytes, bytestring, etc). It's safe to use despite the underscore prefix.

以上是有点hacky,它是为AudioSegment类内部使用而编写的,但它主要只是确定你正在使用什么类型的音频数据(样本数组,样本列表,字节,字节串等)。尽管有下划线前缀,但使用它是安全的。

#2


2  

You can get an array.array from an AudioSegment then convert it to a numpy.ndarray:

您可以从AudioSegment获取array.array,然后将其转换为numpy.ndarray:

from pydub import AudioSegment
import numpy as np
song = AudioSegment.from_mp3('song.mp3')
samples = song.get_array_of_samples()
samples = np.array(samples)