1 |
iliev |
596 |
/* |
2 |
|
|
* jlscp - a java LinuxSampler control protocol API |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2005 Grigor Kirilov Iliev |
5 |
|
|
* |
6 |
|
|
* This file is part of jlscp. |
7 |
|
|
* |
8 |
|
|
* jlscp is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License version 2 |
10 |
|
|
* as published by the Free Software Foundation. |
11 |
|
|
* |
12 |
|
|
* jlscp is distributed in the hope that it will be useful, |
13 |
|
|
* but 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 jlscp; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
|
|
* MA 02111-1307 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
import java.util.logging.Handler; |
24 |
|
|
import java.util.logging.Logger; |
25 |
|
|
import java.util.logging.SimpleFormatter; |
26 |
|
|
import java.util.logging.StreamHandler; |
27 |
|
|
|
28 |
|
|
import org.linuxsampler.lscp.BufferFill; |
29 |
|
|
import org.linuxsampler.lscp.Client; |
30 |
|
|
import org.linuxsampler.lscp.event.*; |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* Listens for events. |
34 |
|
|
* @author Grigor Iliev |
35 |
|
|
*/ |
36 |
|
|
public class Events { |
37 |
|
|
public static void |
38 |
|
|
main(String[] args) { |
39 |
|
|
initLogger(); |
40 |
|
|
|
41 |
|
|
MyListener myListener = new MyListener(); |
42 |
|
|
Client client = new Client(); |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
try { |
46 |
|
|
// Listeners can be added or removed regardless of the connection state. |
47 |
|
|
client.addBufferFillListener(myListener); |
48 |
|
|
client.addChannelCountListener(myListener); |
49 |
|
|
client.addChannelInfoListener(myListener); |
50 |
|
|
client.addMiscellaneousListener(myListener); |
51 |
|
|
|
52 |
|
|
client.connect(); |
53 |
|
|
|
54 |
|
|
client.addStreamCountListener(myListener); |
55 |
|
|
client.addVoiceCountListener(myListener); |
56 |
|
|
|
57 |
|
|
System.out.println("Press 'Enter' to exit"); |
58 |
|
|
System.in.read(); |
59 |
|
|
|
60 |
|
|
client.removeBufferFillListener(myListener); |
61 |
|
|
client.removeChannelCountListener(myListener); |
62 |
|
|
client.removeChannelInfoListener(myListener); |
63 |
|
|
|
64 |
|
|
client.disconnect(); |
65 |
|
|
|
66 |
|
|
client.removeMiscellaneousListener(myListener); |
67 |
|
|
client.removeStreamCountListener(myListener); |
68 |
|
|
client.removeVoiceCountListener(myListener); |
69 |
|
|
|
70 |
|
|
System.exit(0); |
71 |
|
|
} catch(Exception x) { |
72 |
|
|
x.printStackTrace(); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
System.exit(-1); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
private static void |
79 |
|
|
initLogger() { |
80 |
|
|
final Handler handler = new StreamHandler(System.out, new SimpleFormatter()); |
81 |
|
|
Logger.getLogger("org.linuxsampler.lscp").addHandler(handler); |
82 |
|
|
|
83 |
|
|
// Flushing logs on every second |
84 |
|
|
new java.util.Timer().schedule(new java.util.TimerTask() { |
85 |
|
|
public void |
86 |
|
|
run() { handler.flush(); } |
87 |
|
|
}, 1000, 1000); |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
class MyListener implements |
92 |
|
|
BufferFillListener, |
93 |
|
|
ChannelCountListener, |
94 |
|
|
ChannelInfoListener, |
95 |
|
|
MiscellaneousListener, |
96 |
|
|
StreamCountListener, |
97 |
|
|
VoiceCountListener { |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* Invoked when the fill state of a disk stream buffer has changed. |
101 |
|
|
*/ |
102 |
|
|
public void |
103 |
|
|
bufferFillChanged(BufferFillEvent e) { |
104 |
|
|
System.out.println ( |
105 |
|
|
"Buffer fill event occured on channel " + |
106 |
|
|
e.getSamplerChannel() |
107 |
|
|
); |
108 |
|
|
for(BufferFill bf : e.getChannelBufferFillPercentage()) { |
109 |
|
|
System.out.println ( |
110 |
|
|
"\tStream ID " + bf.getStreamID() + ": " + bf.getValue() + "%" |
111 |
|
|
); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/** Invoked when the number of channels is changed. */ |
116 |
|
|
public void |
117 |
|
|
channelCountChanged(ChannelCountEvent e) { |
118 |
|
|
System.out.println("The number of channels has changed to " + e.getChannelCount()); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/** Invoked when changes to the sampler channel has occured. */ |
122 |
|
|
public void |
123 |
|
|
channelInfoChanged(ChannelInfoEvent e) { |
124 |
|
|
System.out.println("Channel " + e.getSamplerChannel() + " has changed"); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
/** Invoked when miscellaneous and debugging events occurs. */ |
128 |
|
|
public void |
129 |
|
|
miscEventOccured(MiscellaneousEvent e) { |
130 |
|
|
System.out.println("Miscellaneous event: " + e.getEventMessage()); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
/** |
134 |
|
|
* Invoked when the number of active disk streams for a specific sampler channel is changed. |
135 |
|
|
*/ |
136 |
|
|
public void |
137 |
|
|
streamCountChanged(StreamCountEvent e) { |
138 |
|
|
System.out.println ( |
139 |
|
|
"The number of disk streams on channel " + e.getSamplerChannel() + |
140 |
|
|
" has changed to " + e.getStreamCount() |
141 |
|
|
); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/** Invoked when the number of active voices has changed. */ |
145 |
|
|
public void |
146 |
|
|
voiceCountChanged(VoiceCountEvent e) { |
147 |
|
|
System.out.println ( |
148 |
|
|
"The number of voices on channel " + e.getSamplerChannel() + |
149 |
|
|
" has changed to " + e.getVoiceCount() |
150 |
|
|
); |
151 |
|
|
} |
152 |
|
|
} |