swmixer
index
/home/nwhitehe/Projects/pygalaxy/swmixer/swmixer.py

Advanced Realtime Software Mixer
 
This module implements an advanced realtime sound mixer suitable for
use in games or other audio applications.  It supports loading sounds
in uncompressed WAV format.  It can mix several sounds together during
playback.  The volume and position of each sound can be finely
controlled.  The mixer can use a separate thread so clients never block
during operations.  Samples can also be looped any number of times.
Looping is accurate down to a single sample, so well designed loops
will play seamlessly.  Also supports sound recording during playback.
 
Copyright 2008, Nathan Whitehead
Released under the LGPL

 
Modules
       
mad
numpy
pyaudio
thread
time
wave

 
Classes
       
Channel
Sound
StreamingSound

 
class Channel
    Represents one sound source currently playing
 
  Methods defined here:
__init__(self, src, env)
fadeout(self, time)
Schedule a fadeout of this sound in given time
get_position(self)
Return current position of sound in samples
get_volume(self)
Return current volume of sound
pause(self)
Pause the sound temporarily
set_position(self, p)
Set current position of sound in samples
set_volume(self, v, fadetime=0)
Set the volume of the sound
 
Removes any previously set envelope information.  Also
overrides any pending fadeins or fadeouts.
stop(self)
Stop the sound playing
unpause(self)
Unpause a previously paused sound

 
class Sound
    Represents a playable sound
 
  Methods defined here:
__init__(self, filename=None, data=None)
Create new sound from a WAV file, MP3 file, or explicit sample data
get_length(self)
Return the length of the sound in samples
 
To convert to seconds, divide by the samplerate and then divide
by 2 if in stereo.
play(self, volume=1.0, offset=0, fadein=0, envelope=None, loops=0)
Play the sound
 
Keyword arguments:
volume - volume to play sound at
offset - sample to start playback
fadein - number of samples to slowly fade in volume
envelope - a list of [offset, volume] pairs defining
           a linear volume envelope
loops - how many times to play the sound (-1 is infinite)
resample(self, scale)
Resample a sound
 
scale = 1.0 means original sound
scale = 0.5 is half as long (up an octave)
scale = 2.0 is twice as long (down an octave)
scale(self, vol)
Scale a sound sample
 
vol is 0.0 to 1.0, amount to scale by

 
class StreamingSound
    Represents a playable sound stream
 
  Methods defined here:
__init__(self, filename, checks=True)
Create new streaming sound from a WAV file or an MP3 file
 
The new streaming sound must match the output samplerate
and stereo-ness.  You can turn off these checks by setting
the keyword checks=False, but the sound will be distorted.
get_length(self)
Return the length of the sound stream in samples
 
Only available for MP3 streams, not WAV ones.  To convert
result to seconds, divide by the samplerate and then divide by
2.
play(self, volume=1.0, offset=0, fadein=0, envelope=None, loops=0)
Play the sound stream
 
Keyword arguments:
volume - volume to play sound at
offset - sample to start playback
fadein - number of samples to slowly fade in volume
envelope - a list of [offset, volume] pairs defining
           a linear volume envelope
loops - how many times to play the sound (-1 is infinite)

 
Functions
       
calc_vol(t, env)
Calculate volume at time t given envelope env
 
envelope is a list of [time, volume] points
time is measured in samples
envelope should be sorted by time
get_microphone()
Return raw data from microphone as Numpy array
 
Default format will be 16-bit signed mono.  Format will match
audio playback.  You must call tick() every frame to update the
results from this function.
init(samplerate=44100, chunksize=1024, stereo=True, microphone=False, input_device_index=None, output_device_index=None)
Initialize mixer
 
Must be called before any sounds can be played or loaded.
 
Keyword arguments:
samplerate - samplerate to use for playback (default 22050)
chunksize - size of playback chunks
  smaller is more responsive but perhaps stutters
  larger is more buffered, less stuttery but less responsive
  Can be any size, does not need to be a power of two. (default 1024)
stereo - whether to play back in stereo
microphone - whether to enable microphone recording
interleave(left, right)
Given two separate arrays, return a new interleaved array
 
This function is useful for converting separate left/right audio
streams into one stereo audio stream.  Input arrays and returned
array are Numpy arrays.
 
See also: uninterleave()
microphone_off()
Turn off microphone
microphone_on()
Turn on microphone
 
Schedule audio input during main mixer tick.
quit()
Stop all playback and terminate mixer
resample(smp, scale=1.0)
Resample a sound to be a different length
 
Sample must be mono.  May take some time for longer sounds
sampled at 44100 Hz.
 
Keyword arguments:
scale - scale factor for length of sound (2.0 means double length)
set_chunksize(size=1024)
Set the audio chunk size for each frame of audio output
 
This function is useful for setting the framerate when audio output
is synchronized with video.
start()
Start separate mixing thread
stereo_to_mono(left, right)
Return mono array from left and right sound stream arrays
tick(extra=None)
Main loop of mixer, mix and do audio IO
 
Audio sources are mixed by addition and then clipped.  Too many
loud sources will cause distortion.
 
extra is for extra sound data to mix into output
  must be in numpy array of correct length
uninterleave(data)
Given a stereo array, return separate left and right streams
 
This function converts one array representing interleaved left and
right audio streams into separate left and right arrays.  The return
value is a list of length two.  Input array and output arrays are all
Numpy arrays.
 
See also: interleave()

 
Data
        gchannels = 1
gchunksize = 1024
gid = 1
ginit = False
ginput_device_index = None
glock = <thread.lock object at 0xb7d540b0>
gmic = False
gmicdata = None
gmicstream = None
gmixer_srcs = []
goutput_device_index = None
gpyaudio = None
gsamplerate = 44100
gsamplewidth = 2
gstereo = True
gstream = None