23#include "com_tagtraum_casampledsp_CAAudioFileReader.h"
32static jobject create_CoreAudioAudioFileFormat_object(JNIEnv *env, jstring file, jint dataFormat, jfloat sampleRate, jint sampleSize,
33 jint channels, jint frameSize, jfloat frameRate, jint frameLength,
34 jboolean bigEndian, jlong duration,
35 jint bitRate, jboolean vbr) {
36 jclass coreAudioAudioFileFormatClass;
38 jobject result = NULL;
40 coreAudioAudioFileFormatClass = env->FindClass(
"com/tagtraum/casampledsp/CAAudioFileFormat");
41 if (coreAudioAudioFileFormatClass == NULL) {
46 cid = env->GetMethodID(coreAudioAudioFileFormatClass,
"<init>",
"(Ljava/lang/String;IFIIIFIZJIZ)V");
52 result = env->NewObject(coreAudioAudioFileFormatClass, cid, file, dataFormat, sampleRate, sampleSize, channels,
53 frameSize, frameRate, frameLength, bigEndian, duration, bitRate, vbr);
56 env->DeleteLocalRef(coreAudioAudioFileFormatClass);
69 (JNIEnv *env, jobject instance, jstring url) {
72 AudioFileID infile = NULL;
73 AudioStreamBasicDescription inputFormat;
74 jobject audioFormat = NULL;
75 jlong durationInMicroSeconds;
77 jboolean vbr = JNI_FALSE;
79 jboolean bigEndian = JNI_TRUE;
82 UInt64 dataPacketCount;
83 UInt64 frameLength = 0;
84 const Float64 bitsPerByte = 8.;
87 if (inputURLRef == NULL) {
92 res = AudioFileOpenURL(inputURLRef, 0x01, 0, &infile);
103 size =
sizeof(inputFormat);
104 res = AudioFileGetProperty(infile, kAudioFilePropertyDataFormat, &size, &inputFormat);
110 size =
sizeof(dataPacketCount);
111 res = AudioFileGetProperty(infile, kAudioFilePropertyAudioDataPacketCount, &size, &dataPacketCount);
117 frameLength = dataPacketCount;
118 frameRate = (jfloat)(inputFormat.mSampleRate/inputFormat.mFramesPerPacket);
119 durationInMicroSeconds = (jlong)((inputFormat.mFramesPerPacket * dataPacketCount * 1000L * 1000L) / inputFormat.mSampleRate);
121 if (inputFormat.mBytesPerPacket && inputFormat.mFramesPerPacket) {
122 bitRate = (jint)(bitsPerByte * (Float64)inputFormat.mBytesPerPacket * inputFormat.mSampleRate / (Float64)inputFormat.mFramesPerPacket);
128 bigEndian = (inputFormat.mFormatID == kAudioFormatLinearPCM) && ((kAudioFormatFlagIsBigEndian & inputFormat.mFormatFlags) == kAudioFormatFlagIsBigEndian);
130 fprintf(stderr,
"dataPackets: %llu\n", dataPacketCount);
131 fprintf(stderr,
"Frames/Pckt: %llu\n", inputFormat.mFramesPerPacket);
132 fprintf(stderr,
"sampleRate : %f\n", inputFormat.mSampleRate);
133 fprintf(stderr,
"sampleSize : %i\n", inputFormat.mBitsPerChannel);
134 fprintf(stderr,
"channels : %i\n", inputFormat.mChannelsPerFrame);
135 fprintf(stderr,
"packetSize : %i\n", inputFormat.mBytesPerFrame);
136 fprintf(stderr,
"dataFormat : %i\n", inputFormat.mFormatID);
137 fprintf(stderr,
"duration : %ld\n", durationInMicroSeconds);
138 fprintf(stderr,
"frameRate : %f\n", frameRate);
139 fprintf(stderr,
"frameLength: %i\n", frameLength);
141 fprintf(stderr,
"bigEndian : true\n");
143 fprintf(stderr,
"bigEndian : false\n");
146 audioFormat = create_CoreAudioAudioFileFormat_object(env, url,
147 inputFormat.mFormatID,
148 inputFormat.mSampleRate,
149 inputFormat.mBitsPerChannel,
150 inputFormat.mChannelsPerFrame,
151 inputFormat.mBytesPerFrame,
155 durationInMicroSeconds,
159 if (infile != NULL) {
160 AudioFileClose(infile);
170static void CAAudioFileReader_PacketsProc (
172 UInt32 inNumberBytes,
173 UInt32 inNumberPackets,
174 const void *inInputData,
175 AudioStreamPacketDescription *inPacketDescriptions
178 fprintf(stderr,
"CAAudioFileReader_PacketsProc\n");
186static void CAAudioFileReader_PropertyListenerProc (
188 AudioFileStreamID inAudioFileStream,
189 AudioFileStreamPropertyID inPropertyID,
193 fprintf(stderr,
"CAAudioFileReader_PropertyListenerProc\n");
194 fprintf(stderr,
"AudioFileStreamPropertyID %i\n", inPropertyID);
209 (JNIEnv *env, jobject instance, jbyteArray byteArray, jint length, jint hint) {
211 AudioFileStreamID stream = NULL;
212 jobject audioFormat = NULL;
214 AudioStreamBasicDescription inputFormat;
216 jlong durationInMicroSeconds = -1;
217 jfloat frameRate = -1;
218 UInt64 dataPacketCount = -1;
219 UInt64 frameLength = -1;
220 jboolean bigEndian = JNI_TRUE;
222 inBuf = env->GetByteArrayElements(byteArray, NULL);
223 res = AudioFileStreamOpen(inBuf, CAAudioFileReader_PropertyListenerProc, CAAudioFileReader_PacketsProc, hint, &stream);
229 res = AudioFileStreamParseBytes(stream, length, inBuf, kAudioFileStreamPropertyFlag_CacheProperty);
237 size =
sizeof(inputFormat);
238 res = AudioFileStreamGetProperty(stream, kAudioFileStreamProperty_DataFormat, &size, &inputFormat);
244 size =
sizeof(dataPacketCount);
245 res = AudioFileStreamGetProperty(stream, kAudioFileStreamProperty_AudioDataPacketCount, &size, &dataPacketCount);
247 frameLength = dataPacketCount;
248 durationInMicroSeconds = (jlong)((inputFormat.mFramesPerPacket * dataPacketCount * 1000L * 1000L) / inputFormat.mSampleRate);
249 frameRate = (jfloat)(dataPacketCount*inputFormat.mSampleRate/frameLength);
256 bigEndian = (inputFormat.mFormatID == kAudioFormatLinearPCM) && ((kAudioFormatFlagIsBigEndian & inputFormat.mFormatFlags) == kAudioFormatFlagIsBigEndian);
257 audioFormat = create_CoreAudioAudioFileFormat_object(env, NULL,
258 inputFormat.mFormatID,
259 inputFormat.mSampleRate,
260 inputFormat.mBitsPerChannel,
261 inputFormat.mChannelsPerFrame,
262 inputFormat.mBytesPerFrame,
266 durationInMicroSeconds,
273 env->ReleaseByteArrayElements(byteArray, inBuf, JNI_ABORT);
275 if (stream != NULL) {
276 AudioFileStreamClose(stream);
JNIEXPORT jobject JNICALL Java_com_tagtraum_casampledsp_CAAudioFileReader_intGetAudioFormat___3BII(JNIEnv *env, jobject instance, jbyteArray byteArray, jint length, jint hint)
Creates a CoreAudioFileFormat for the first X bytes of a stream.
JNIEXPORT jobject JNICALL Java_com_tagtraum_casampledsp_CAAudioFileReader_intGetAudioFormat__Ljava_lang_String_2(JNIEnv *env, jobject instance, jstring url)
Creates a CoreAudioFileFormat for a URL.
void throwFileNotFoundExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IllegalArgumentException.
void throwUnsupportedAudioFileExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an UnsupportedAudioFileException exception.
void ca_create_url_ref(JNIEnv *env, jstring path, CFURLRef &urlRef)
Creates a CFURLRef from the given path.