PortAudio 2.0

com_portaudio_BlockingStream.c

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 
00039 #include "com_portaudio_BlockingStream.h"
00040 #include "portaudio.h"
00041 #include "jpa_tools.h"
00042 
00043 #ifndef FALSE
00044 #define FALSE  (0)
00045 #endif
00046 #ifndef TRUE
00047 #define TRUE  (!FALSE)
00048 #endif
00049 
00050 /*
00051  * Class:     com_portaudio_BlockingStream
00052  * Method:    getReadAvailable
00053  * Signature: ()I
00054  */
00055 JNIEXPORT jint JNICALL Java_com_portaudio_BlockingStream_getReadAvailable
00056   (JNIEnv *env, jobject blockingStream)
00057 {
00058         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00059         if( stream == NULL ) return 0;
00060         return Pa_GetStreamReadAvailable( stream );
00061 }
00062 
00063 /*
00064  * Class:     com_portaudio_BlockingStream
00065  * Method:    getWriteAvailable
00066  * Signature: ()I
00067  */
00068 JNIEXPORT jint JNICALL Java_com_portaudio_BlockingStream_getWriteAvailable
00069   (JNIEnv *env, jobject blockingStream)
00070 {
00071         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00072         if( stream == NULL ) return 0;
00073         return Pa_GetStreamWriteAvailable( stream );
00074 }
00075 
00076 
00077 /*
00078  * Class:     com_portaudio_BlockingStream
00079  * Method:    writeFloats
00080  * Signature: ([FI)Z
00081  */
00082 JNIEXPORT jboolean JNICALL Java_com_portaudio_BlockingStream_writeFloats
00083   (JNIEnv *env, jobject blockingStream, jfloatArray buffer, jint numFrames)
00084 {
00085         jfloat *carr;
00086         jint err;
00087         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00088         if( buffer == NULL )
00089         {
00090                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00091                   "null stream buffer");
00092                 return FALSE;
00093         }
00094         carr = (*env)->GetFloatArrayElements(env, buffer, NULL);
00095         if (carr == NULL)
00096         {
00097                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00098                   "invalid stream buffer");
00099                 return FALSE;
00100         }
00101         err = Pa_WriteStream( stream, carr, numFrames );
00102         (*env)->ReleaseFloatArrayElements(env, buffer, carr, 0);
00103         if( err == paOutputUnderflowed )
00104         {
00105                 return TRUE;
00106         }
00107         else
00108         {
00109                 jpa_CheckError( env, err );
00110                 return FALSE;
00111         }
00112 }
00113 
00114 /*
00115  * Class:     com_portaudio_BlockingStream
00116  * Method:    readFloats
00117  * Signature: ([FI)Z
00118  */
00119 JNIEXPORT jboolean JNICALL Java_com_portaudio_BlockingStream_readFloats
00120   (JNIEnv *env, jobject blockingStream, jfloatArray buffer, jint numFrames)
00121 {
00122         jfloat *carr;
00123         jint err;
00124         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00125         if( buffer == NULL )
00126         {
00127                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00128                   "null stream buffer");
00129                 return FALSE;
00130         }
00131         carr = (*env)->GetFloatArrayElements(env, buffer, NULL);
00132         if (carr == NULL)
00133         {
00134                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00135                   "invalid stream buffer");
00136                 return FALSE;
00137         }
00138         err = Pa_ReadStream( stream, carr, numFrames );
00139         (*env)->ReleaseFloatArrayElements(env, buffer, carr, 0);
00140         if( err == paInputOverflowed )
00141         {
00142                 return TRUE;
00143         }
00144         else
00145         {
00146                 jpa_CheckError( env, err );
00147                 return FALSE;
00148         }
00149 }
00150 
00151 /*
00152  * Class:     com_portaudio_BlockingStream
00153  * Method:    writeShorts
00154  * Signature: ([SI)Z
00155  */
00156 JNIEXPORT jboolean JNICALL Java_com_portaudio_BlockingStream_writeShorts
00157   (JNIEnv *env, jobject blockingStream, jfloatArray buffer, jint numFrames)
00158 {
00159         jshort *carr;
00160         jint err;
00161         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00162         if( buffer == NULL )
00163         {
00164                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00165                   "null stream buffer");
00166                 return FALSE;
00167         }
00168         carr = (*env)->GetShortArrayElements(env, buffer, NULL);
00169         if (carr == NULL)
00170         {
00171                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00172                   "invalid stream buffer");
00173                 return FALSE;
00174         }
00175         err = Pa_WriteStream( stream, carr, numFrames );
00176         (*env)->ReleaseShortArrayElements(env, buffer, carr, 0);
00177         if( err == paOutputUnderflowed )
00178         {
00179                 return TRUE;
00180         }
00181         else
00182         {
00183                 jpa_CheckError( env, err );
00184                 return FALSE;
00185         }
00186 }
00187 
00188 /*
00189  * Class:     com_portaudio_BlockingStream
00190  * Method:    readShorts
00191  * Signature: ([SI)Z
00192  */
00193 JNIEXPORT jboolean JNICALL Java_com_portaudio_BlockingStream_readShorts
00194   (JNIEnv *env, jobject blockingStream, jfloatArray buffer, jint numFrames)
00195 {
00196         jshort *carr;
00197         jint err;
00198         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00199         if( buffer == NULL )
00200         {
00201                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00202                   "null stream buffer");
00203                 return FALSE;
00204         }
00205         carr = (*env)->GetShortArrayElements(env, buffer, NULL);
00206         if (carr == NULL)
00207         {
00208                 (*env)->ThrowNew( env, (*env)->FindClass(env,"java/lang/RuntimeException"),
00209                   "invalid stream buffer");
00210                 return FALSE;
00211         }
00212         err = Pa_ReadStream( stream, carr, numFrames );
00213         (*env)->ReleaseShortArrayElements(env, buffer, carr, 0);
00214         if( err == paInputOverflowed )
00215         {
00216                 return TRUE;
00217         }
00218         else
00219         {
00220                 jpa_CheckError( env, err );
00221                 return FALSE;
00222         }
00223 }
00224 
00225 /*
00226  * Class:     com_portaudio_BlockingStream
00227  * Method:    start
00228  * Signature: ()V
00229  */
00230 JNIEXPORT void JNICALL Java_com_portaudio_BlockingStream_start
00231   (JNIEnv *env, jobject blockingStream )
00232 {
00233         PaStream *stream = jpa_GetStreamPointer( env, blockingStream );
00234         int err = Pa_StartStream( stream );
00235         jpa_CheckError( env, err );
00236 }
00237 
00238 /*
00239  * Class:     com_portaudio_BlockingStream
00240  * Method:    stop
00241  * Signature: ()V
00242  */
00243 JNIEXPORT void JNICALL Java_com_portaudio_BlockingStream_stop
00244   (JNIEnv *env, jobject blockingStream )
00245 {
00246         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00247         int err = Pa_StopStream( stream );
00248         jpa_CheckError( env, err );
00249 }
00250 /*
00251  * Class:     com_portaudio_BlockingStream
00252  * Method:    abort
00253  * Signature: ()V
00254  */
00255 JNIEXPORT void JNICALL Java_com_portaudio_BlockingStream_abort
00256   (JNIEnv *env, jobject blockingStream )
00257 {
00258         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00259         int err = Pa_AbortStream( stream );
00260         jpa_CheckError( env, err );
00261 }
00262 
00263 /*
00264  * Class:     com_portaudio_BlockingStream
00265  * Method:    close
00266  * Signature: ()V
00267  */
00268 JNIEXPORT void JNICALL Java_com_portaudio_BlockingStream_close
00269   (JNIEnv *env, jobject blockingStream )
00270 {
00271         jclass cls;
00272         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00273         if( stream != NULL )
00274         {
00275                 int err = Pa_CloseStream( stream );
00276                 jpa_CheckError( env, err );
00277                 cls = (*env)->GetObjectClass(env, blockingStream);
00278                 jpa_SetLongField( env, cls, blockingStream, "nativeStream", (jlong) 0 );
00279         }
00280 }
00281 
00282 /*
00283  * Class:     com_portaudio_BlockingStream
00284  * Method:    isStopped
00285  * Signature: ()V
00286  */
00287 JNIEXPORT jboolean JNICALL Java_com_portaudio_BlockingStream_isStopped
00288   (JNIEnv *env, jobject blockingStream )
00289 {
00290         int err;
00291         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00292         if( stream == NULL ) return 1;
00293         err = Pa_IsStreamStopped( stream );
00294         return (jpa_CheckError( env, err ) > 0);
00295 }
00296 /*
00297  * Class:     com_portaudio_BlockingStream
00298  * Method:    isActive
00299  * Signature: ()V
00300  */
00301 JNIEXPORT jboolean JNICALL Java_com_portaudio_BlockingStream_isActive
00302   (JNIEnv *env, jobject blockingStream )
00303 {
00304         int err;
00305         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00306         if( stream == NULL ) return 0;
00307         err = Pa_IsStreamActive( stream );
00308         return (jpa_CheckError( env, err ) > 0);
00309 }
00310 
00311 
00312 /*
00313  * Class:     com_portaudio_BlockingStream
00314  * Method:    getTime
00315  * Signature: ()D
00316  */
00317 JNIEXPORT jdouble JNICALL Java_com_portaudio_BlockingStream_getTime
00318   (JNIEnv *env, jobject blockingStream )
00319 {
00320         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00321         if( stream == NULL ) return 0.0;
00322         return Pa_GetStreamTime( stream );
00323 }
00324 
00325 
00326 /*
00327  * Class:     com_portaudio_BlockingStream
00328  * Method:    getInfo
00329  * Signature: ()Lcom/portaudio/StreamInfo;
00330  */
00331 JNIEXPORT void JNICALL Java_com_portaudio_BlockingStream_getInfo
00332   (JNIEnv *env, jobject blockingStream, jobject streamInfo)
00333 {
00334         
00335         PaStream *stream =jpa_GetStreamPointer( env, blockingStream );
00336         const PaStreamInfo *info = Pa_GetStreamInfo( stream );
00337         if( streamInfo == NULL )
00338         {
00339                 jpa_ThrowError( env, "Invalid stream." );
00340         }
00341         else
00342         {
00343                 /* Get a reference to obj's class */
00344                 jclass cls = (*env)->GetObjectClass(env, streamInfo);
00345  
00346                 jpa_SetIntField( env, cls, streamInfo, "structVersion", info->structVersion );
00347                 jpa_SetDoubleField( env, cls, streamInfo, "inputLatency", info->inputLatency );
00348                 jpa_SetDoubleField( env, cls, streamInfo, "outputLatency", info->outputLatency );
00349                 jpa_SetDoubleField( env, cls, streamInfo, "sampleRate", info->sampleRate );
00350         }
00351 }
00352