From c9d5ae019dd135b1e13dff8700bd00e871474785 Mon Sep 17 00:00:00 2001 From: Zach Denton Date: Sat, 23 Apr 2011 18:11:21 -0400 Subject: [PATCH] renamed to `wavebender` --- binaural.py | 2 +- damped.py | 5 ++--- focus3.py | 2 +- sbagen.py | 9 ++++----- square.py | 2 +- violin.py | 16 ++++++++-------- wavegen.py => wavebender.py | 6 +++--- whitenoise.py | 8 ++++++++ 8 files changed, 28 insertions(+), 22 deletions(-) rename wavegen.py => wavebender.py (93%) create mode 100755 whitenoise.py diff --git a/binaural.py b/binaural.py index c59a780..2608e81 100755 --- a/binaural.py +++ b/binaural.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from wavegen import * +from wavebender import * import sys channels = ((sine_wave(170.0, amplitude=0.1),), diff --git a/damped.py b/damped.py index 6d3b6d8..f8a9531 100755 --- a/damped.py +++ b/damped.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from wavegen import * +from wavebender import * from itertools import * import sys @@ -10,7 +10,6 @@ def ncycles(iterable, n): def waves(): l = int(44100*0.4) # each note lasts 0.4 seconds - # ACG ACG ACG ACG DCD DCD DCD DCD ... return cycle(chain(ncycles(chain(islice(damped_wave(frequency=440.0, amplitude=0.1, length=int(l/4)), l), islice(damped_wave(frequency=261.63, amplitude=0.1, length=int(l/4)), l), islice(damped_wave(frequency=329.63, amplitude=0.1, length=int(l/4)), l)), 3), @@ -24,7 +23,7 @@ def waves(): islice(damped_wave(frequency=293.66, amplitude=0.1, length=int(l/4)), l)), islice(damped_wave(frequency=261.63, amplitude=0.1, length=3*l), 3*l))) -channels = ((waves(),), (waves(), white_noise(amplitude=0.01),)) +channels = ((waves(),), (waves(), white_noise(amplitude=0.001),)) samples = compute_samples(channels, None) write_wavefile(sys.stdout, samples, None) diff --git a/focus3.py b/focus3.py index 9cee48a..b95e202 100755 --- a/focus3.py +++ b/focus3.py @@ -10,7 +10,7 @@ +++ 100+0/10 302+4/10 500+0/10 ''' -from wavegen import * +from wavebender import * from itertools import * import sys diff --git a/sbagen.py b/sbagen.py index 0db3000..ae42de3 100755 --- a/sbagen.py +++ b/sbagen.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import re import sys -from wavegen import * +from wavebender import * from itertools import * def sbagen_phrase(phrase): @@ -35,11 +35,10 @@ def sequencer(*seqs): ''' return chain(*(islice(generator, duration) for generator, duration in seqs)) -try: +if sys.argv[1:]: channels = sbagen_line(' '.join(sys.argv[1:])) -except IndexError: - print >> sys.stderr, "You must enter an sbagen script." +else: sys.exit(1) samples = compute_samples(channels) -write_pcm(sys.stdout, samples) +write_wavefile(sys.stdout, samples) diff --git a/square.py b/square.py index a11cc86..8efb60c 100755 --- a/square.py +++ b/square.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from wavegen import * +from wavebender import * from math import cos import sys diff --git a/violin.py b/violin.py index 70be8ae..3bdbb89 100755 --- a/violin.py +++ b/violin.py @@ -1,16 +1,16 @@ #!/usr/bin/env python -from wavegen import * +from wavebender import * import sys def violin(amplitude=0.1): # simulates a violin playing G. - return (sine_wave(400.0, amplitude=0.76*amplitude), - sine_wave(800.0, amplitude=0.44*amplitude), - sine_wave(1200.0, amplitude=0.32*amplitude), - sine_wave(3400.0, amplitude=0.16*amplitude), - sine_wave(600.0, amplitude=1.0*amplitude), - sine_wave(1000.0, amplitude=0.44*amplitude), - sine_wave(1600.0, amplitude=0.32*amplitude)) + return (damped_wave(400.0, amplitude=0.76*amplitude, length=44100 * 5), + damped_wave(800.0, amplitude=0.44*amplitude, length=44100 * 5), + damped_wave(1200.0, amplitude=0.32*amplitude, length=44100 * 5), + damped_wave(3400.0, amplitude=0.16*amplitude, length=44100 * 5), + damped_wave(600.0, amplitude=1.0*amplitude, length=44100 * 5), + damped_wave(1000.0, amplitude=0.44*amplitude, length=44100 * 5), + damped_wave(1600.0, amplitude=0.32*amplitude, length=44100 * 5)) channels = (violin(),) samples = compute_samples(channels, 44100 * 60 * 1) diff --git a/wavegen.py b/wavebender.py similarity index 93% rename from wavegen.py rename to wavebender.py index 8513b56..840efc2 100755 --- a/wavegen.py +++ b/wavebender.py @@ -19,7 +19,7 @@ def sine_wave(frequency=440.0, framerate=44100, amplitude=0.5): period = int(framerate / frequency) if amplitude > 1.0: amplitude = 1.0 if amplitude < 0.0: amplitude = 0.0 - lookup_table = [float(amplitude) * math.sin(2.0*math.pi*float(frequency)*(float(i%period)/float(framerate))) for i in range(period)] + lookup_table = [float(amplitude) * math.sin(2.0*math.pi*float(frequency)*(float(i%period)/float(framerate))) for i in xrange(period)] return (lookup_table[i%period] for i in count(0)) def square_wave(frequency=440.0, framerate=44100, amplitude=0.5): @@ -34,7 +34,7 @@ def square_wave(frequency=440.0, framerate=44100, amplitude=0.5): def damped_wave(frequency=440.0, framerate=44100, amplitude=0.5, length=44100): if amplitude > 1.0: amplitude = 1.0 if amplitude < 0.0: amplitude = 0.0 - return (float(amplitude) * math.exp(-(float(i%length)/float(framerate))) * math.sin(2.0*math.pi*float(frequency)*(float(i)/float(framerate))) for i in count(0)) + return (math.exp(-(float(i%length)/float(framerate))) * s for i, s in enumerate(sine_wave(frequency, framerate, amplitude))) def white_noise(amplitude=0.5): ''' @@ -95,7 +95,7 @@ def main(): args = parser.parse_args() # each channel is defined by infinite functions which are added to produce a sample. - channels = ((sine_wave(args.frequency, args.rate, args.amplitude), white_noise(0.1)) for i in range(args.channels)) + channels = ((sine_wave(args.frequency, args.rate, args.amplitude),) for i in range(args.channels)) # convert the channel functions into waveforms samples = compute_samples(channels, args.rate * args.time) diff --git a/whitenoise.py b/whitenoise.py new file mode 100755 index 0000000..0fdc255 --- /dev/null +++ b/whitenoise.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python +from wavebender import * +import sys + +channels = ((white_noise(amplitude=0.1),),) + +samples = compute_samples(channels, 44100 * 60 * 1) +write_wavefile(sys.stdout, samples, 44100 * 60 * 1, nchannels=1)