# HG changeset patch # User drewp@bigasterisk.com # Date 2005-04-17 08:41:28 # Node ID 4a51d4eefa95fb7e9ba07a7c986baeeced0e4266 # Parent 13c089886f61a0b9b766885400cf0096e7dfb78c add wavecurve from semprini with new cmdline ui diff --git a/bin/wavecurve b/bin/wavecurve new file mode 100644 --- /dev/null +++ b/bin/wavecurve @@ -0,0 +1,17 @@ +#!/usr/bin/env python +import os, sys, optparse +import run_local +from light9.wavepoints import simp + +parser = optparse.OptionParser() +parser.add_option("-t",type="float",default=.01, + help="seconds per sample (default .01, .07 is smooth)") +options,args = parser.parse_args() + +inpath,outpath = args + +points = simp(inpath, seconds_per_average=options.t) + +f = file(outpath, 'w') +for time_val in points: + print >>f, "%s %s" % time_val diff --git a/light9/wavepoints.py b/light9/wavepoints.py new file mode 100644 --- /dev/null +++ b/light9/wavepoints.py @@ -0,0 +1,40 @@ +from __future__ import division +import wave, audioop + +def simp(filename, seconds_per_average=0.001): + """smaller seconds_per_average means fewer data points""" + wavefile = wave.open(filename, 'rb') + print "# gnuplot data for %s, seconds_per_average=%s" % \ + (filename, seconds_per_average) + print "# %d channels, samplewidth: %d, framerate: %s, frames: %d\n# Compression type: %s (%s)" % wavefile.getparams() + + framerate = wavefile.getframerate() # frames / second + frames_to_read = int(framerate * seconds_per_average) + print "# frames_to_read=%s" % frames_to_read + + time_and_max = [] + values = [] + count = 0 + while 1: + fragment = wavefile.readframes(frames_to_read) + if not fragment: + break + + # other possibilities: + # m = audioop.avg(fragment, 2) + # print count, "%s %s" % audioop.minmax(fragment, 2) + + m = audioop.rms(fragment, wavefile._framesize) + time_and_max.append((count, m)) + values.append(m) + count += frames_to_read + # if count>1000000: + # break + + # find the min and max + min_value, max_value = min(values), max(values) + points = [] # (secs,height) + for count, value in time_and_max: + points.append((count/framerate, + (value - min_value) / (max_value - min_value))) + return points