PortAudio 2.0
|
00001 /* 00002 * Portable Audio I/O Library 00003 * Java Binding for PortAudio 00004 * 00005 * Based on the Open Source API proposed by Ross Bencina 00006 * Copyright (c) 2008 Ross Bencina 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining 00009 * a copy of this software and associated documentation files 00010 * (the "Software"), to deal in the Software without restriction, 00011 * including without limitation the rights to use, copy, modify, merge, 00012 * publish, distribute, sublicense, and/or sell copies of the Software, 00013 * and to permit persons to whom the Software is furnished to do so, 00014 * subject to the following conditions: 00015 * 00016 * The above copyright notice and this permission notice shall be 00017 * included in all copies or substantial portions of the Software. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00020 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00021 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00022 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00023 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00024 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00025 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00026 */ 00027 00028 /* 00029 * The text above constitutes the entire PortAudio license; however, 00030 * the PortAudio community also makes the following non-binding requests: 00031 * 00032 * Any person wishing to distribute modifications to the Software is 00033 * requested to send the modifications to the original developer so that 00034 * they can be incorporated into the canonical version. It is also 00035 * requested that these non-binding requests be included along with the 00036 * license above. 00037 */ 00038 00044 package com.portaudio; 00045 00059 public class BlockingStream 00060 { 00061 // nativeStream is only accessed by the native code. It contains a pointer 00062 // to a PaStream. 00063 private long nativeStream; 00064 private int inputFormat = -1; 00065 private int outputFormat = -1; 00066 00067 protected BlockingStream() 00068 { 00069 } 00070 00074 public native int getReadAvailable(); 00075 00079 public native int getWriteAvailable(); 00080 00081 private native boolean readFloats( float[] buffer, int numFrames ); 00082 00083 private native boolean writeFloats( float[] buffer, int numFrames ); 00084 00093 public boolean read( float[] buffer, int numFrames ) 00094 { 00095 if( inputFormat != PortAudio.FORMAT_FLOAT_32 ) 00096 { 00097 throw new RuntimeException( 00098 "Tried to read float samples from a non float stream." ); 00099 } 00100 return readFloats( buffer, numFrames ); 00101 } 00102 00112 public boolean write( float[] buffer, int numFrames ) 00113 { 00114 if( outputFormat != PortAudio.FORMAT_FLOAT_32 ) 00115 { 00116 throw new RuntimeException( 00117 "Tried to write float samples to a non float stream." ); 00118 } 00119 return writeFloats( buffer, numFrames ); 00120 } 00121 00122 private native boolean readShorts( short[] buffer, int numFrames ); 00123 00124 private native boolean writeShorts( short[] buffer, int numFrames ); 00125 00134 public boolean read( short[] buffer, int numFrames ) 00135 { 00136 if( inputFormat != PortAudio.FORMAT_INT_16 ) 00137 { 00138 throw new RuntimeException( 00139 "Tried to read short samples from a non short stream." ); 00140 } 00141 return readShorts( buffer, numFrames ); 00142 } 00143 00152 public boolean write( short[] buffer, int numFrames ) 00153 { 00154 if( outputFormat != PortAudio.FORMAT_INT_16 ) 00155 { 00156 throw new RuntimeException( 00157 "Tried to write short samples from a non short stream." ); 00158 } 00159 return writeShorts( buffer, numFrames ); 00160 } 00161 00165 public native void start(); 00166 00171 public native void stop(); 00172 00176 public native void abort(); 00177 00182 public native void close(); 00183 00184 public native boolean isStopped(); 00185 00186 public native boolean isActive(); 00187 00188 public String toString() 00189 { 00190 return "BlockingStream: streamPtr = " + Long.toHexString( nativeStream ) 00191 + ", inFormat = " + inputFormat + ", outFormat = " 00192 + outputFormat; 00193 } 00194 00198 public native double getTime(); 00199 00200 private native void getInfo( StreamInfo streamInfo ); 00201 00202 public StreamInfo getInfo() 00203 { 00204 StreamInfo streamInfo = new StreamInfo(); 00205 getInfo( streamInfo ); 00206 return streamInfo; 00207 } 00208 }