jpa_tools.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_PortAudio.h"
00040 #include "portaudio.h"
00041 #include "jpa_tools.h"
00042 
00043 jint jpa_GetIntField( JNIEnv *env, jclass cls, jobject obj, const char *fieldName )
00044 {
00045      /* Look for the instance field maxInputChannels in cls */
00046      jfieldID fid = (*env)->GetFieldID(env, cls, fieldName, "I");
00047      if (fid == NULL)
00048          {
00049                  jpa_ThrowError( env, "Cannot find integer JNI field." );
00050                 return 0;
00051      }
00052          else
00053          {
00054                 return (*env)->GetIntField(env, obj, fid );
00055          }
00056 }
00057 
00058 void jpa_SetIntField( JNIEnv *env, jclass cls, jobject obj, const char *fieldName, jint value )
00059 {
00060      /* Look for the instance field maxInputChannels in cls */
00061      jfieldID fid = (*env)->GetFieldID(env, cls, fieldName, "I");
00062      if (fid == NULL)
00063          {
00064                  jpa_ThrowError( env, "Cannot find integer JNI field." );
00065      }
00066          else
00067          {
00068                 (*env)->SetIntField(env, obj, fid, value );
00069          }
00070 }
00071 
00072 jlong jpa_GetLongField( JNIEnv *env, jclass cls, jobject obj, const char *fieldName )
00073 {
00074      /* Look for the instance field maxInputChannels in cls */
00075      jfieldID fid = (*env)->GetFieldID(env, cls, fieldName, "J");
00076      if (fid == NULL)
00077          {
00078                  jpa_ThrowError( env, "Cannot find long JNI field." );
00079                 return 0L;
00080      }
00081          else
00082          {
00083                 return (*env)->GetLongField(env, obj, fid );
00084          }
00085 }
00086 
00087 void jpa_SetLongField( JNIEnv *env, jclass cls, jobject obj, const char *fieldName, jlong value )
00088 {
00089      /* Look for the instance field maxInputChannels in cls */
00090      jfieldID fid = (*env)->GetFieldID(env, cls, fieldName, "J");
00091      if (fid == NULL)
00092          {
00093                  jpa_ThrowError( env, "Cannot find long JNI field." );
00094      }
00095          else
00096          {
00097                 (*env)->SetLongField(env, obj, fid, value );
00098          }
00099 }
00100 
00101 
00102 void jpa_SetDoubleField( JNIEnv *env, jclass cls, jobject obj, const char *fieldName, jdouble value )
00103 {
00104      /* Look for the instance field maxInputChannels in cls */
00105      jfieldID fid = (*env)->GetFieldID(env, cls, fieldName, "D");
00106      if (fid == NULL)
00107          {
00108                  jpa_ThrowError( env, "Cannot find double JNI field." );
00109      }
00110          else
00111          {
00112                 (*env)->SetDoubleField(env, obj, fid, value );
00113          }
00114 }
00115 
00116 
00117 jdouble jpa_GetDoubleField( JNIEnv *env, jclass cls, jobject obj, const char *fieldName )
00118 {
00119      /* Look for the instance field maxInputChannels in cls */
00120      jfieldID fid = (*env)->GetFieldID(env, cls, fieldName, "D");
00121      if (fid == NULL)
00122          {
00123                  jpa_ThrowError( env, "Cannot find double JNI field." );
00124                 return 0;
00125      }
00126          else
00127          {
00128                 return (*env)->GetDoubleField(env, obj, fid );
00129          }
00130 }
00131 
00132 void jpa_SetStringField( JNIEnv *env, jclass cls, jobject obj, const char *fieldName, const char *value )
00133 {
00134      /* Look for the instance field maxInputChannels in cls */
00135      jfieldID fid = (*env)->GetFieldID(env, cls, fieldName, "Ljava/lang/String;");
00136      if (fid == NULL)
00137          {
00138                  jpa_ThrowError( env, "Cannot find String JNI field." );
00139      }
00140          else
00141          {
00142                 jstring jstr = (*env)->NewStringUTF(env, value);
00143                 if (jstr == NULL)
00144                 {
00145                         jpa_ThrowError( env, "Cannot create new String." );
00146                 }
00147                 else
00148                 {
00149                         (*env)->SetObjectField(env, obj, fid, jstr );
00150                 }
00151          }
00152 }
00153 
00154 PaStreamParameters *jpa_FillStreamParameters( JNIEnv *env, jobject jstreamParam, PaStreamParameters *myParams )
00155 {
00156         jclass cls;
00157         
00158         if( jstreamParam == NULL ) return NULL; // OK, not an error
00159 
00160         cls = (*env)->GetObjectClass(env, jstreamParam);
00161         
00162         myParams->channelCount = jpa_GetIntField( env, cls, jstreamParam, "channelCount" );
00163         myParams->device = jpa_GetIntField( env, cls, jstreamParam, "device" );
00164         myParams->sampleFormat = jpa_GetIntField( env, cls, jstreamParam, "sampleFormat" );
00165         myParams->suggestedLatency = jpa_GetDoubleField( env, cls, jstreamParam, "suggestedLatency" );
00166         myParams->hostApiSpecificStreamInfo = NULL;
00167 
00168         return myParams;
00169 }
00170 
00171 // Create an exception that will be thrown when we return from the JNI call.
00172 jint jpa_ThrowError( JNIEnv *env, const char *message )
00173 {
00174         return (*env)->ThrowNew(env, (*env)->FindClass( env, "java/lang/RuntimeException"),
00175                   message );
00176 }
00177 
00178 // Throw an exception on error.
00179 jint jpa_CheckError( JNIEnv *env, PaError err )
00180 {
00181         if( err == -1 )
00182         {
00183         return jpa_ThrowError( env, "-1, possibly no available default device" );
00184     }
00185     else if( err < 0 )
00186     {
00187                 if( err == paUnanticipatedHostError )
00188                 {
00189                         const PaHostErrorInfo *hostErrorInfo = Pa_GetLastHostErrorInfo();
00190                         return jpa_ThrowError( env, hostErrorInfo->errorText );
00191                 }
00192                 else
00193                 {
00194                         return jpa_ThrowError( env, Pa_GetErrorText( err ) );
00195                 }
00196         }
00197         else
00198         {
00199                 return err;
00200         }
00201 }
00202 
00203 // Get the stream pointer from a BlockingStream long field.
00204 PaStream *jpa_GetStreamPointer( JNIEnv *env, jobject blockingStream )
00205 {
00206         jclass cls = (*env)->GetObjectClass(env, blockingStream);
00207         return (PaStream *) jpa_GetLongField( env, cls, blockingStream, "nativeStream" );
00208 }

Generated on Sat Aug 6 19:33:25 2016 for PortAudio by  doxygen 1.5.6