daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.IOException;
  3.  
  4. import javax.sound.midi.MidiUnavailableException;
  5. import javax.sound.midi.Sequence;
  6. import javax.sound.midi.MidiEvent;
  7. import javax.sound.midi.MidiMessage;
  8. import javax.sound.midi.MidiSystem;
  9. import javax.sound.midi.ShortMessage;
  10. import javax.sound.midi.Track;
  11. import javax.sound.midi.InvalidMidiDataException;
  12. import javax.sound.midi.Sequencer;
  13.  
  14. /** <titleabbrev>CreateSequence</titleabbrev>
  15.     <title>Creating a Sequence</title>
  16.  
  17.     <formalpara><title>Purpose</title>
  18.     <para>
  19.     Shows how to construct a Sequence object with a Track and MidiEvents
  20.     in memory and save it to a Standard MIDI File (SMF).
  21.     </para></formalpara>
  22.  
  23.     <formalpara><title>Usage</title>
  24.     <para>
  25.     <cmdsynopsis>
  26.     <command>java CreateSequence</command>
  27.     <arg choice="plain"><replaceable class="parameter">midi_file</replaceable></arg>
  28.     </cmdsynopsis>
  29.     </para>
  30.     </formalpara>
  31.  
  32.     <formalpara><title>Parameters</title>
  33.     <variablelist>
  34.     <varlistentry>
  35.     <term><option><replaceable class="parameter">midi_file</replaceable></option></term>
  36.     <listitem><para>the name of the file to save to as a Standard Midi File.</para></listitem>
  37.     </varlistentry>
  38.     </variablelist>
  39.     </formalpara>
  40.  
  41.     <formalpara><title>Bugs, limitations</title>
  42.     <para>None
  43.     </para>
  44.     </formalpara>
  45.  
  46.     <formalpara><title>Source code</title>
  47.     <para>
  48.     <ulink url="CreateSequence.java.html">CreateSequence.java</ulink>
  49.     </para>
  50.     </formalpara>
  51.      
  52. */
  53. public class CreateSequence
  54. {
  55.     /*  This velocity is used for all notes.
  56.      */
  57.     private static final int    VELOCITY = 100;
  58.  
  59.  
  60.     public static void main(String[] args) throws InvalidMidiDataException
  61.     {
  62.         if (args.length != 1)
  63.         {
  64.             printUsageAndExit();
  65.         }
  66.         File outputFile = new File(args[0]);
  67.         Sequence    sequence = null;
  68.         try
  69.         {
  70.             sequence = new Sequence(Sequence.PPQ, 1);
  71.         }
  72.         catch (InvalidMidiDataException e)
  73.         {
  74.             e.printStackTrace();
  75.             System.exit(1);
  76.         }
  77.         /* Track objects cannot be created by invoking their constructor
  78.            directly. Instead, the Sequence object does the job. So we
  79.            obtain the Track there. This links the Track to the Sequence
  80.            automatically.
  81.         */
  82.         Track   track = sequence.createTrack();
  83.  
  84.         // first chord: C major
  85.         track.add(createNoteOnEvent(60, 0));
  86.         track.add(createNoteOnEvent(64, 0));
  87.         track.add(createNoteOnEvent(67, 0));
  88.         track.add(createNoteOnEvent(72, 0));
  89.         track.add(createNoteOffEvent(60, 1));
  90.         track.add(createNoteOffEvent(64, 1));
  91.         track.add(createNoteOffEvent(67, 1));
  92.         track.add(createNoteOffEvent(72, 1));
  93.  
  94.         // second chord: f minor N
  95.         track.add(createNoteOnEvent(53, 1));
  96.         track.add(createNoteOnEvent(65, 1));
  97.         track.add(createNoteOnEvent(68, 1));
  98.         track.add(createNoteOnEvent(73, 1));
  99.         track.add(createNoteOffEvent(63, 2));
  100.         track.add(createNoteOffEvent(65, 2));
  101.         track.add(createNoteOffEvent(68, 2));
  102.         track.add(createNoteOffEvent(73, 2));
  103.  
  104.         // third chord: C major 6-4
  105.         track.add(createNoteOnEvent(55, 2));
  106.         track.add(createNoteOnEvent(64, 2));
  107.         track.add(createNoteOnEvent(67, 2));
  108.         track.add(createNoteOnEvent(72, 2));
  109.         track.add(createNoteOffEvent(64, 3));
  110.         track.add(createNoteOffEvent(72, 3));
  111.  
  112.         // forth chord: G major 7
  113.         track.add(createNoteOnEvent(65, 3));
  114.         track.add(createNoteOnEvent(71, 3));
  115.         track.add(createNoteOffEvent(55, 4));
  116.         track.add(createNoteOffEvent(65, 4));
  117.         track.add(createNoteOffEvent(67, 4));
  118.         track.add(createNoteOffEvent(71, 4));
  119.  
  120.         // fifth chord: C major
  121.         track.add(createNoteOnEvent(48, 4));
  122.         track.add(createNoteOnEvent(64, 4));
  123.         track.add(createNoteOnEvent(67, 4));
  124.         track.add(createNoteOnEvent(72, 4));
  125.         track.add(createNoteOffEvent(48, 8));
  126.         track.add(createNoteOffEvent(64, 8));
  127.         track.add(createNoteOffEvent(67, 8));
  128.         track.add(createNoteOffEvent(72, 8));
  129.  
  130.         /* Now we just save the Sequence to the file we specified.
  131.            The '0' (second parameter) means saving as SMF type 0.
  132.            Since we have only one Track, this is actually the only option
  133.            (type 1 is for multiple tracks).
  134.         */
  135.         try {
  136.             Sequencer player = MidiSystem.getSequencer();
  137.             player.open(); //the sequencer was never opened
  138.             player.setSequence(sequence);
  139.             player.setLoopStartPoint(0);
  140.             player.start();
  141.             try {
  142.                 Thread.sleep(3000);
  143.             }
  144.             catch (InterruptedException e) {
  145.                
  146.             }
  147.             player.close();
  148.         }
  149.         catch (MidiUnavailableException e){
  150.             System.out.println("error1");
  151.         }              
  152.        
  153.         try
  154.         {
  155.             MidiSystem.write(sequence, 0, outputFile);
  156.         }
  157.         catch (IOException e)
  158.         {
  159.             e.printStackTrace();
  160.             System.exit(1);
  161.         }
  162.        
  163.     }
  164.  
  165.  
  166.  
  167.     private static MidiEvent createNoteOnEvent(int nKey, long lTick)
  168.     {
  169.         return createNoteEvent(ShortMessage.NOTE_ON,
  170.                                nKey,
  171.                                VELOCITY,
  172.                                lTick);
  173.     }
  174.  
  175.  
  176.     private static MidiEvent createNoteOffEvent(int nKey, long lTick)
  177.     {
  178.         return createNoteEvent(ShortMessage.NOTE_OFF,
  179.                                nKey,
  180.                                0,
  181.                                lTick);
  182.     }
  183.  
  184.  
  185.  
  186.     private static MidiEvent createNoteEvent(int nCommand,
  187.                                              int nKey,
  188.                                              int nVelocity,
  189.                                              long lTick)
  190.     {
  191.         ShortMessage    message = new ShortMessage();
  192.         try
  193.         {
  194.             message.setMessage(nCommand,
  195.                                0,   // always on channel 1
  196.                                nKey,
  197.                                nVelocity);
  198.         }
  199.         catch (InvalidMidiDataException e)
  200.         {
  201.             e.printStackTrace();
  202.             System.exit(1);
  203.         }
  204.         MidiEvent   event = new MidiEvent(message,
  205.                                           lTick);
  206.         return event;
  207.     }
  208.  
  209.  
  210.  
  211.     private static void printUsageAndExit()
  212.     {
  213.             out("usage:");
  214.             out("java CreateSequence <midifile>");
  215.             System.exit(1);
  216.     }
  217.  
  218.  
  219.     private static void out(String strMessage)
  220.     {
  221.         System.out.println(strMessage);
  222.     }
  223. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top