#!/usr/bin/env python # # Copyright (c) 2012, Matthias P. Braendli # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THIS SOFTWARE. from math import sqrt import wave import sys import struct import subprocess import traceback DEBUG = False class RunningStatistics(): """Taken from http://www.johndcook.com/standard_deviation.html""" def __init__(self): self.m_n = 0 self.m_oldM = 0.0 self.m_newM = 0.0 self.m_oldS = 0.0 self.m_newS = 0.0 def push(self, x): self.m_n += 1 # See Knuth TAOCP vol 2, 3rd edition, page 232 if self.m_n == 1: self.m_oldM = x self.m_newM = x self.m_oldS = 0.0 else: self.m_newM = self.m_oldM + (x - self.m_oldM) / self.m_n self.m_newS = self.m_oldS + (x - self.m_oldM) * (x - self.m_newM) self.m_oldM = self.m_newM self.m_oldS = self.m_newS def mean(self): return self.m_newM def variance(self): if self.m_n > 1: return self.m_newS/(self.m_n - 1) else: return 0.0 def standard_deviation(self): return sqrt(self.variance()) def run(rs): try: w = wave.open(proc.stdout) max_samps = min(w.getframerate() * MEASURE_TIME * w.getnchannels(), w.getnframes()) if DEBUG: print("Number of frames: {0}".format(w.getnframes())) print("Sample width: {0}".format(w.getsampwidth())) print("Frame rate: {0}".format(w.getframerate())) print("Channels: {0}".format(w.getnchannels())) print("Max samps: {0}".format(max_samps)) if w.getnchannels() == 1: if w.getsampwidth() == 2: STR = "