Saturday, January 28, 2017

JFugue Jython Example 13. Create a Listener to Find Out About Music

This is Example 13 from JFugue website (JFugue Examples) in Jython.



# JFugue 13. Create a Listener to Find Out About Music

'''
You can create a ParserListener to listen for any musical
event that any parser is parsing. Here, we'll create a
simple tool that counts how many "C" notes (of any octave)
are played in any song.
'''

import sys
sys.path.append("C:/jfugue-5.0.7.jar")

from java.io import File
from javax.sound.midi import MidiSystem
from org.jfugue.midi import MidiParser
from org.jfugue.parser import ParserListenerAdapter

class MyParserListener(ParserListenerAdapter):
    counter = 0
    def onNoteParsed(self,note):
        # A "C" note is in the 0th position of an octave
        if note.getPositionInOctave() == 0:
            self.counter+=1

parser = MidiParser()
listener = MyParserListener()
parser.addParserListener(listener)
parser.parse(MidiSystem.getSequence(File("RowYourBoat.mid")))
print "There are %d 'C' notes in this music." % listener.counter

No comments:

Post a Comment