JMSL Tutorial: JSyn and JMSL, Sample Playing Instruments

JMSL's SamplePlayingInstrument provides audio sample playback. You can assign audio samples to pitch values and play them back with a MusicShape for example. Unassigned pitches will transpose nearest samples.

If markers are located in the audio file as is shown below, the instrument will loop between the markers to sustain the sample if necessary.
sound file with markers for looping

It is straightforward to assign samples to pitches, as the fully functioning class definition below demonstrates:

package jmslexamples.jsyn2;

import java.io.File;

import com.softsynth.jmsl.*;
import com.softsynth.jmsl.jsyn2.SamplePlayingInstrument;
import com.softsynth.jmsl.view.MusicShapeEditor;

public class SamplePlayingMusicShape {

	public static void main(String[] args) {

		final SamplePlayingInstrument ins = new SamplePlayingInstrument();
		ins.setDirectory(new File("D:/documents/JMSLScoreWork/JMSLScoreSamples")); // CHANGEME
		System.out.println("DIRECTORY: " + ins.getDirectory().getAbsolutePath());
		// ins.getMusicDevice().edit(f);
		ins.getMusicDevice().open();

		ins.addSamplePitch("kawai_k1m/guitar/gtr61.wav", 61);
		ins.addSamplePitch("kawai_k1m/guitar/gtr64.wav", 64);
		ins.addSamplePitch("kawai_k1m/guitar/gtr67.wav", 67);
		ins.addSamplePitch("kawai_k1m/guitar/gtr70.wav", 70);

		ins.buildFromAttributes();

		JMSLMixerContainer mixer = new JMSLMixerContainer();
		mixer.addInstrument(ins);
		mixer.start();

		JMSL.clock.setAdvance(0.25);
		final MusicShape s1 = new MusicShape(4);
		s1.useStandardDimensionNameSpace();
		s1.add(0.25, 59, 0.5, 0.25);
		s1.add(0.25, 59.5, 0.5, 0.25);
		s1.add(0.5, 60, 0.5, 0.25);
		s1.add(0.5, 65, 0.5, 0.25);
		s1.add(0.75, 70, 0.5, 0.25);

		s1.setInstrument(ins);
		s1.setRepeats(Integer.MAX_VALUE);

		MusicShapeEditor se = new MusicShapeEditor();
		se.addMusicShape(s1);

		java.awt.Frame f = new java.awt.Frame("JSyn2 SamplePlayingInstrument");
		f.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				JMSL.closeMusicDevices();
				System.exit(0);
			}
		});
		f.add(se.getComponent());
		// f.setSize(300, 200);
		f.pack();
		f.setVisible(true);
		

		s1.launch(JMSL.now());


	}
}


  (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved
  JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.