Japanese

MDPW for Java

Install

Note

Sequence

Chord

Music piece

Visitor


Download
mdpw30.zip

Back

Install

Install Jython(Python) if you have not installed yet. Download mdpw30.zip and extract it. You will find mdpw.jar. Add the jar file to your Java development environment. You will also find the "mdpw" folder which contains the jython library for the mdpw. A sample batch file describes below. You may chage the first line corresponding to your jython installation.
set PYTHONHOME="c:\java\jython2.2.1"
@echo off

set ARGS=

:loop
if [%1] == [] goto end
    set ARGS=%ARGS% %1
    shift
    goto loop
:end

java.exe -Dpython.home=%PYTHONHOME% -classpath "%PYTHONHOME%\jython.jar;..\mdpw.jar;%CLASSPATH%" org.python.util.jython %ARGS%

Top of this page| Next step

How to make a Note

from mdpw import * 

nC1 = mn(64,1,64,1)

p = midifile_player()
p.init("note.mid")
p.setTempo(120)
p.play(nC1)
p.end()
The function to make a note is "mn". You can specify the pitch (1st argument), duration (2nd), velocity (3rd) and program number. The object to play is "midifile_player". It has methods init, setTempo, play and end.
Top of this page| Next step

How to make a Sequence

from mdpw import * 
  
C = 64
D = 66
E = 68
v = 64
nC1 = mn(C,1,v,1)
nD1 = mn(D,1,v,1)
nE1 = mn(E,1,v,1)

seq = ms([nC1, nD1, nE1])
p = midifile_player()
p.init("seq.mid")
p.setTempo(120)
p.play(seq)
p.end()
The function to make a sequence of notes is "ms". It returns sequnce object which inherits the "list", so you can manipulate the elements in the sequence with the same way as the list. The sequence can contains notes sequences and chords. You can use "midifile_player" to play it.
Top of this page| Next step

How to make a Chord

from mdpw import * 
  
C = 64
E = 68
G = 71
v = 64
nC1 = mn(C,1,v,1)
nE1 = mn(E,1,v,1)
nG1 = mn(G,1,v,1)

chord = mc([nC1, nE1, nG1])
p = midifile_player()
p.init("chord.mid")
p.setTempo(120)
p.play(chord)
p.end()

Top of this page| Next step

How to make a musical piece

You can make a musical piece by sequences and chords. Both can contain both. You can make a seqence as the melody, a sequence of chords as the accompaniment and a chord of those two.
from mdpw import * 
  
C = 64
D = 66
E = 68
G = 71

sv = 96
cv = 64
nC1 = mn(C,1,sv,1)
nD1 = mn(D,1,sv,1)
nE1 = mn(E,1,sv,1)
nR1 = mn(E,1,0,1)
nC2 = mn(C-12,2,cv,2)
nE2 = mn(E-12,2,cv,2)
nG2 = mn(G-12,2,cv,2)

seq = ms([nC1, nD1, nE1, nR1])
chord = mc([nC2, nE2, nG2])

piece = mc([ms([seq,seq]),ms([chord, chord,chord,chord])])
p = midifile_player()
p.init("piece.mid")
p.setTempo(120)
p.play(piece)
p.end()

Top of this page| Next step

Visitor

This sample implements the "Crescendo" visitor. You can implement how to proccess the notes at the method "proccess". The first argument is the note to be proccessed and the second argument is the timing the note in the sequence.
from mdpw import *


class crescendo(visitor):
  def __init__(self, d, sv, ev):
   self.setVelocities(sv, ev)
   self.setDuration(d)
  def setDuration(self, d):
    self.duration = d
  def setVelocities(self, sv, ev):
    self.startv = sv
    self.endv = ev
  def process(self, n, d):
    n.setVelocity((self.endv-self.startv)*d/self.duration+self.startv)
  
    
C = 64
D = 66
E = 68
v = 64
nC1 = mn(C,1,v,1)
nD1 = mn(D,1,v,1)
nE1 = mn(E,1,v,1)
seq = ms([nC1, nD1, nE1])
vi = crescendo(seq.getDuration(), 32, 115)
p = midifile_player()
p.init("seq.mid")
p.setTempo(120)
p.play(seq.visit(vi))
p.end()
Are you inspired? Enjoy Music Programing.
Top of this page