swmixer
index
/home/nwhitehe/Projects/SoundTools/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
       
numpy
pyaudio
thread
time
wave

 
Classes
       
Channel
Sound

 
class Channel
    Represents one sound source currently playing
 
  Methods defined here:
__init__(self, data, pos, env, loops)
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 or explicit sample data
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

 
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
 
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=22050, chunksize=1024, stereo=True, microphone=False)
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
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)
start()
Start separate mixing thread
tick()

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