def startup(args): global outfile global on, off, dp global start, stop global processor from functools import partial from onyx.audio.liveaudio import inputs, default_input, LiveAudioSource from onyx.signalprocessing.htkmfcc import make_fft_abs_processor from onyx.util.debugprint import DebugPrint, dcheck, dprint #from onyx.util.streamprocess import ChainProcessor if '-o' in args: outfile = open(args[args.index('-o')+1], 'wb', 0) else: outfile = sys.stdout if '-l' in args: logfile = open(args[args.index('-l')+1], 'wb', 0) else: logfile = sys.stdout ## if '-m' in args: ## mic_name = args[args.index('-m')+1] ## else: ## mic_name = 'blue' debug = DebugPrint(logfile, 'saddemo') ## on = doer(debug.on) ## off = doer(debug.off) dp = partial(dprint, 'saddemo', DebugPrint.NO_PREFIX, ' ') plot_points = 255 line = [' '] * plot_points ## # select a live input ## mic_id = default_input() ## for id, chan, name, manu in inputs(): ## if name.lower().find(mic_name.lower()) != -1: ## mic_id = id ## break ## mic = LiveAudioSource(mic_id, verbose=False) ## start = doer(mic.start) ## stop = doer(mic.stop) return # use the module's mic mic = audio.mic # XXX LiveAudioSource (and thus audiomodule.cpp) need to be able to provide # the sample frequency.... sample_nsec = 22676 # 44100 Hz #frame_nsec = 10000000 # 10 msec #window_nsec = 25600000 # 25.6 msec frame_nsec = 8000000 # 8 msec window_nsec = 24000000 # 24 msec dither = 1 / (1 << 10) preemcoef = 0.96875 samples_per_sec, fft_size, fftmag = make_fft_abs_processor(sample_nsec, frame_nsec, window_nsec, dither=dither, preemcoef=preemcoef, zmeansource=True, usehamming=True, max_channels=2) print 'samples_per_sec', samples_per_sec, ' fft_size', fft_size # figure out range of fft bins to work with lowfreq = 300 highfreq = 4000 import math low_index = int(math.ceil(lowfreq * fft_size / samples_per_sec)) high_index = int(math.floor(highfreq * fft_size / samples_per_sec)) # the bin selector select = slice(low_index, high_index) def display(data): band = data[select] band *= band sum = float(band.sum()) dB = int(dB_scale * (10 * math.log10(sum) + dB_offset)) dp('%11.6f ' % sum, '%3d' % dB) dB = max(1, dB) #outfile.write('\n' + ' ' * dB + '|') outfile.write( pen * dB + 'O' + '\n') mic.set_sendee(fftmag.process) fftmag.set_sendee(display)
audio = audio() # default input audio.mic = None # XXX LiveAudioSource (and thus audiomodule.cpp) need to be able to provide # the sample frequency.... sample_nsec = 22676 # 44100 Hz #frame_nsec = 10000000 # 10 msec #window_nsec = 25600000 # 25.6 msec frame_nsec = 8000000 # 8 msec window_nsec = 24000000 # 24 msec dither = 1 / (1 << 10) preemcoef = 0.96875 samples_per_sec, fft_size, fftmag = make_fft_abs_processor(sample_nsec, frame_nsec, window_nsec, dither=dither, preemcoef=preemcoef, zmeansource=True, usehamming=True, max_channels=2) class DpToggle(object): """ An object with two attributes, on and off, that toggle debug printing of the DebugPrint object created from the constructor args. """ # XXX DebugPrint needs work to develop semantics to support multiple # instances of DpToggle.... def __init__(self, *args): self.dp_control = DebugPrint(*args) self._on = False def __del__(self): self.off @property