def callback(in_data, frame_count, time_info, status): nextTime = time.time() newSamples = np.fromstring(in_data,dtype=np.int16) sig.Y = np.append(sig.Y,zip(newSamples[::2],newSamples[1::2]),0) sig.A = signal.lift((sig.Y[:,0] + sig.Y[:,1]) / 2, True) startTime = nextTime return (in_data,pyaudio.paContinue)
def beats(s): """ Extract beats in the signal in 4 different frequency ranges """ # quick note: s.avg4 is a decaying 4 channel fft # s.longavg4 decays at a slower rate # beat detection huristic: # beat occured if s.avg4 * threshold > s.longavg4 threshold = 1.7 return util.numpymap( lambda (x, y): 1 if x > threshold * y else 0, zip(s.avg4 * threshold, s.longavg4)) # Lift the beats sig.beats = signal.lift(beats) # not sure if this can be called sustain. # blend gives a decay effect sig.sustain = signalutil.blend(beats, 0.7) def graphsProcess(s): # clear screen surface.fill((0, 0, 0)) # draw a decaying fft differential and the beats in the full # pygame window. graphsGraphs([ barGraph(s.avg12rel / 10), boopGraph(s.beats), boopGraph(s.sustain) ])(surface, (0, 0) + SCREEN_DIMENSIONS) # affect the window
if len(sys.argv) < 2: print "Usage: %s file.mp3" % sys.argv[0] sys.exit(1) else: fPath = sys.argv[1] # initialize PyGame SCREEN_DIMENSIONS = (640, 480) pygame.init() surface = display.set_mode(SCREEN_DIMENSIONS) sF, data = audio.read(fPath) sig = signal.Signal(data, sF) sig.A = signal.lift((data[:,0] + data[:,1]) / 2, True) def beats(s): """ Extract beats in the signal in 4 different frequency ranges """ # quick note: s.avg4 is a decaying 4 channel fft # s.longavg4 decays at a slower rate # beat detection huristic: # beat occured if s.avg4 * threshold > s.longavg4 threshold = 1.7 return util.numpymap( lambda (x, y): 1 if x > threshold * y else 0, zip(s.avg4 * threshold, s.longavg4))