JMSL Tutorial: MusicJob

A MusicJob has an Instrument. An Instrument's play() method interprets data. So a MusicJob could create double[] data on the fly and hand it to an Instrument to play MIDI, make JSyn sounds, or some other, non-musical interpretation like drawing or running some hardware.

Here we define a simple Instrument that prints out the data it receives. It interprets data[0] as duration, and scales it with a timeStretch value. The essential code in this Instrument is shown below.

    public double play(double playTime, double timeStretch, double[] data) {
        double duration = data[0];
        JMSL.out.println("CustomPrintingInstrument.play(), playTime=" + playTime);
        JMSL.out.println("Data:");
        for (int i = 0; i < data.length; i++) {
            JMSL.out.println(i + ", " + data[i]);
        }
        JMSL.out.println("");
        return playTime + duration * timeStretch;
    }


Every time the MusicJob repeats, it will generate new data. It will hand it to its Instrument's play() method which will return an updated playTime. The essential code in this MusicJob is shown below.

    public double repeat(double playTime) {
        double[] data = new double[3];
        data[0] = 2.0; // duration
        data[1] = JMSLRandom.choose(); // something else
        data[2] = JMSLRandom.gauss(5, 3); // something else again
        return getInstrument().play(playTime, 1, data);
    }

Note that the duration in data[0] is set to a constant 2.0 seconds. You can scale the playback of that up or down by setting the MusicJob's timeStretch to a value other than the default 1. For example, setTimeStretch(0.5) would multiply the duration of 2 by timeStretch 0.5 and you'd experience a pause time of 1.0 second instead of 2 seconds.

Check out complete Source Code for the example in the video show below.


To recap: We have seen that a MusicJob has an Instrument that can be defined and customized by the programmer. We see that a MusicJob can generate arbitrary data on the fly. It can use this Instrument to interpret the data it created. It can use the playTime returned by Instrument.play() to delay its repeat() cycle.

Before proceeding, watch this video explaining how timeStretch works!
  (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved
  JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.