# Program to load MIDI notes and a song from files and play it

def playMIDI(songFileName, MIDIfileName):

  # Step 1: First load and process the MIDI notes file

  MIDIfile = open(getMediaPath(MIDIfileName), 'r')

  MIDIdictionary = {}  # A Python dictionary (see Appendix A.11 in text book
                       # or search for information on the Web

  for line in MIDIfile.readlines():  # Loop over all lines in this file

    line = line.strip()  # Remove surounding spaces and new-lines
 
    lineList = line.split(',')  # Split at each comma in the line

    noteName = lineList[0]  # Extract the note name
    noteName = noteName.replace(' ', '')  # Remove all whitespaces

    MIDInumber = int(lineList[1])  # Get the MIDi note number

    print noteName, MIDInumber

    MIDIdictionary[noteName] = MIDInumber  # Insert into dictionary

    print MIDIdictionary

  MIDIfile.close()  # Close the file, end of reading it

  # Step 2: Load the song file and play it

  songFile = open(getMediaPath(songFileName), 'r')

  for line in songFile.readlines():  # Loop over all lines in this file

    line = line.strip()  # Remove surounding spaces and new-lines
 
    lineList = line.split(',')  # Split at each comma in the line

    noteName = lineList[0]  # Extract the note name
    noteName = noteName.replace(' ', '')  # Remove all whitespaces

    # Now get the MIDI note number from the dictionary
    MIDInumber = MIDIdictionary[noteName]

    noteDuration = int(lineList[1])  # Second column inf song file

    noteIntensity = int(lineList[2])  # Third column in song file

    print 'Playing:', noteName, MIDInumber, noteDuration, noteIntensity
    playNote(MIDInumber, noteDuration, noteIntensity)

  songFile.close()  # End reading the song file


def song(): # A short midi song
  playNote(60,200,127)
  playNote(65,500,127)
  playNote(64,800,127)
  playNote(60,600,127)
  for i in range (1,2): #loop twice
    playNote(64,120,127)
    playNote(65,120,127)
    playNote(67,60,127)


