简体   繁体   English

在Python中生成正弦波声音

[英]Generating sine wave sound in Python

I need to generate a sine wave sound in Python, and I need to be able to control frequency, duration, and relative volume. 我需要在Python中生成正弦波声音,我需要能够控制频率,持续时间和相对音量。 By 'generate' I mean that I want it to play though the speakers immediately, not save to a file. 通过'生成'我的意思是我希望它立即通过扬声器播放,而不是保存到文件中。

What is the easiest way to do this? 最简单的方法是什么?

import pyaudio
import numpy as np

p = pyaudio.PyAudio()

volume = 0.5     # range [0.0, 1.0]
fs = 44100       # sampling rate, Hz, must be integer
duration = 1.0   # in seconds, may be float
f = 440.0        # sine frequency, Hz, may be float

# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)

# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=fs,
                output=True)

# play. May repeat with different volume values (if done interactively) 
stream.write(volume*samples)

stream.stop_stream()
stream.close()

p.terminate()

ivan-onys gave an excellent answer, but there is a little addition to it: this script will produce 4 times shorter sound than expected because Pyaudio write method needs string data of float32, but when you pass numpy array to this method, it converts whole array as entity to a string, therefore you have to convert data in numpy array to the byte sequence yourself like this: ivan-onys给出了一个很好的答案,但还有一点补充:这个脚本产生的声音比预期短4倍,因为Pyaudio write方法需要float32的字符串数据,但是当你将numpy数组传递给这个方法时,它会转换为整个数组作为字符串的实体,因此您必须将numpy数组中的数据转换为字节序列,如下所示:

samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32).tobytes()

and you have to change this line as well: 你也必须改变这一行:

stream.write(samples)

One of the more consistent andeasy to install ways to deal with sound in Python is the Pygame multimedia libraries. Pygame多媒体库是在Python中安装处理声音的更一致的安装方法之一。

I'd recomend using it - there is the pygame.sndarray submodule that allows you to manipulate numbers in a data vector that become a high-level sound object that can be playerd in the pygame.mixer module. 我建议使用它 - 有pygame.sndarray子模块允许你操纵数据向量中的数字,这些数字向量成为可以在pygame.mixer模块中播放的高级声音对象。

The documentation in the pygame.org site should be enough for using the sndarray module. pygame.org站点中的文档应该足以使用sndarray模块。

Today for Python 3.5+ the best way is to install the packages recommended by the developer. 今天对于Python 3.5+,最好的方法是安装开发人员推荐的软件包。

http://people.csail.mit.edu/hubert/pyaudio/ http://people.csail.mit.edu/hubert/pyaudio/

For Debian do 对于Debian来说

sudo apt-get install python3-all-dev portaudio19-dev

before trying to install pyaudio 在尝试安装pyaudio之前

I the bregman lab toolbox you have a set of functions that does exactly what you want. 我是bregman实验室工具箱,你有一套完全符合你想要的功能。 This python module is a little bit buggy but you can adapt this code to get your own functions 这个python模块有点儿错误,但您可以调整此代码以获得自己的功能

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

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