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