Muzz5
Muzz5

Reputation: 481

Real-time procedural sounds with 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.

Upvotes: 4

Views: 2363

Answers (3)

PatriceG
PatriceG

Reputation: 4063

Pyglet could be a good option as it embeds functions for audio procedural: Pyglet/media_procedural

Upvotes: 0

John Vinyard
John Vinyard

Reputation: 13485

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

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))

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)

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

Upvotes: 3

japreiss
japreiss

Reputation: 11251

Check out this Python wiki page. Particularly the "Music programming in Python" section.

Upvotes: 0

Related Questions