001/* Interface for implementing for implementing LiveSound actor for different 002 platforms 003 004 Copyright (c) 2000-2018 The Regents of the University of California. 005 All rights reserved. 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the above 009 copyright notice and the following two paragraphs appear in all copies 010 of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 028 */ 029 030package ptolemy.media.javasound; 031 032import java.io.IOException; 033 034/////////////////////////////////////////////////////////////////// 035////LiveSoundInterface 036 037/** 038Interface for implementing for implementing LiveSoundActor on different 039platforms. 040 041@author Ishwinder Singh 042@version $Id$ 043@since Ptolemy II 10.0 044@Pt.ProposedRating Yellow (ishwinde) 045@Pt.AcceptedRating Yellow (ishwinde) 046 */ 047 048public interface LiveSoundInterface { 049 050 /** Add a live sound listener. The listener will be notified 051 * of all changes in live audio parameters. If the listener 052 * is already listening, then do nothing. 053 * 054 * @param listener The LiveSoundListener to add. 055 * @see #removeLiveSoundListener(LiveSoundListener) 056 */ 057 public void addLiveSoundListener(LiveSoundListener listener); 058 059 /** Flush queued data from the capture buffer. The flushed data is 060 * discarded. It is only legal to flush the capture buffer after 061 * startCapture() is called. Flushing an active audio buffer is likely to 062 * cause a discontinuity in the data, resulting in a perceptible click. 063 * <p> 064 * Note that only the object with the exclusive lock on the capture audio 065 * resources is allowed to invoke this method. An exception will occur if 066 * the specified object does not have the lock on the playback audio 067 * resources. 068 * 069 * @param consumer The object that has an exclusive lock on 070 * the capture audio resources. 071 * 072 * @exception IllegalStateException If audio capture is currently 073 * inactive. That is, if startCapture() has not yet been called 074 * or if stopCapture() has already been called. 075 * 076 * @exception IOException If the calling program does not have permission 077 * to access the audio capture resources. 078 */ 079 public void flushCaptureBuffer(Object consumer) 080 throws IOException, IllegalStateException; 081 082 /** Flush queued data from the playback buffer. The flushed data is 083 * discarded. It is only legal to flush the playback buffer after 084 * startPlayback() is called, and only makes sense to do so (but is 085 * not required) after putSamples() is called. Flushing an active audio 086 * buffer is likely to cause a discontinuity in the data, resulting in a 087 * perceptible click. 088 * <p> 089 * Note that only the object with the exclusive lock on the playback audio 090 * resources is allowed to invoke this method. An exception will occur if 091 * the specified object does not have the lock on the playback audio 092 * resources. 093 * 094 * @param producer The object that has an exclusive lock on 095 * the playback audio resources. 096 * 097 * @exception IllegalStateException If audio playback is currently 098 * inactive. That is, if startPlayback() has not yet been called 099 * or if stopPlayback() has already been called. 100 * 101 * @exception IOException If the calling program does not have permission 102 * to access the audio playback resources. 103 */ 104 public void flushPlaybackBuffer(Object producer) 105 throws IOException, IllegalStateException; 106 107 /** Return the number of bits per audio sample, which is 108 * set by the setBitsPerSample() method. The default 109 * value of this parameter is 16 bits. 110 * 111 * @return The sample size in bits. 112 * @see #setBitsPerSample(int) 113 */ 114 public int getBitsPerSample(); 115 116 /** Return the suggested size of the internal capture and playback audio 117 * buffers, in samples per channel. This parameter is set by the 118 * setBufferSize() method. There is no guarantee that the value returned 119 * is the actual buffer size used for capture and playback. 120 * Furthermore, the buffers used for capture and playback may have 121 * different sizes. The default value of this parameter is 4096. 122 * 123 * @return The suggested internal buffer size in samples per 124 * channel. 125 * @see #setBufferSize(int) 126 */ 127 public int getBufferSize(); 128 129 /** Return the size of the internal capture audio buffer, in samples per 130 * channel. 131 * 132 * @return The internal buffer size in samples per channel. 133 * 134 * @exception IllegalStateException If audio capture is inactive. 135 */ 136 public int getBufferSizeCapture() throws IllegalStateException; 137 138 /** Return the size of the internal playback audio buffer, in samples per 139 * channel. This may differ from the requested buffer size if the hardware 140 * does not support the requested buffer size. If playback has not 141 * been started, then will simply return the requested buffer size. 142 * @return The internal buffer size in samples per channel. 143 * @exception IllegalStateException If audio playback is inactive. 144 */ 145 public int getBufferSizePlayback(); 146 147 /** Return the number of audio channels, which is set by 148 * the setChannels() method. The default value of this 149 * parameter is 1 (for mono audio). 150 * 151 * @return The number of audio channels. 152 * @see #setChannels(int) 153 */ 154 public int getChannels(); 155 156 /** Return the current sampling rate in Hz, which is set 157 * by the setSampleRate() method. The default value of 158 * this parameter is 8000 Hz. 159 * 160 * @return The sample rate in Hz. 161 * @see #setSampleRate(int) 162 */ 163 public int getSampleRate(); 164 165 /** Return an array of captured audio samples. This method 166 * should be repeatedly called to obtain audio data. 167 * The returned audio samples will have values in the range 168 * [-1, 1], regardless of the audio bit resolution (bits per 169 * sample). This method should be called often enough to 170 * prevent overflow of the internal audio buffer. If 171 * overflow occurs, some audio data will be lost but no 172 * exception or other error condition will occur. If 173 * the audio data is not yet available, then this method 174 * will block until the data is available. 175 * <p> 176 * The first index of the returned array 177 * represents the channel number (0 for first channel, 1 for 178 * second channel). The number of channels is set by the 179 * setChannels() method. The second index represents the 180 * sample index within a channel. For example, 181 * <i>returned array</i>[n][m] contains the (m+1)th sample 182 * of the (n+1)th channel. For each channel, n, the length of 183 * <i>returned array</i>[n] is equal to the value returned by 184 * the getTransferSize() method. 185 * The size of the 2nd dimension of the returned array 186 * is set by the setTransferSize() method. 187 * <p> 188 * Note that only the object with the exclusive lock on 189 * the captured audio resources is allowed to invoked this 190 * method. An exception will occur if the specified object 191 * does not have the lock on the captured audio resources. 192 * 193 * @param consumer The object that has an exclusive lock on 194 * the capture audio resources. 195 * 196 * @return Two dimensional array of captured audio samples. 197 * 198 * @exception IllegalStateException If audio capture is currently 199 * inactive. That is, if startCapture() has not yet been called or if 200 * stopCapture() has already been called. 201 * 202 * @exception IOException If the calling program does not have permission 203 * to access the audio capture resources. 204 */ 205 public double[][] getSamples(Object consumer) 206 throws IOException, IllegalStateException; 207 208 /** Get the array length (in samples per channel) to use 209 * for capturing and playing samples via the putSamples() 210 * and getSamples() methods. This method gets the size 211 * of the 2nd dimension of the 2-dimensional array 212 * used by the putSamples() and getSamples() methods. This 213 * method returns the value that was set by the 214 * setTransferSize(). If setTransferSize() was not invoked, 215 * the default value of 128 is returned. 216 * 217 * @return The size of the 2nd dimension of the 2-dimensional 218 * array used by the putSamples() and getSamples() methods. 219 * @see #setTransferSize(int) 220 */ 221 public int getTransferSize(); 222 223 /** Return true if audio capture is currently active. 224 * Otherwise return false. 225 * 226 * @return True If audio capture is currently active. 227 * Otherwise return false. 228 */ 229 public boolean isCaptureActive(); 230 231 /** Return true if audio playback is currently active. 232 * Otherwise return false. 233 * 234 * @return True If audio playback is currently active. 235 * Otherwise return false. 236 */ 237 public boolean isPlaybackActive(); 238 239 /** Play an array of audio samples. There will be a 240 * delay before the audio data is actually heard, since the 241 * audio data in <i>samplesArray</i> is queued to an 242 * internal audio buffer. The setBufferSize() method suggests a size 243 * for the internal buffer. An upper bound 244 * on the latency is given by (<i>bufferSize</i> / 245 * <i>sampleRate</i>) seconds. This method should be invoked often 246 * enough to prevent underflow of the internal audio buffer. 247 * Underflow is undesirable since it will cause audible gaps 248 * in audio playback, but no exception or error condition will 249 * occur. If the caller attempts to write more data than can 250 * be written, this method blocks until the data can be 251 * written to the internal audio buffer. 252 * Note that only the object with the exclusive lock on 253 * the playback audio resources is allowed to invoke this 254 * method. An exception will occur if the specified object 255 * does not have the lock on the playback audio resources. 256 * 257 * @param producer The object that has an exclusive lock on 258 * the playback audio resources. 259 * 260 * @param samplesArray A two dimensional array containing 261 * the samples to play or write to a file. 262 * 263 * @exception IOException If the calling program does not have permission 264 * to access the audio playback resources. 265 * 266 * @exception IllegalStateException If audio playback is currently 267 * inactive. That is, If startPlayback() has not yet been called 268 * or if stopPlayback() has already been called. 269 */ 270 public void putSamples(Object producer, double[][] samplesArray) 271 throws IOException, IllegalStateException; 272 273 /** Remove a live sound listener. If the listener is 274 * is not listening, then do nothing. 275 * 276 * @param listener The LiveSoundListener to remove. 277 * @see #addLiveSoundListener(LiveSoundListener) 278 */ 279 public void removeLiveSoundListener(LiveSoundListener listener); 280 281 /** Stop audio capture. If audio capture is already inactive, 282 * then do nothing. This method should generally not be used, 283 * but it may be needed to turn of audio capture for the 284 * case where an ill-behaved application exits without calling 285 * stopCapture(). The preferred way of stopping audio capture 286 * is by calling the stopCapture() method. 287 * 288 */ 289 public void resetCapture(); 290 291 /** Stop audio playback. If audio playback is already inactive, 292 * then do nothing. This method should generally not be used, 293 * but it may be needed to turn of audio playback for the 294 * case where an ill-behaved application exits without calling 295 * stopPlayback(). The preferred way of stopping audio playback 296 * is by calling the stopPlayback() method. 297 * 298 */ 299 public void resetPlayback(); 300 301 /** Set the number of bits per sample to use for audio capture 302 * and playback and notify any registered listeners of the change. 303 * Allowable values include 8 and 16 bits. If 304 * this method is not invoked, then the default value of 16 305 * bits is used. 306 * @param bitsPerSample The number of bits per sample. 307 * @exception IOException If the specified bits per sample is 308 * not supported by the audio hardware or by Java. 309 * @see #getBitsPerSample() 310 */ 311 public void setBitsPerSample(int bitsPerSample) throws IOException; 312 313 /** Request that the internal capture and playback 314 * audio buffers have bufferSize samples per channel and notify the 315 * registered listeners of the change. If this method 316 * is not invoked, the default value of 1024 is used. 317 * 318 * @param bufferSize The suggested size of the internal capture and 319 * playback audio buffers, in samples per channel. 320 * @exception IOException If the specified number of channels is 321 * not supported by the audio hardware or by Java. 322 * @see #getBufferSize() 323 */ 324 public void setBufferSize(int bufferSize) throws IOException; 325 326 /** Set the number of audio channels to use for capture and 327 * playback and notify any registered listeners of the change. 328 * Allowable values are 1 (for mono) and 2 (for 329 * stereo). If this method is not invoked, the default 330 * value of 1 audio channel is used. Note that this method 331 * sets the size of the first dimension of the 332 * 2-dimensional array used by the putSamples() and 333 * getSamples() methods. 334 * 335 * @param channels The number audio channels. 336 * 337 * @exception IOException If the specified number of channels is 338 * not supported by the audio hardware or by Java. 339 * @see #getChannels() 340 */ 341 public void setChannels(int channels) throws IOException; 342 343 /** Set the sample rate to use for audio capture and playback 344 * and notify an registered listeners of the change. 345 * Allowable values for this parameter are 8000, 11025, 346 * 22050, 44100, and 48000 Hz. If this method is not invoked, 347 * then the default value of 8000 Hz is used. 348 * 349 * @param sampleRate Sample rate in Hz. 350 * 351 * @exception IOException If the specified sample rate is 352 * not supported by the audio hardware or by Java. 353 * @see #getSampleRate() 354 */ 355 public void setSampleRate(int sampleRate) throws IOException; 356 357 /** Set the array length (in samples per channel) to use 358 * for capturing and playing samples via the putSamples() 359 * and getSamples() methods. This method sets the size 360 * of the 2nd dimension of the 2-dimensional array 361 * used by the putSamples() and getSamples() methods. If 362 * this method is not invoked, the default value of 128 is 363 * used. 364 * <p> 365 * This method should only be called while audio capture and 366 * playback are inactive. Otherwise an exception will occur. 367 * 368 * @param transferSize The size of the 2nd dimension of 369 * the 2-dimensional array used by the putSamples() and 370 * getSamples() methods 371 * 372 * @exception IllegalStateException If this method is called 373 * while audio capture or playback are active. 374 * @see #getTransferSize() 375 */ 376 public void setTransferSize(int transferSize) throws IllegalStateException; 377 378 /** Start audio capture. The specified object will be 379 * given an exclusive lock on the audio capture resources 380 * until the stopCapture() method is called with the 381 * same object reference. After this method returns, 382 * the getSamples() method may be repeatedly invoked 383 * (using the object reference as a parameter) to 384 * capture audio. 385 * <p> 386 * If audio capture is already active, then an 387 * exception will occur. 388 * 389 * @param consumer The object to be given exclusive access 390 * to the captured audio resources. 391 * 392 * @exception IOException If another object currently has access 393 * to the audio capture resources or if starting the capture or 394 * playback throws it. 395 * 396 * @exception IllegalStateException If this method is called 397 * while audio capture is already active. 398 */ 399 public void startCapture(Object consumer) 400 throws IOException, IllegalStateException; 401 402 /** Start audio playback. The specified object will be 403 * given an exclusive lock on the audio playback resources 404 * until the stopPlayback() method is called with the 405 * same object reference. After this method returns, 406 * the putSamples() method may be repeatedly invoked 407 * (using the object reference as a parameter) to 408 * playback audio. 409 * <p> 410 * If audio playback is already active, then an 411 * exception will occur. 412 * 413 * @param producer The object to be given exclusive access 414 * to the playback resources. 415 * 416 * @exception IOException If another object currently has access 417 * to the audio capture resources or if starting the playback throws it. 418 * 419 * @exception IllegalStateException If this method is called 420 * while audio playback is already active. 421 */ 422 public void startPlayback(Object producer) 423 throws IOException, IllegalStateException; 424 425 /** Stop audio capture. If the specified object has 426 * the lock on audio capture when this method is 427 * invoked, then stop audio capture. Otherwise 428 * an exception will occur. 429 * 430 * @param consumer The object that held on exclusive 431 * lock on the captured audio resources when this 432 * method was invoked. 433 * 434 * @exception IOException If another object currently has access 435 * to the audio capture resources or if stopping the capture throws it. 436 * 437 * @exception IllegalStateException If the specified 438 * object did not hold an exclusive lock on the 439 * captured audio resources when this method was invoked. 440 */ 441 public void stopCapture(Object consumer) 442 throws IOException, IllegalStateException; 443 444 /** Stop audio playback. If the specified object has 445 * the lock on audio playback when this method is 446 * invoked, then stop audio playback. Otherwise 447 * an exception will occur. 448 * 449 * @param producer The object that held on exclusive 450 * lock on the playback audio resources when this 451 * method was invoked. 452 * 453 * @exception IOException If another object currently has access 454 * to the audio capture resources or if stopping the playback throws it. 455 456 * @exception IllegalStateException If the specified 457 * object did not hold an exclusive lock on the 458 * playback audio resources when this method was invoked. 459 * 460 */ 461 public void stopPlayback(Object producer) 462 throws IOException, IllegalStateException; 463 464}