23#include "com_tagtraum_casampledsp_CAURLInputStream.h"
26static jfieldID nativeBufferFID = NULL;
27static jmethodID rewindMID = NULL;
28static jmethodID limitMID = NULL;
35static void init_ids(JNIEnv *env, jobject stream) {
36 if (nativeBufferFID == NULL || rewindMID == NULL || limitMID == NULL) {
37 nativeBufferFID = env->GetFieldID(env->GetObjectClass(stream),
"nativeBuffer",
"Ljava/nio/ByteBuffer;");
38 jclass bufferClass = env->FindClass(
"java/nio/Buffer");
39 rewindMID = env->GetMethodID(bufferClass,
"rewind",
"()Ljava/nio/Buffer;");
40 limitMID = env->GetMethodID(bufferClass,
"limit",
"(I)Ljava/nio/Buffer;");
54 fprintf(stderr,
"fillNativeBuffer: %llu\n", afioPtr);
58 jobject byteBuffer = NULL;
60 UInt32 ioNumberDataPackets;
61 UInt32 outNumBytes = 0;
63 init_ids(env, stream);
66 byteBuffer = env->GetObjectField(stream, nativeBufferFID);
67 if (byteBuffer == NULL) {
73 outNumBytes = env->GetDirectBufferCapacity(byteBuffer);
75 afio->
srcBuffer = (
char *)env->GetDirectBufferAddress(byteBuffer);
85 res = AudioFileReadPacketData(afio->
afid,
false, &outNumBytes, afio->
pktDescs,
95 afio->
pos += ioNumberDataPackets;
100 env->CallObjectMethod(byteBuffer, rewindMID);
101 env->CallObjectMethod(byteBuffer, limitMID, outNumBytes);
117 CFURLRef inputURLRef;
132 res = AudioFileOpenURL(inputURLRef, 0x01, 0, &afio->
afid);
134 if (res == fnfErr || res == kAudioFileUnspecifiedError) {
144 res = AudioFileGetProperty(afio->
afid, kAudioFilePropertyDataFormat, &size, &afio->
srcFormat);
153 fprintf(stderr,
"VBR\n");
157 res = AudioFileGetProperty(afio->
afid, kAudioFilePropertyPacketSizeUpperBound, &size, &afio->
srcSizePerPacket);
167 fprintf(stderr,
"VBR: srcSizePerPacket == 0!!\n");
174 fprintf(stderr,
"CBR\n");
182 fprintf(stderr,
"CBR: srcSizePerPacket == 0!!\n");
188 res = AudioFileGetPropertyInfo(afio->
afid, kAudioFilePropertyMagicCookieData, &afio->
cookieSize, NULL);
189 if (res && res != kAudioFileUnsupportedPropertyError) {
196 res = AudioFileGetProperty(afio->
afid, kAudioFilePropertyMagicCookieData, &afio->
cookieSize, afio->
cookie);
206 if (afio->
afid != NULL) {
207 AudioFileClose(afio->
afid);
212 if (afio->
cookie != NULL) {
219 fprintf(stderr,
"Opened: %llu\n", (jlong)afio);
249 AudioFramePacketTranslation translation;
251 translation.mFrame = (SInt64)(afio->
srcFormat.mSampleRate * microseconds) / 1000000LL;
254 fprintf(stderr,
"microseconds : %llu\n", microseconds);
255 fprintf(stderr,
"translation.mFrame: %llu\n", translation.mFrame);
258 size =
sizeof(translation);
259 res = AudioFileGetProperty(afio->
afid, kAudioFilePropertyFrameToPacket, &size, &translation);
264 afio->
pos = translation.mPacket;
265 afio->
frameOffset = translation.mFrameOffsetInPacket;
269 fprintf(stderr,
"frameOffset: %i\n", afio->
frameOffset);
270 fprintf(stderr,
"afio->pos : %llu\n", afio->
pos);
289 fprintf(stderr,
"Closing: %llu\n", afioPtr);
291 if (afioPtr == 0)
return;
294 int res = AudioFileClose(afio->
afid);
301 if (afio->
cookie != NULL) {
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 throwIOExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IOException.
void ca_create_url_ref(JNIEnv *env, jstring path, CFURLRef &urlRef)
Creates a CFURLRef from the given path.
Central context representing the native peer to the Java CAURLInputStream object.
SInt64 lastPos
Last position (in packets)
UInt32 numPacketsPerRead
Number of packets per read.
char * srcBuffer
Source buffer.
UInt32 frameOffset
Frame offset (needed for seeking to the middles of a packet)
UInt32 srcSizePerPacket
Source size per packet.
AudioStreamBasicDescription srcFormat
Source format.
UInt32 cookieSize
Cookie size.
SInt64 pos
Current position (in packets)
UInt32 srcBufferSize
Source buffer size.
AudioStreamPacketDescription * pktDescs
Packet descriptions.