tonelib
index
/home/zelle/Dropbox/classes/cs220/projects/audio/songplayer/tonelib.py

Simple tone generation library for creating, playing, and saving audio tones.
 
A tone is represented as a list of "samples" of the waveform where
each sample is a floating point number in the range -1 to 1
inclusive. Sampling is done at the standard CD rate of 44100 hz.
 
The generate_tone function produces a list of samples representing
a tone of a given frequency, duration, and amplitude produced from
a standard wave function.
 
Filter functions modify a list of samples, for example, by making a
tone "fade-out" or adjusting the overall volume.
 
write_wavefile writes a list of samples to an uncompressed wav file.
 
play_sound is a simple function for playing tones. Intended for
testing purposes only.

 
Modules
       
math
os
random
struct
subprocess
sys
wave

 
Functions
       
adjvolume(samples, factor=0.75)
Adjust the amplitude of entire sample uniformly.
 
pre: factor > 0
post: Every sample in samples has been multiplied by factor.
 
note: factor > 1.0 amplifies while factor < 1.0 decreases volume.
fadeout(samples, decaytime=0.5, SAMPLERATE=44100)
Exponential taper of signal amplitude
 
pre: decaytime > 0
 
post: Values in samples have been decreased with increasing
      damping from the begining to end. The rate of damping is
      determined by decaytime, which is the half-life of the
      amplitude.  So the sample at decaytime is reduced by .5, the
      sample at 2*decaytime is reduced by .25, etc.
playsound = _play_sound_linux(samples)
Play sound on Linux system
 
pre: samples is a list representing a valid sound sample.
post: The sound has been piped to an external process for playback.
 
Note: This should work on any Linux system using ALSA.
sawtoothwave(t)
Standard periodic sawtooth wave generator.
 
pre: t >= 0
post: returns amplitude of standard sawtooth wave at time t.
      (0.0 at t=0, rises to 1 near t=pi, 
       -1.0 at t=pi, rises to 0.0 at t=tau)
sinewave(t)
standard periodic sine wave generator
squarewave(t)
Standard periodic square wave generator.
 
pre: t >= 0
post: returns amplitude of standard square wave at time t.
      (1.0 for 0 <= t < pi and -1.0 for pi <= t < 2*pi)
test()
tone(wavefn=<function sinewave at 0x7fa34f5cbb80>, freq=440, duration=1, amp=1)
Create a tone with given characteristics
 
params: wavefn is a standard wave function, frequency is in hz,
        duration is in seconds, amplitude is a float in range
        0..1.
 
returns a list of floats representing sequential samples of
          a tone with the given waveform, frequency, duration,
          and amplitude.
trianglewave(t)
Standard periodic triangle wave generator.
 
pre: t >= 0
post: returns amplitude of standard triangle wave at time t.
      (0.0 at t=0, 1.0 at t=pi/2, 0.0 at t=pi, -1.0 at t=1.5*pi)
whitenoise(t)
White noise "wave" generator
 
post: returns random value in range -1 to 1
write_wavefile(samples, fname, SAMPLERATE=44100)
Write sampled wave to a wav format sound file
 
pre: samples is a list representing a valid sound sample.
post: The sound information has been written to the file fname in
      wav audio file format.
 
Note: This function wipes out any previous contents of file, fname.

 
Data
        SAMPLERATE = 44100