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 doesn't create distinct messages when recording events.

  38  */
  39 public class SeqRecordDoesNotCopy {
  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.setMessage(0x90, 0x40, 0x00);
  58             t.add(new MidiEvent(msg, 12000));
  59             t = seq.getTracks()[1];
  60             s.recordEnable(t, -1);
  61             System.out.println("Started recording...");
  62             s.startRecording();
  63             Receiver r = s.getReceiver();
  64             Thread.sleep(100);
  65             // send a normal message
  66             System.out.println("Recording a normal NOTE ON message...");
  67             msg.setMessage(0x90, 0x40, 0x6F);
  68             r.send(msg, -1);
  69             Thread.sleep(100);
  70             // send a normal message
  71             System.out.println("Recording a normal NOTE OFF message...");
  72             msg.setMessage(0x90, 0x40, 0x00);
  73             r.send(msg, -1);
  74             Thread.sleep(100);
  75             s.stop();
  76             // now see if the messages were recorded
  77             System.out.println("Recorded messages:");
  78             int sameMessage = 0;
  79             for (int i = 0; i < t.size(); i++) {
  80                 System.out.print(" "+(i+1)+". ");
  81                 printEvent(t.get(i));
  82                 if (t.get(i).getMessage() == msg) {
  83                     System.out.println("## Failed: Same Message reference!");
  84                     sameMessage++;
  85                 }
  86             }
  87             if (sameMessage > 0) {
  88                 System.out.println("## Failed: The same instance was recorded!");
  89                 throw new Exception("Test FAILED!");
  90             }
  91             System.out.println("Did not detect any duplicate messages.");
  92             System.out.println("Test passed.");
  93         } catch (Exception e) {
  94             System.out.println("Unexpected Exception: "+e);
  95             //e.printStackTrace();
  96             throw new RuntimeException("Test FAILED!");
  97         } finally {
  98             s.close();
  99         }
 100     }
 101     public static void printEvent(MidiEvent event)
 102     {
 103         MidiMessage message = event.getMessage();
 104         long tick = event.getTick();
 105         byte[] data = message.getMessage();
 106 
 107         StringBuffer sb = new StringBuffer((data.length * 3) - 1);
 108 
 109         for (int i = 0; i < data.length; i++)
 110         {
 111                 sb.append(toHexByteString(data[i]));
 112                 if (i < data.length - 1) sb.append(' ');
 113         }
 114         System.out.printf("%5d: %s%n", tick, sb);
 115     }
 116 
 117     private static String toHexByteString(int n)
 118     {
 119         if (n < 0) n &= 0xFF;
 120         String s = Integer.toHexString(n).toUpperCase();
 121         if (s.length() == 1) s = '0' + s;
 122         return s;
 123     }
 124 }
--- EOF ---