< prev index next >

test/javax/sound/midi/Sequencer/SeqRecordsRealTimeEvents.java

Print this page


   1 /*
   2  * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.sound.midi.MidiEvent;
  25 import javax.sound.midi.MidiMessage;
  26 import javax.sound.midi.MidiSystem;

  27 import javax.sound.midi.Receiver;
  28 import javax.sound.midi.Sequence;
  29 import javax.sound.midi.Sequencer;
  30 import javax.sound.midi.ShortMessage;
  31 import javax.sound.midi.Track;
  32 
  33 /**
  34  * @test
  35  * @bug 5048381
  36  * @summary Sequencer records real time messages into the sequence
  37  * @key headful
  38  */
  39 public class SeqRecordsRealTimeEvents {
  40     public static void main(String argv[]) throws Exception {
  41         Sequencer s = MidiSystem.getSequencer();



  42         s.open();




  43         try {
  44             Sequence seq = new Sequence(Sequence.PPQ, 384, 2);
  45             s.setSequence(seq);
  46             Track t = seq.getTracks()[0];
  47             ShortMessage msg = new ShortMessage();
  48             msg.setMessage(0x90, 0x40, 0x7F);
  49             t.add(new MidiEvent(msg, 11000));
  50             msg = new ShortMessage();
  51             msg.setMessage(0x90, 0x40, 0x00);
  52             t.add(new MidiEvent(msg, 12000));
  53             t = seq.getTracks()[1];
  54             s.recordEnable(t, -1);
  55             System.out.println("Started recording...");
  56             s.startRecording();
  57             Receiver r = s.getReceiver();
  58             Thread.sleep(100);
  59             int oldTrackSize = t.size();
  60             // send a realtime message to the track
  61             System.out.println("Recording real time message...");
  62             msg = new ShortMessage();


  73             // now see if the messages were recorded
  74             int newMessages = t.size() - oldTrackSize;
  75             System.out.println("Recorded messages:");
  76             for (int i = 0; i < t.size(); i++) {
  77                 System.out.print(" "+(i+1)+". ");
  78                 printEvent(t.get(i));
  79             }
  80             if (newMessages == 0) {
  81                 System.out.println("## Failed: No messages were recorded!");
  82                 throw new Exception("Test FAILED!");
  83             } else if (newMessages == 1) {
  84                 System.out.println("Only one message was recorded. Correct!");
  85             } else if (newMessages > 1) {
  86                 System.out.println("## Failed: 2 or more messages were recorded!");
  87                 throw new Exception("Test FAILED!");
  88             }
  89             System.out.println("Test passed.");
  90         } catch (Exception e) {
  91             System.out.println("Unexpected Exception: "+e);
  92             //e.printStackTrace();
  93             throw new Exception("Test FAILED!");
  94         } finally {
  95             s.close();
  96         }
  97     }
  98     public static void printEvent(MidiEvent event)
  99     {
 100         MidiMessage message = event.getMessage();
 101         long tick = event.getTick();
 102         byte[] data = message.getMessage();
 103 
 104         StringBuffer sb = new StringBuffer((data.length * 3) - 1);
 105 
 106         for (int i = 0; i < data.length; i++)
 107         {
 108                 sb.append(toHexByteString(data[i]));
 109                 if (i < data.length - 1) sb.append(' ');
 110         }
 111         System.out.printf("%5d: %s%n", tick, sb);
 112     }
 113 
   1 /*
   2  * Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.sound.midi.MidiEvent;
  25 import javax.sound.midi.MidiMessage;
  26 import javax.sound.midi.MidiSystem;
  27 import javax.sound.midi.MidiUnavailableException;
  28 import javax.sound.midi.Receiver;
  29 import javax.sound.midi.Sequence;
  30 import javax.sound.midi.Sequencer;
  31 import javax.sound.midi.ShortMessage;
  32 import javax.sound.midi.Track;
  33 
  34 /**
  35  * @test
  36  * @bug 5048381
  37  * @summary Sequencer records real time messages into the sequence

  38  */
  39 public class SeqRecordsRealTimeEvents {
  40 
  41     public static void main(String argv[]) {
  42         Sequencer s = null;
  43         try {
  44             s = MidiSystem.getSequencer();
  45             s.open();
  46         } catch (final MidiUnavailableException ignored) {
  47             // the test is not applicable
  48             return;
  49         }
  50         try {
  51             Sequence seq = new Sequence(Sequence.PPQ, 384, 2);
  52             s.setSequence(seq);
  53             Track t = seq.getTracks()[0];
  54             ShortMessage msg = new ShortMessage();
  55             msg.setMessage(0x90, 0x40, 0x7F);
  56             t.add(new MidiEvent(msg, 11000));
  57             msg = new ShortMessage();
  58             msg.setMessage(0x90, 0x40, 0x00);
  59             t.add(new MidiEvent(msg, 12000));
  60             t = seq.getTracks()[1];
  61             s.recordEnable(t, -1);
  62             System.out.println("Started recording...");
  63             s.startRecording();
  64             Receiver r = s.getReceiver();
  65             Thread.sleep(100);
  66             int oldTrackSize = t.size();
  67             // send a realtime message to the track
  68             System.out.println("Recording real time message...");
  69             msg = new ShortMessage();


  80             // now see if the messages were recorded
  81             int newMessages = t.size() - oldTrackSize;
  82             System.out.println("Recorded messages:");
  83             for (int i = 0; i < t.size(); i++) {
  84                 System.out.print(" "+(i+1)+". ");
  85                 printEvent(t.get(i));
  86             }
  87             if (newMessages == 0) {
  88                 System.out.println("## Failed: No messages were recorded!");
  89                 throw new Exception("Test FAILED!");
  90             } else if (newMessages == 1) {
  91                 System.out.println("Only one message was recorded. Correct!");
  92             } else if (newMessages > 1) {
  93                 System.out.println("## Failed: 2 or more messages were recorded!");
  94                 throw new Exception("Test FAILED!");
  95             }
  96             System.out.println("Test passed.");
  97         } catch (Exception e) {
  98             System.out.println("Unexpected Exception: "+e);
  99             //e.printStackTrace();
 100             throw new RuntimeException("Test FAILED!");
 101         } finally {
 102             s.close();
 103         }
 104     }
 105     public static void printEvent(MidiEvent event)
 106     {
 107         MidiMessage message = event.getMessage();
 108         long tick = event.getTick();
 109         byte[] data = message.getMessage();
 110 
 111         StringBuffer sb = new StringBuffer((data.length * 3) - 1);
 112 
 113         for (int i = 0; i < data.length; i++)
 114         {
 115                 sb.append(toHexByteString(data[i]));
 116                 if (i < data.length - 1) sb.append(' ');
 117         }
 118         System.out.printf("%5d: %s%n", tick, sb);
 119     }
 120 
< prev index next >