【问题标题】:How to play sound from samples contained in NumPy array?如何从 NumPy 数组中包含的样本中播放声音?
【发布时间】:2015-11-27 10:51:20
【问题描述】:

我试图在 Matlab 中找到一个对应于 soundsc()sound() 的函数。基本上,我想通过播放 NumPy 数组中包含的样本来听声音。是否有一些功能可以做到这一点?

【问题讨论】:

  • 我的回答有帮助吗?

标签: matlab audio numpy scipy


【解决方案1】:

这适用于 Linux 和 Mac

大多数 Linux 计算机都预装了 vox 库,让您可以从命令行播放音频。

假设您使用scipy.io.write 将数组写入波形文件,您可以使用subprocess 模块在Python 程序中播放它。

这是一个完整的例子:

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


fs = 44100 # sampling frequency
input_array = np.random.rand(fs*2) # 2 seconds audio
write('output.wav', fs, input_array)

# now that file is written to the disk - play it
import subprocess
subprocess.call(["play", 'output.wav']) <-  for linux
subprocess.call(["afplay", 'output.wav']) <- for Mac

对于 Windows,据我所知,没有内置的命令行播放器 - 所以在使用上述代码之前,您可能需要安装一些允许您这样做的程序。

【讨论】:

    【解决方案2】:

    我不确定是否存在numpy 函数来执行此操作,但您可以使用this function 中的this function 将您的数组(只要它只包含整数)转换为wav 文件,然后播放该文件.

    我没有深入了解细节,但我认为this page 引用了 Python 中用于声音的有用工具。

    编辑:另见the answers to this SO question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 2011-01-08
      相关资源
      最近更新 更多