Source code: qt/QTThread.java
1 /*
2
3 < This Java Class is part of the jMusic API Version 2000.10>
4
5 Copyright (C) 2000 Andrew Sorensen & Andrew Brown
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or any
10 later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22
23 package qt;
24
25 import jm.JMC;
26 import jm.music.data.*;
27 import jm.midi.*;
28 import jm.util.*;
29
30 import quicktime.*;
31 import quicktime.qd.*;
32 import quicktime.sound.*;
33 import quicktime.std.music.*;
34 import quicktime.std.*;
35 import quicktime.std.qtcomponents.*;
36
37 import quicktime.util.*;
38
39 public final class QTThread implements JMC, Runnable{
40 private Thread myThread;
41 private Note note;
42 private NoteChannel nc, tempnc;
43 private int tempPitch;
44
45
46
47 public QTThread() {
48 note = new Note();
49 // set a pitch flag for 'empty'note as a REST
50 note.setPitch(REST);
51 // Thread Stuff
52 myThread = new Thread(this);
53 myThread.start();
54 //myThread.setPriority(Thread.MAX_PRIORITY);
55 }
56
57 public void setNote(NoteChannel nc, Note note) {
58 myThread.interrupt(); // stop short any sounding notes
59 this.nc = nc;
60 this.note = note;
61 }
62
63 public void run() {
64 while (true) {
65 if(note.getPitch() != REST) sendNote();
66 try { myThread.sleep((long) 5); }
67 catch( InterruptedException e) {}
68 }
69 }
70
71 private void sendNote() {
72 tempPitch = note.getPitch();
73 tempnc = nc;
74 try {
75 tempnc.playNote ( tempPitch, note.getDynamic()); // note on
76 try {
77 myThread.sleep ((long)note.getDuration() * 500);
78 }
79 catch (InterruptedException e) {
80 tempnc.playNote (tempPitch, 0); // note off
81 note.setPitch(REST);
82 }
83 tempnc.playNote ( tempPitch, 0); // note off
84 note.setPitch(REST);
85 }
86 catch (QTException qte) {
87 qte.printStackTrace();
88 }
89 }
90
91 public void suspendThread() {
92 myThread.suspend();
93 }
94
95 public void resumeThread() {
96 myThread.resume();
97 }
98
99 public void stop() {
100 myThread.stop();
101 }
102 }