/[svn]/jlscp/trunk/examples/Events.java
ViewVC logotype

Contents of /jlscp/trunk/examples/Events.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 671 - (show annotations) (download)
Wed Jun 22 06:18:33 2005 UTC (18 years, 9 months ago) by iliev
File size: 4249 byte(s)
Updating to version 0.2a

1 /*
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 try {
45 // Listeners can be added or removed regardless of the connection state.
46 client.addBufferFillListener(myListener);
47 client.addChannelCountListener(myListener);
48 client.addChannelInfoListener(myListener);
49 client.addMiscellaneousListener(myListener);
50
51 client.connect();
52
53 client.addStreamCountListener(myListener);
54 client.addVoiceCountListener(myListener);
55
56 System.out.println("Press 'Enter' to exit");
57 System.in.read();
58
59 client.removeBufferFillListener(myListener);
60 client.removeChannelCountListener(myListener);
61 client.removeChannelInfoListener(myListener);
62
63 client.disconnect();
64
65 client.removeMiscellaneousListener(myListener);
66 client.removeStreamCountListener(myListener);
67 client.removeVoiceCountListener(myListener);
68
69 System.exit(0);
70 } catch(Exception x) {
71 x.printStackTrace();
72 }
73
74 System.exit(-1);
75 }
76
77 private static void
78 initLogger() {
79 final Handler handler = new StreamHandler(System.out, new SimpleFormatter());
80 Logger.getLogger("org.linuxsampler.lscp").addHandler(handler);
81
82 // Flushing logs on every second
83 new java.util.Timer().schedule(new java.util.TimerTask() {
84 public void
85 run() { handler.flush(); }
86 }, 1000, 1000);
87 }
88 }
89
90 class MyListener implements
91 BufferFillListener,
92 ChannelCountListener,
93 ChannelInfoListener,
94 MiscellaneousListener,
95 StreamCountListener,
96 VoiceCountListener {
97
98 /**
99 * Invoked when the fill state of a disk stream buffer has changed.
100 */
101 public void
102 bufferFillChanged(BufferFillEvent e) {
103 System.out.println (
104 "Buffer fill event occured on channel " +
105 e.getSamplerChannel()
106 );
107 for(BufferFill bf : e.getChannelBufferFillPercentage()) {
108 System.out.println (
109 "\tStream ID " + bf.getStreamID() + ": " + bf.getValue() + "%"
110 );
111 }
112 }
113
114 /** Invoked when the number of channels is changed. */
115 public void
116 channelCountChanged(ChannelCountEvent e) {
117 System.out.println("The number of channels has changed to " + e.getChannelCount());
118 }
119
120 /** Invoked when changes to the sampler channel has occured. */
121 public void
122 channelInfoChanged(ChannelInfoEvent e) {
123 System.out.println("Channel " + e.getSamplerChannel() + " has changed");
124 }
125
126 /** Invoked when miscellaneous and debugging events occurs. */
127 public void
128 miscEventOccured(MiscellaneousEvent e) {
129 System.out.println("Miscellaneous event: " + e.getEventMessage());
130 }
131
132 /**
133 * Invoked when the number of active disk streams for a specific sampler channel is changed.
134 */
135 public void
136 streamCountChanged(StreamCountEvent e) {
137 System.out.println (
138 "The number of disk streams on channel " + e.getSamplerChannel() +
139 " has changed to " + e.getStreamCount()
140 );
141 }
142
143 /** Invoked when the number of active voices has changed. */
144 public void
145 voiceCountChanged(VoiceCountEvent e) {
146 System.out.println (
147 "The number of voices on channel " + e.getSamplerChannel() +
148 " has changed to " + e.getVoiceCount()
149 );
150 }
151 }

  ViewVC Help
Powered by ViewVC