Source

sketchbook / siganal / main.py

Full commit
 #! /usr/bin/env python
#display
from __future__ import division
import pygame
import random
from pygame.time import get_ticks

#analysis
from scikits import audiolab
import scipy
  
 # Window dimensions
screen_width = 600
screen_height = 600
line_colour = 100,100,100

screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
zoom_factor = 10

class SignalVis(object):
    def __init__(self, filename, frame_interval, points):
        """
        filename - pretty obvious :) currently only working with wavs
        frame_interval - frame size for analysis in ms
        """
        self.data, self.fs, self.encoding = audiolab.wavread(filename)
        self.frame_bits = int(frame_interval/1000 * self.fs)
        self.points = points
        self.interval = int(round(self.frame_bits /self.points))

    def next(self):
        while len(self.data) > 0:
            X = scipy.fft(self.data[:self.frame_bits])
            Xdb = 20*scipy.log10(scipy.absolute(X))
            self.data = self.data[self.frame_bits:]
            yield [int(x) for i, x in enumerate(Xdb) if (i % self.interval == 0)]

def get_time_since(ticks):
    return get_ticks() - ticks

running = True
interval = 10
vis = SignalVis('louis.wav', interval, screen_width)
pygame.mixer.init()
pygame.mixer.music.load('postal.wav')
local_time = get_ticks()
pygame.mixer.music.play()

for line_data in vis.next():
    #blank screen
    screen.fill((0,0,0))

    for x in xrange(len(line_data)):
        screen.set_at((x, screen_height-(300 + int(round(line_data[x]*2)))), line_colour)
    
    while get_time_since(local_time) < interval:
        pass
    local_time = get_ticks()
    pygame.display.flip()