实时程序的声音与Python? [英] Real-time procedural sounds with Python?

查看:107
本文介绍了实时程序的声音与Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做程序的声音在Python,瞬间回放,而不是将它们保存到一个文件中。我应该是用这个?我可以只使用内置模块,否则我将需要额外的东西?

I want to do procedural sounds in Python, and instantly play them back rather than save them to a file. What should I be using for this? Can I just use built-in modules, or will I need anything extra?

我可能会想改变音高,音量,这样的事情。

I will probably want to change pitch, volume, things like that.

推荐答案

使用 scikits.audiolab 应该做的伎俩。 AUDIOLAB的 播放 它支持ALSA和核心音频后端功能。

Using numpy along with scikits.audiolab should do the trick. audiolab has a play function which supports the ALSA and Core Audio backends.

下面是一个如何你可能会产生简单的正弦波使用numpy的一个例子:

Here's an example of how you might produce a simple sine wave using numpy:

from __future__ import division
import numpy as np

def testsignal(hz,amplitude = .5,seconds=5.,sr=44100.):
    '''
    Create a sine wave at hz for n seconds
    '''
    # cycles per sample
    cps = hz / sr
    # total samples
    ts = seconds * sr
    return amplitude * np.sin(np.arange(0,ts*cps,cps) * (2*np.pi))

要在440赫兹产生正弦波五秒钟,听它,你会做:

To create five seconds of a sine wave at 440 hz and listen to it, you'd do:

>>> from scikits.audiolab import play
>>> samples = testsignal(440)
>>> play(samples)

注意播放是一个阻塞调用。直到声音已完成播放控制不会返回到你的code。

Note that play is a blocking call. Control won't be returned to your code until the sound has completed playing.

这篇关于实时程序的声音与Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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