<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_by_API%2Fjavax.sound.midi%2FMidiSystem</id>
		<title>Java by API/javax.sound.midi/MidiSystem - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_by_API%2Fjavax.sound.midi%2FMidiSystem"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_by_API/javax.sound.midi/MidiSystem&amp;action=history"/>
		<updated>2026-04-23T10:42:44Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_by_API/javax.sound.midi/MidiSystem&amp;diff=2000&amp;oldid=prev</id>
		<title> в 17:43, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_by_API/javax.sound.midi/MidiSystem&amp;diff=2000&amp;oldid=prev"/>
				<updated>2010-05-31T17:43:48Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:43, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_by_API/javax.sound.midi/MidiSystem&amp;diff=2001&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_by_API/javax.sound.midi/MidiSystem&amp;diff=2001&amp;oldid=prev"/>
				<updated>2010-05-31T14:39:58Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== MidiSystem: getMidiFileTypes(Sequence sequence) ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import javax.sound.midi.InvalidMidiDataException;&lt;br /&gt;
import javax.sound.midi.MetaEventListener;&lt;br /&gt;
import javax.sound.midi.MetaMessage;&lt;br /&gt;
import javax.sound.midi.MidiEvent;&lt;br /&gt;
import javax.sound.midi.MidiSystem;&lt;br /&gt;
import javax.sound.midi.MidiUnavailableException;&lt;br /&gt;
import javax.sound.midi.Sequence;&lt;br /&gt;
import javax.sound.midi.Sequencer;&lt;br /&gt;
import javax.sound.midi.ShortMessage;&lt;br /&gt;
import javax.sound.midi.Synthesizer;&lt;br /&gt;
import javax.sound.midi.Track;&lt;br /&gt;
public class Main {&lt;br /&gt;
  // These are some MIDI constants from the spec. They aren&amp;quot;t defined&lt;br /&gt;
  // for us in javax.sound.midi.&lt;br /&gt;
  public static final int DAMPER_PEDAL = 64;&lt;br /&gt;
  public static final int DAMPER_ON = 127;&lt;br /&gt;
  public static final int DAMPER_OFF = 0;&lt;br /&gt;
  public static final int END_OF_TRACK = 47;&lt;br /&gt;
  public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException,&lt;br /&gt;
      IOException {&lt;br /&gt;
    int instrument = 0;&lt;br /&gt;
    int tempo = 120;&lt;br /&gt;
    String filename = null;&lt;br /&gt;
    // Parse the options&lt;br /&gt;
    // -i &amp;lt;instrument number&amp;gt; default 0, a piano. Allowed values: 0-127&lt;br /&gt;
    // -t &amp;lt;beats per minute&amp;gt; default tempo is 120 quarter notes per minute&lt;br /&gt;
    // -o &amp;lt;filename&amp;gt; save to a midi file instead of playing&lt;br /&gt;
    int a = 0;&lt;br /&gt;
    while (a &amp;lt; args.length) {&lt;br /&gt;
      if (args[a].equals(&amp;quot;-i&amp;quot;)) {&lt;br /&gt;
        instrument = Integer.parseInt(args[a + 1]);&lt;br /&gt;
        a += 2;&lt;br /&gt;
      } else if (args[a].equals(&amp;quot;-t&amp;quot;)) {&lt;br /&gt;
        tempo = Integer.parseInt(args[a + 1]);&lt;br /&gt;
        a += 2;&lt;br /&gt;
      } else if (args[a].equals(&amp;quot;-o&amp;quot;)) {&lt;br /&gt;
        filename = args[a + 1];&lt;br /&gt;
        a += 2;&lt;br /&gt;
      } else&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    char[] notes = args[a].toCharArray();&lt;br /&gt;
    // 16 ticks per quarter note.&lt;br /&gt;
    Sequence sequence = new Sequence(Sequence.PPQ, 16);&lt;br /&gt;
    // Add the specified notes to the track&lt;br /&gt;
    addTrack(sequence, instrument, tempo, notes);&lt;br /&gt;
    if (filename == null) { // no filename, so play the notes&lt;br /&gt;
      // Set up the Sequencer and Synthesizer objects&lt;br /&gt;
      Sequencer sequencer = MidiSystem.getSequencer();&lt;br /&gt;
      sequencer.open();&lt;br /&gt;
      Synthesizer synthesizer = MidiSystem.getSynthesizer();&lt;br /&gt;
      synthesizer.open();&lt;br /&gt;
      sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());&lt;br /&gt;
      // Specify the sequence to play, and the tempo to play it at&lt;br /&gt;
      sequencer.setSequence(sequence);&lt;br /&gt;
      sequencer.setTempoInBPM(tempo);&lt;br /&gt;
      // Let us know when it is done playing&lt;br /&gt;
      sequencer.addMetaEventListener(new MetaEventListener() {&lt;br /&gt;
        public void meta(MetaMessage m) {&lt;br /&gt;
          // A message of this type is automatically sent&lt;br /&gt;
          // when we reach the end of the track&lt;br /&gt;
          if (m.getType() == END_OF_TRACK)&lt;br /&gt;
            System.exit(0);&lt;br /&gt;
        }&lt;br /&gt;
      });&lt;br /&gt;
      // And start playing now.&lt;br /&gt;
      sequencer.start();&lt;br /&gt;
    } else { // A file name was specified, so save the notes&lt;br /&gt;
      int[] allowedTypes = MidiSystem.getMidiFileTypes(sequence);&lt;br /&gt;
      if (allowedTypes.length == 0) {&lt;br /&gt;
        System.err.println(&amp;quot;No supported MIDI file types.&amp;quot;);&lt;br /&gt;
      } else {&lt;br /&gt;
        MidiSystem.write(sequence, allowedTypes[0], new File(filename));&lt;br /&gt;
        System.exit(0);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  static final int[] offsets = { // add these amounts to the base value&lt;br /&gt;
  // A B C D E F G&lt;br /&gt;
      -4, -2, 0, 1, 3, 5, 7 };&lt;br /&gt;
  /*&lt;br /&gt;
   * This method parses the specified char[] of notes into a Track. The musical&lt;br /&gt;
   * notation is the following: A-G: A named note; Add b for flat and # for&lt;br /&gt;
   * sharp. +: Move up one octave. Persists. -: Move down one octave. Persists.&lt;br /&gt;
   * /1: Notes are whole notes. Persists &amp;quot;till changed /2: Half notes /4:&lt;br /&gt;
   * Quarter notes /n: N can also be, 8, 16, 32, 64. s: Toggle sustain pedal on&lt;br /&gt;
   * or off (initially off)&lt;br /&gt;
   *  &amp;gt;: Louder. Persists &amp;lt;: Softer. Persists .: Rest. Length depends on current&lt;br /&gt;
   * length setting Space: Play the previous note or notes; notes not separated&lt;br /&gt;
   * by spaces are played at the same time&lt;br /&gt;
   */&lt;br /&gt;
  public static void addTrack(Sequence s, int instrument, int tempo, char[] notes)&lt;br /&gt;
      throws InvalidMidiDataException {&lt;br /&gt;
    Track track = s.createTrack(); // Begin with a new track&lt;br /&gt;
    // Set the instrument on channel 0&lt;br /&gt;
    ShortMessage sm = new ShortMessage();&lt;br /&gt;
    sm.setMessage(ShortMessage.PROGRAM_CHANGE, 0, instrument, 0);&lt;br /&gt;
    track.add(new MidiEvent(sm, 0));&lt;br /&gt;
    int n = 0; // current character in notes[] array&lt;br /&gt;
    int t = 0; // time in ticks for the composition&lt;br /&gt;
    // These values persist and apply to all notes &amp;quot;till changed&lt;br /&gt;
    int notelength = 16; // default to quarter notes&lt;br /&gt;
    int velocity = 64; // default to middle volume&lt;br /&gt;
    int basekey = 60; // 60 is middle C. Adjusted up and down by octave&lt;br /&gt;
    boolean sustain = false; // is the sustain pedal depressed?&lt;br /&gt;
    int numnotes = 0; // How many notes in current chord?&lt;br /&gt;
    while (n &amp;lt; notes.length) {&lt;br /&gt;
      char c = notes[n++];&lt;br /&gt;
      if (c == &amp;quot;+&amp;quot;)&lt;br /&gt;
        basekey += 12; // increase octave&lt;br /&gt;
      else if (c == &amp;quot;-&amp;quot;)&lt;br /&gt;
        basekey -= 12; // decrease octave&lt;br /&gt;
      else if (c == &amp;quot;&amp;gt;&amp;quot;)&lt;br /&gt;
        velocity += 16; // increase volume;&lt;br /&gt;
      else if (c == &amp;quot;&amp;lt;&amp;quot;)&lt;br /&gt;
        velocity -= 16; // decrease volume;&lt;br /&gt;
      else if (c == &amp;quot;/&amp;quot;) {&lt;br /&gt;
        char d = notes[n++];&lt;br /&gt;
        if (d == &amp;quot;2&amp;quot;)&lt;br /&gt;
          notelength = 32; // half note&lt;br /&gt;
        else if (d == &amp;quot;4&amp;quot;)&lt;br /&gt;
          notelength = 16; // quarter note&lt;br /&gt;
        else if (d == &amp;quot;8&amp;quot;)&lt;br /&gt;
          notelength = 8; // eighth note&lt;br /&gt;
        else if (d == &amp;quot;3&amp;quot; &amp;amp;&amp;amp; notes[n++] == &amp;quot;2&amp;quot;)&lt;br /&gt;
          notelength = 2;&lt;br /&gt;
        else if (d == &amp;quot;6&amp;quot; &amp;amp;&amp;amp; notes[n++] == &amp;quot;4&amp;quot;)&lt;br /&gt;
          notelength = 1;&lt;br /&gt;
        else if (d == &amp;quot;1&amp;quot;) {&lt;br /&gt;
          if (n &amp;lt; notes.length &amp;amp;&amp;amp; notes[n] == &amp;quot;6&amp;quot;)&lt;br /&gt;
            notelength = 4; // 1/16th note&lt;br /&gt;
          else&lt;br /&gt;
            notelength = 64; // whole note&lt;br /&gt;
        }&lt;br /&gt;
      } else if (c == &amp;quot;s&amp;quot;) {&lt;br /&gt;
        sustain = !sustain;&lt;br /&gt;
        // Change the sustain setting for channel 0&lt;br /&gt;
        ShortMessage m = new ShortMessage();&lt;br /&gt;
        m&lt;br /&gt;
            .setMessage(ShortMessage.CONTROL_CHANGE, 0, DAMPER_PEDAL, sustain ? DAMPER_ON&lt;br /&gt;
                : DAMPER_OFF);&lt;br /&gt;
        track.add(new MidiEvent(m, t));&lt;br /&gt;
      } else if (c &amp;gt;= &amp;quot;A&amp;quot; &amp;amp;&amp;amp; c &amp;lt;= &amp;quot;G&amp;quot;) {&lt;br /&gt;
        int key = basekey + offsets[c - &amp;quot;A&amp;quot;];&lt;br /&gt;
        if (n &amp;lt; notes.length) {&lt;br /&gt;
          if (notes[n] == &amp;quot;b&amp;quot;) { // flat&lt;br /&gt;
            key--;&lt;br /&gt;
            n++;&lt;br /&gt;
          } else if (notes[n] == &amp;quot;#&amp;quot;) { // sharp&lt;br /&gt;
            key++;&lt;br /&gt;
            n++;&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        addNote(track, t, notelength, key, velocity);&lt;br /&gt;
        numnotes++;&lt;br /&gt;
      } else if (c == &amp;quot; &amp;quot;) {&lt;br /&gt;
        // Spaces separate groups of notes played at the same time.&lt;br /&gt;
        // But we ignore them unless they follow a note or notes.&lt;br /&gt;
        if (numnotes &amp;gt; 0) {&lt;br /&gt;
          t += notelength;&lt;br /&gt;
          numnotes = 0;&lt;br /&gt;
        }&lt;br /&gt;
      } else if (c == &amp;quot;.&amp;quot;) {&lt;br /&gt;
        // Rests are like spaces in that they force any previous&lt;br /&gt;
        // note to be output (since they are never part of chords)&lt;br /&gt;
        if (numnotes &amp;gt; 0) {&lt;br /&gt;
          t += notelength;&lt;br /&gt;
          numnotes = 0;&lt;br /&gt;
        }&lt;br /&gt;
        // Now add additional rest time&lt;br /&gt;
        t += notelength;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // A convenience method to add a note to the track on channel 0&lt;br /&gt;
  public static void addNote(Track track, int startTick, int tickLength, int key, int velocity)&lt;br /&gt;
      throws InvalidMidiDataException {&lt;br /&gt;
    ShortMessage on = new ShortMessage();&lt;br /&gt;
    on.setMessage(ShortMessage.NOTE_ON, 0, key, velocity);&lt;br /&gt;
    ShortMessage off = new ShortMessage();&lt;br /&gt;
    off.setMessage(ShortMessage.NOTE_OFF, 0, key, velocity);&lt;br /&gt;
    track.add(new MidiEvent(on, startTick));&lt;br /&gt;
    track.add(new MidiEvent(off, startTick + tickLength));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== MidiSystem: write(Sequence in, int type, File out) ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import javax.sound.midi.InvalidMidiDataException;&lt;br /&gt;
import javax.sound.midi.MetaEventListener;&lt;br /&gt;
import javax.sound.midi.MetaMessage;&lt;br /&gt;
import javax.sound.midi.MidiEvent;&lt;br /&gt;
import javax.sound.midi.MidiSystem;&lt;br /&gt;
import javax.sound.midi.MidiUnavailableException;&lt;br /&gt;
import javax.sound.midi.Sequence;&lt;br /&gt;
import javax.sound.midi.Sequencer;&lt;br /&gt;
import javax.sound.midi.ShortMessage;&lt;br /&gt;
import javax.sound.midi.Synthesizer;&lt;br /&gt;
import javax.sound.midi.Track;&lt;br /&gt;
public class Main {&lt;br /&gt;
  // These are some MIDI constants from the spec. They aren&amp;quot;t defined&lt;br /&gt;
  // for us in javax.sound.midi.&lt;br /&gt;
  public static final int DAMPER_PEDAL = 64;&lt;br /&gt;
  public static final int DAMPER_ON = 127;&lt;br /&gt;
  public static final int DAMPER_OFF = 0;&lt;br /&gt;
  public static final int END_OF_TRACK = 47;&lt;br /&gt;
  public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException,&lt;br /&gt;
      IOException {&lt;br /&gt;
    int instrument = 0;&lt;br /&gt;
    int tempo = 120;&lt;br /&gt;
    String filename = null;&lt;br /&gt;
    // Parse the options&lt;br /&gt;
    // -i &amp;lt;instrument number&amp;gt; default 0, a piano. Allowed values: 0-127&lt;br /&gt;
    // -t &amp;lt;beats per minute&amp;gt; default tempo is 120 quarter notes per minute&lt;br /&gt;
    // -o &amp;lt;filename&amp;gt; save to a midi file instead of playing&lt;br /&gt;
    int a = 0;&lt;br /&gt;
    while (a &amp;lt; args.length) {&lt;br /&gt;
      if (args[a].equals(&amp;quot;-i&amp;quot;)) {&lt;br /&gt;
        instrument = Integer.parseInt(args[a + 1]);&lt;br /&gt;
        a += 2;&lt;br /&gt;
      } else if (args[a].equals(&amp;quot;-t&amp;quot;)) {&lt;br /&gt;
        tempo = Integer.parseInt(args[a + 1]);&lt;br /&gt;
        a += 2;&lt;br /&gt;
      } else if (args[a].equals(&amp;quot;-o&amp;quot;)) {&lt;br /&gt;
        filename = args[a + 1];&lt;br /&gt;
        a += 2;&lt;br /&gt;
      } else&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    char[] notes = args[a].toCharArray();&lt;br /&gt;
    // 16 ticks per quarter note.&lt;br /&gt;
    Sequence sequence = new Sequence(Sequence.PPQ, 16);&lt;br /&gt;
    // Add the specified notes to the track&lt;br /&gt;
    addTrack(sequence, instrument, tempo, notes);&lt;br /&gt;
    if (filename == null) { // no filename, so play the notes&lt;br /&gt;
      // Set up the Sequencer and Synthesizer objects&lt;br /&gt;
      Sequencer sequencer = MidiSystem.getSequencer();&lt;br /&gt;
      sequencer.open();&lt;br /&gt;
      Synthesizer synthesizer = MidiSystem.getSynthesizer();&lt;br /&gt;
      synthesizer.open();&lt;br /&gt;
      sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());&lt;br /&gt;
      // Specify the sequence to play, and the tempo to play it at&lt;br /&gt;
      sequencer.setSequence(sequence);&lt;br /&gt;
      sequencer.setTempoInBPM(tempo);&lt;br /&gt;
      // Let us know when it is done playing&lt;br /&gt;
      sequencer.addMetaEventListener(new MetaEventListener() {&lt;br /&gt;
        public void meta(MetaMessage m) {&lt;br /&gt;
          // A message of this type is automatically sent&lt;br /&gt;
          // when we reach the end of the track&lt;br /&gt;
          if (m.getType() == END_OF_TRACK)&lt;br /&gt;
            System.exit(0);&lt;br /&gt;
        }&lt;br /&gt;
      });&lt;br /&gt;
      // And start playing now.&lt;br /&gt;
      sequencer.start();&lt;br /&gt;
    } else { // A file name was specified, so save the notes&lt;br /&gt;
      int[] allowedTypes = MidiSystem.getMidiFileTypes(sequence);&lt;br /&gt;
      if (allowedTypes.length == 0) {&lt;br /&gt;
        System.err.println(&amp;quot;No supported MIDI file types.&amp;quot;);&lt;br /&gt;
      } else {&lt;br /&gt;
        MidiSystem.write(sequence, allowedTypes[0], new File(filename));&lt;br /&gt;
        System.exit(0);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  static final int[] offsets = { // add these amounts to the base value&lt;br /&gt;
  // A B C D E F G&lt;br /&gt;
      -4, -2, 0, 1, 3, 5, 7 };&lt;br /&gt;
  /*&lt;br /&gt;
   * This method parses the specified char[] of notes into a Track. The musical&lt;br /&gt;
   * notation is the following: A-G: A named note; Add b for flat and # for&lt;br /&gt;
   * sharp. +: Move up one octave. Persists. -: Move down one octave. Persists.&lt;br /&gt;
   * /1: Notes are whole notes. Persists &amp;quot;till changed /2: Half notes /4:&lt;br /&gt;
   * Quarter notes /n: N can also be, 8, 16, 32, 64. s: Toggle sustain pedal on&lt;br /&gt;
   * or off (initially off)&lt;br /&gt;
   *  &amp;gt;: Louder. Persists &amp;lt;: Softer. Persists .: Rest. Length depends on current&lt;br /&gt;
   * length setting Space: Play the previous note or notes; notes not separated&lt;br /&gt;
   * by spaces are played at the same time&lt;br /&gt;
   */&lt;br /&gt;
  public static void addTrack(Sequence s, int instrument, int tempo, char[] notes)&lt;br /&gt;
      throws InvalidMidiDataException {&lt;br /&gt;
    Track track = s.createTrack(); // Begin with a new track&lt;br /&gt;
    // Set the instrument on channel 0&lt;br /&gt;
    ShortMessage sm = new ShortMessage();&lt;br /&gt;
    sm.setMessage(ShortMessage.PROGRAM_CHANGE, 0, instrument, 0);&lt;br /&gt;
    track.add(new MidiEvent(sm, 0));&lt;br /&gt;
    int n = 0; // current character in notes[] array&lt;br /&gt;
    int t = 0; // time in ticks for the composition&lt;br /&gt;
    // These values persist and apply to all notes &amp;quot;till changed&lt;br /&gt;
    int notelength = 16; // default to quarter notes&lt;br /&gt;
    int velocity = 64; // default to middle volume&lt;br /&gt;
    int basekey = 60; // 60 is middle C. Adjusted up and down by octave&lt;br /&gt;
    boolean sustain = false; // is the sustain pedal depressed?&lt;br /&gt;
    int numnotes = 0; // How many notes in current chord?&lt;br /&gt;
    while (n &amp;lt; notes.length) {&lt;br /&gt;
      char c = notes[n++];&lt;br /&gt;
      if (c == &amp;quot;+&amp;quot;)&lt;br /&gt;
        basekey += 12; // increase octave&lt;br /&gt;
      else if (c == &amp;quot;-&amp;quot;)&lt;br /&gt;
        basekey -= 12; // decrease octave&lt;br /&gt;
      else if (c == &amp;quot;&amp;gt;&amp;quot;)&lt;br /&gt;
        velocity += 16; // increase volume;&lt;br /&gt;
      else if (c == &amp;quot;&amp;lt;&amp;quot;)&lt;br /&gt;
        velocity -= 16; // decrease volume;&lt;br /&gt;
      else if (c == &amp;quot;/&amp;quot;) {&lt;br /&gt;
        char d = notes[n++];&lt;br /&gt;
        if (d == &amp;quot;2&amp;quot;)&lt;br /&gt;
          notelength = 32; // half note&lt;br /&gt;
        else if (d == &amp;quot;4&amp;quot;)&lt;br /&gt;
          notelength = 16; // quarter note&lt;br /&gt;
        else if (d == &amp;quot;8&amp;quot;)&lt;br /&gt;
          notelength = 8; // eighth note&lt;br /&gt;
        else if (d == &amp;quot;3&amp;quot; &amp;amp;&amp;amp; notes[n++] == &amp;quot;2&amp;quot;)&lt;br /&gt;
          notelength = 2;&lt;br /&gt;
        else if (d == &amp;quot;6&amp;quot; &amp;amp;&amp;amp; notes[n++] == &amp;quot;4&amp;quot;)&lt;br /&gt;
          notelength = 1;&lt;br /&gt;
        else if (d == &amp;quot;1&amp;quot;) {&lt;br /&gt;
          if (n &amp;lt; notes.length &amp;amp;&amp;amp; notes[n] == &amp;quot;6&amp;quot;)&lt;br /&gt;
            notelength = 4; // 1/16th note&lt;br /&gt;
          else&lt;br /&gt;
            notelength = 64; // whole note&lt;br /&gt;
        }&lt;br /&gt;
      } else if (c == &amp;quot;s&amp;quot;) {&lt;br /&gt;
        sustain = !sustain;&lt;br /&gt;
        // Change the sustain setting for channel 0&lt;br /&gt;
        ShortMessage m = new ShortMessage();&lt;br /&gt;
        m&lt;br /&gt;
            .setMessage(ShortMessage.CONTROL_CHANGE, 0, DAMPER_PEDAL, sustain ? DAMPER_ON&lt;br /&gt;
                : DAMPER_OFF);&lt;br /&gt;
        track.add(new MidiEvent(m, t));&lt;br /&gt;
      } else if (c &amp;gt;= &amp;quot;A&amp;quot; &amp;amp;&amp;amp; c &amp;lt;= &amp;quot;G&amp;quot;) {&lt;br /&gt;
        int key = basekey + offsets[c - &amp;quot;A&amp;quot;];&lt;br /&gt;
        if (n &amp;lt; notes.length) {&lt;br /&gt;
          if (notes[n] == &amp;quot;b&amp;quot;) { // flat&lt;br /&gt;
            key--;&lt;br /&gt;
            n++;&lt;br /&gt;
          } else if (notes[n] == &amp;quot;#&amp;quot;) { // sharp&lt;br /&gt;
            key++;&lt;br /&gt;
            n++;&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        addNote(track, t, notelength, key, velocity);&lt;br /&gt;
        numnotes++;&lt;br /&gt;
      } else if (c == &amp;quot; &amp;quot;) {&lt;br /&gt;
        // Spaces separate groups of notes played at the same time.&lt;br /&gt;
        // But we ignore them unless they follow a note or notes.&lt;br /&gt;
        if (numnotes &amp;gt; 0) {&lt;br /&gt;
          t += notelength;&lt;br /&gt;
          numnotes = 0;&lt;br /&gt;
        }&lt;br /&gt;
      } else if (c == &amp;quot;.&amp;quot;) {&lt;br /&gt;
        // Rests are like spaces in that they force any previous&lt;br /&gt;
        // note to be output (since they are never part of chords)&lt;br /&gt;
        if (numnotes &amp;gt; 0) {&lt;br /&gt;
          t += notelength;&lt;br /&gt;
          numnotes = 0;&lt;br /&gt;
        }&lt;br /&gt;
        // Now add additional rest time&lt;br /&gt;
        t += notelength;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // A convenience method to add a note to the track on channel 0&lt;br /&gt;
  public static void addNote(Track track, int startTick, int tickLength, int key, int velocity)&lt;br /&gt;
      throws InvalidMidiDataException {&lt;br /&gt;
    ShortMessage on = new ShortMessage();&lt;br /&gt;
    on.setMessage(ShortMessage.NOTE_ON, 0, key, velocity);&lt;br /&gt;
    ShortMessage off = new ShortMessage();&lt;br /&gt;
    off.setMessage(ShortMessage.NOTE_OFF, 0, key, velocity);&lt;br /&gt;
    track.add(new MidiEvent(on, startTick));&lt;br /&gt;
    track.add(new MidiEvent(off, startTick + tickLength));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>