Sunday, January 29, 2017

JFugue Jython Example 16. Use "Replacement Maps" to Create Carnatic Music

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



# JFugue 16. Use "Replacement Maps" to Create Carnatic Music

'''
JFugue's ReplacementMap capability lets you use your own symbols
in a music string. JFugue comes with a CarnaticReplacementMap
that maps Carnatic notes to microtone frequencies.
'''

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

from org.jfugue.player import Player
from org.staccato.maps import CarnaticReplacementMap
from org.staccato import ReplacementMapPreprocessor

rmp = ReplacementMapPreprocessor.getInstance()
rmp.setReplacementMap(CarnaticReplacementMap())

player = Player()
player.play("<S> <R1> <R2> <R3> <R4>")

JFugue Jython Example 15. Anticipate Musical Events Before They Occur

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



# JFugue 15. Anticipate Musical Events Before They Occur

'''
You might imagine creating new types of ParserListeners,
like an AnimationParserListener, that depend on knowing
about the musical events before they happen. For example,
perhaps your animation is of a robot playing a drum or
strumming a guitar. Before the note makes a sound, the
animation needs to get its virtual hands in the right
place, so you might want a notice 500ms earlier that
a musical event is about to happen. To bend time with
JFugue, use a combination of the TemporalPLP class
and Player.delayPlay(). delayPlay() creates a new
thread that first waits the specified amount of
time before playing. If you do this, make sure to
call delayPlay() before plp.parse().
'''

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

from org.jfugue.player import Player
from org.jfugue.temporal import TemporalPLP
from org.staccato import StaccatoParser
from org.jfugue.devtools import DiagnosticParserListener

MUSIC = "C D E F G A B"
TEMPORAL_DELAY = 500

# Part 1. Parse the original music
parser = StaccatoParser()
plp = TemporalPLP()
parser.addParserListener(plp)
parser.parse(MUSIC)

# Part 2. Send the events from Part 1,
# and play the original music with a delay
dpl = DiagnosticParserListener()
# Or your AnimationParserListener!
plp.addParserListener(dpl)
Player().delayPlay(TEMPORAL_DELAY, MUSIC)
plp.parse()

Saturday, January 28, 2017

JFugue Jython Example 14. Create interactive musical programs using the RealtimePlayer.

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



# JFugue 14. Play Music in Realtime

'''
Create interactive musical programs using the RealtimePlayer.
'''

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

import random
from org.jfugue.pattern import Pattern
from org.jfugue.realtime import RealtimePlayer
from org.jfugue.theory import Note

player = RealtimePlayer()
quit = False

s = [None]*3
s[0]="Enter a '+C' to start a note,"
s[1]="'-C' to stop a note, 'i' for a random instrument,"
s[2]="'p' for a pattern, or 'q' to quit\n"
info = " ".join(s)

PATTERNS = [Pattern("Cmajq Dmajq Emajq"),
            Pattern("V0 Ei Gi Di Ci  V1 Gi Ci Fi Ei"),
            Pattern("V0 Cmajq V1 Gmajq")]

while quit == False:
    entry = raw_input(info)
    if entry.startswith("+"):
        player.startNote(Note(entry[1:]))
    elif entry.startswith("-"):
        player.stopNote(Note(entry[1:]))
    elif entry.lower()=="i":
        player.changeInstrument(random.randint(0,127))
    elif entry.lower()=="p":
        player.play(random.choice(PATTERNS))
    elif entry.lower()=="q":
        quit = True
        player.close()

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

Friday, January 27, 2017

JFugue Jython Example 12. Connecting Any Parser to Any ParserListener

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



# JFugue 12. Connecting Any Parser to Any ParserListener

'''
You can use JFugue to convert between music formats.
Most commonly, JFugue is used to turn Staccato music
into MIDI sound. Alternatively, you can play with the
MIDI, MusicXML, and LilyPond parsers and listeners.
Or, you can easily create your own parser or listener,
and it will instantly interoperate with the other
existing formats. (And if you convert to Staccato,
you can then play the Staccato music... and edit it!)
'''

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.player import Player
from org.staccato import StaccatoParserListener

parser = MidiParser()
listener = StaccatoParserListener()
parser.addParserListener(listener)
parser.parse(MidiSystem.getSequence(File("RowYourBoat.mid")))
staccatoPattern = listener.getPattern()

print staccatoPattern

player = Player()
player.play(staccatoPattern)

JFugue Jython Example 11. See the Contents of a MIDI File in Human-Readable and Machine-Parseable Staccato Format

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



# JFugue 11. See the Contents of a MIDI File in Human-Readable
# and Machine-Parseable Staccato Format

'''
Want to see the music in your MIDI file? Of course,
you could load it in a sheet music tool. Here's how you can load it
with JFugue. You'll get a Pattern of your music, which you can then
pick apart in interesting ways (for example, count how many "C" notes
there are... that's coming up in a few examples)
'''

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

from java.io import File, FileNotFoundException
from org.jfugue.midi import MidiFileManager


try:
    pattern = MidiFileManager.loadPatternFromMidi(File("RowYourBoat.mid"))
except FileNotFoundException:
    print "file not found"
    pattern = None

pat = pattern.toString().split(" ")
for p in pat: print p

Thursday, January 26, 2017

JFugue Jython Example 10. All That, in One Line of Code?

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



# JFugue 10. All That, in One Line of Code?

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

'''
Try this. The main line of code even fits within
the 140-character limit of a tweet.
'''

from org.jfugue.player import Player
from org.jfugue.rhythm import Rhythm
from org.jfugue.theory import ChordProgression

cp = ChordProgression("I IV vi V").eachChordAs("$_i $_i Ri $_i")
r = Rhythm().addLayer("..X...X...X...XO")
Player().play(cp,r)