#!/usr/bin/env python #Author: Shriphani Palakodety #Tool to aid those with noise cancellation headphones import pyaudio import wave import sys import struct import numpy import time skype_on_call = False f = open("skype_Status", "r") for new_line in f: if new_line == "PROGRESS": skype_on_call = True def record(): '''Records Input From Microphone Using PyAudio''' duration = 1 #record for 1 second. Pretty long duration don't you think outfile = "analysis.wav" p = pyaudio.PyAudio() inStream = p.open(format=pyaudio.paInt16, channels=1, rate=44100,input=True, frames_per_buffer=1024) out = [] upper_lim = 44100 / 1024 * duration #upper limit of the range we record to. 44100 / 1024 sized chunk * 5 seconds for i in xrange(0, upper_lim): data = inStream.read(1024) out.append(data) #now the writing section where we write to file data = ''.join(out) outFile = wave.open(outfile, "wb") outFile.setnchannels(1) outFile.setsampwidth(p.get_sample_size(pyaudio.paInt16)) outFile.setframerate(44100) outFile.writeframes(data) outFile.close() analyze() def analyze(): if skype_on_call: print "\n" print "Skype Call In Progress" print "Listener On Hold" return inFile = wave.open("analysis.wav", "rb") #open a wav file in read mode sample_rate = inFile.getframerate() total_samples = inFile.getnframes() fftLength = 128 fft_num = (total_samples/fftLength) -2 vals = inFile.readframes(2000) results = struct.unpack("%dh"%(2000), vals) results = [x**2 for x in results] intensity = 20 * numpy.log10(numpy.sqrt(sum(results)/2000)) if (intensity > 47): print "\n\n" print "Someone might be calling you" curtime = time.localtime() print "Current Time: %d:%d:%d"%(curtime[3], curtime[4], curtime[5]) print "Intensity: "+str(intensity) + " dB" else: print "\n\n\n\n" print "No disturbance in the background" inFile.close() if skype_on_call: analyze() else: record()