#!/usr/bin/env python
# encoding: utf-8
"""
untitled.py

Created by Alan Fineberg on 2009-05-02.
Copyright (c) 2009 __MyCompanyName__. All rights reserved.
"""

import sys
import os
from expandinglist import ExpandingList
from note import Note

class SimpleParser(object):
    def __init__(self, score, synth):
        self.synth = synth
        self.score = score
        self.total_duration = 0
#        self.notes
    
        self.duration_dict = {64: 6, 32: 12, 16: 24, 12: 32, 8: 48, 4: 96, 2: 192, 1: 384}
        
    def onPulse(self, duration):     
        if self.total_duration % 384 == 0:
            self.current_slice = self.score.score_slice.next()
        
            self.notes = ExpandingList(object=list)
            
            for measure in self.current_slice:
                segment = 0
                for note in measure:
                    print note
#                    print note.tone
                    # notes is a list of lists   [ [note, note, note], [note], [], []]
                    self.notes[segment].append(note['tone'])             
                    segment += self.duration_dict[note['duration']]
            
        i = self.total_duration % 192
        self.total_duration += duration
        
        self.__play__(i)
        
        
        
    def __play__(self, index):
        for note in self.notes[index]:
            self.noteOn(note)

    def noteOn(self, note):
        self.synth.noteon(0, note, 127)
        
if __name__ == '__main__':
	import fluidsynth
	synth = fluidsynth.Synth()
	synth.start()
	id = synth.sfload("HS_R8_Drums.sf2")
	synth.program_select(0, id, 0, 0)
	synth.sfont_select(0, id)
	
	class Score(object):
	    def __init__(self, score_slice):
	        self.score_slice = score_slice
	
	class ScoreSlice(object):
	    def __init__(self):
	        pass
	        
	    def next(self):
	        return [[{'tone': 35, 'duration': 2}, {'tone': 35, 'duration': 2}], # Kick
	                [{'tone': 0, 'duration': 16},{'tone': 50, 'duration': 16}, {'tone': 50, 'duration': 8}, {'tone': 43, 'duration': 8}, {'tone': 50, 'duration': 16},], # Tom
                    # Snare
	                [{'tone': 0, 'duration': 4}, {'tone': 38, 'duration': 4}, {'tone': 0, 'duration': 4}, \
	                        {'tone': 38, 'duration': 8}, {'tone': 0, 'duration': 16}, \
	                        {'tone': 38, 'duration': 16}, {'tone': 38, 'duration': 16}],
                    # Hi-Hat
	                [{'tone': 42, 'duration': 8}, {'tone': 42, 'duration': 8}, \
	                        {'tone': 42, 'duration': 8}, {'tone': 42, 'duration': 8}, \
	                        {'tone': 42, 'duration': 8}, {'tone': 42, 'duration': 8}, \
	                        {'tone': 42, 'duration': 8}, {'tone': 42, 'duration': 8}],
	                [{'tone': 42, 'duration': 12}, {'tone': 42, 'duration': 12}, \
        	                {'tone': 42, 'duration': 12}, {'tone': 42, 'duration': 4}, \
        	                {'tone': 41, 'duration': 4}, {'tone': 42, 'duration': 8}, \
        	                {'tone': 42, 'duration': 12}, {'tone': 41, 'duration': 12}],
        	        [{'tone': 38, 'duration': 64}, {'tone': 0, 'duration': 32}, {'tone': 0, 'duration': 32}]*32]
	        
	from time import time
	from time import sleep
	
	parser = SimpleParser(Score(ScoreSlice()), synth)
	
	# Simulates a metronome.
	for i in xrange(8000):
	    parser.onPulse(1)
	    sleep(.0050)
	
	
	


