简体   繁体   English

如何从 numpy 数组生成音频?

[英]How to generate audio from a numpy array?

我想从 numpy 中的二维数组创建“心率监视器”效果,并希望音调反映数组中的值。

You can use the write function from scipy.io.wavfile to create a wav file which you can then play however you wish.您可以使用scipy.io.wavfilewrite函数创建一个 wav 文件,然后您可以随意播放该文件。 Note that the array must be integers, so if you have floats, you might want to scale them appropriately:请注意,数组必须是整数,因此如果您有浮点数,您可能需要适当地缩放它们:

import numpy as np
from scipy.io.wavfile import write

data = np.random.uniform(-1,1,44100) # 44100 random samples between -1 and 1
scaled = np.int16(data/np.max(np.abs(data)) * 32767)
write('test.wav', 44100, scaled)

If you want Python to actually play audio, then this page provides an overview of some of the packages/modules.如果您希望 Python 实际播放音频,那么此页面提供了一些包/模块的概述。

For the people coming here in 2016 scikits.audiolab doesn't really seem to work anymore.对于 2016 年来到这里的人来说,scikits.audiolab 似乎不再起作用了。 I was able to get a solution using sounddevice.我能够使用 sounddevice 获得解决方案。

import numpy as np
import sounddevice as sd

fs = 44100
data = np.random.uniform(-1, 1, fs)
sd.play(data, fs)

in Jupyter the best option is:在 Jupyter 中,最好的选择是:

from IPython.display import Audio
Audio(numpy.sin(numpy.linspace(0, 3000, 20000)), rate=20000)

In addition, you could try scikits.audiolab .此外,您可以尝试scikits.audiolab It features file IO and the ability to 'play' arrays.它具有文件 IO 和“播放”数组的能力。 Arrays don't have to be integers.数组不一定是整数。 To mimick dbaupp's example:模仿 dbaupp 的例子:

import numpy as np
import scikits.audiolab

data = np.random.uniform(-1,1,44100)
# write array to file:
scikits.audiolab.wavwrite(data, 'test.wav', fs=44100, enc='pcm16')
# play the array:
scikits.audiolab.play(data, fs=44100)

I had some problems using scikit.audiolabs , so I looked for some other options for this task.我在使用scikit.audiolabs遇到了一些问题,所以我为这个任务寻找了一些其他选项。 I came up with sounddevice , which seems a lot more up-to-date.我想出了sounddevice ,它似乎更新了很多。 I have not checked if it works with Python 3.我还没有检查它是否适用于 Python 3。

A simple way to perform what you want is this:执行您想要的操作的一种简单方法是:

import numpy as np
import sounddevice as sd

sd.default.samplerate = 44100

time = 2.0
frequency = 440

# Generate time of samples between 0 and two seconds
samples = np.arange(44100 * time) / 44100.0
# Recall that a sinusoidal wave of frequency f has formula w(t) = A*sin(2*pi*f*t)
wave = 10000 * np.sin(2 * np.pi * frequency * samples)
# Convert it to wav format (16 bits)
wav_wave = np.array(wave, dtype=np.int16)

sd.play(wav_wave, blocking=True)

PyGame has the module pygame.sndarray which can play numpy data as audio. pygame.sndarraypygame.sndarray模块,它可以将 numpy 数据作为音频播放。 The other answers are probably better, as PyGame can be difficult to get up and running.其他答案可能更好,因为 PyGame 可能难以启动和运行。 Then again, scipy and numpy come with their own difficulties, so maybe it isn't a large step to add PyGame into the mix.再说一次,scipy 和 numpy 都有自己的困难,所以也许将 PyGame 添加到组合中并不是很大的一步。

http://www.pygame.org/docs/ref/sndarray.html http://www.pygame.org/docs/ref/sndarray.html

Another modern and convenient solution is to use pysoundfile , which can read and write a wide range of audio file formats :另一种现代方便的解决方案是使用pysoundfile ,它可以读写各种音频文件格式

import numpy as np
import soundfile as sf

data = np.random.uniform(-1, 1, 44100)
sf.write('new_file.wav', data, 44100)

Not sure of the particulars of how you would produce the audio from the array, but I have found mpg321 to be a great command-line audio player, and could potentially work for you.不确定如何从阵列产生音频的细节,但我发现mpg321是一个很棒的命令行音频播放器,并且可能对你有用

I use it as my player of choice for Anki , which is written in python and has libraries that could be a great starting place for interfacing your code/arrays with audio.我使用它作为Anki的首选播放器,它是用 python 编写的,并且具有可以作为将代码/数组与音频接口的一个很好的起点。

Check out:查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM