如何从 numpy 数组生成音频? [英] How to generate audio from a numpy array?

查看:60
本文介绍了如何从 numpy 数组生成音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create "heart rate monitor" effect from a 2D array in numpy and want the tone to reflect the values in the array.

推荐答案

您可以使用 write 函数来自 scipy.io.wavfile 以创建一个 wav 文件,然后您可以播放随心所欲.请注意,数组必须是整数,因此如果您有浮点数,您可能需要适当地缩放它们:

You can use the write function from scipy.io.wavfile to create a wav file which you can then play however you wish. 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)

如果您希望 Python 真正播放音频,那么此页面提供了一些包/模块.

If you want Python to actually play audio, then this page provides an overview of some of the packages/modules.

这篇关于如何从 numpy 数组生成音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆