*/
-#include "config.h"
+#include "aubio_priv.h"
#ifdef HAVE_SOURCE_APPLE_AUDIO
-#include "aubio_priv.h"
#include "fvec.h"
#include "fmat.h"
+#include "ioutils.h"
#include "io/source_apple_audio.h"
// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
#define RT_BYTE3( a ) ( ((a) >> 16) & 0xff )
#define RT_BYTE4( a ) ( ((a) >> 24) & 0xff )
-#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
-
struct _aubio_source_apple_audio_t {
uint_t channels;
uint_t samplerate; //< requested samplerate
AudioBufferList bufferList;
};
-extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
+extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
extern void freeAudioBufferList(AudioBufferList *bufferList);
-extern CFURLRef getURLFromPath(const char * path);
+extern CFURLRef createURLFromPath(const char * path);
char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
-uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path);
+uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path);
-aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size)
+aubio_source_apple_audio_t * new_aubio_source_apple_audio(const char_t * path, uint_t samplerate, uint_t block_size)
{
aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
- if (path == NULL) {
+ if (path == NULL || strnlen(path, PATH_MAX) < 1) {
AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
goto beach;
}
s->block_size = block_size;
s->samplerate = samplerate;
- s->path = path;
if ( aubio_source_apple_audio_open ( s, path ) ) {
goto beach;
return s;
beach:
- AUBIO_FREE(s);
+ del_aubio_source_apple_audio(s);
return NULL;
}
-uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path)
+uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path)
{
OSStatus err = noErr;
UInt32 propSize;
- s->path = path;
+
+ s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
+ strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
// open the resource url
- CFURLRef fileURL = getURLFromPath(path);
+ CFURLRef fileURL = createURLFromPath(s->path);
err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
+ CFRelease(fileURL);
if (err == -43) {
AUBIO_ERR("source_apple_audio: Failed opening %s, "
"file not found, or no read access\n", s->path);
s->channels = fileFormat.mChannelsPerFrame;
AudioStreamBasicDescription clientFormat;
- propSize = sizeof(clientFormat);
+ propSize = sizeof(AudioStreamBasicDescription);
memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
clientFormat.mFormatID = kAudioFormatLinearPCM;
clientFormat.mSampleRate = (Float64)(s->samplerate);
- clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
+ clientFormat.mFormatFlags = kAudioFormatFlagIsFloat;
clientFormat.mChannelsPerFrame = s->channels;
- clientFormat.mBitsPerChannel = sizeof(short) * 8;
+ clientFormat.mBitsPerChannel = sizeof(smpl_t) * 8;
clientFormat.mFramesPerPacket = 1;
clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
- clientFormat.mReserved = 0;
// set the client format description
err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
// allocate the AudioBufferList
freeAudioBufferList(&s->bufferList);
- if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
+ if (createAudioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
goto beach;
}
return err;
}
-void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
- UInt32 c, v, loadedPackets = s->block_size;
+static UInt32 aubio_source_apple_audio_read_frame(aubio_source_apple_audio_t *s)
+{
+ UInt32 loadedPackets = s->block_size;
OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
if (err) {
char_t errorstr[20];
AUBIO_ERROR("source_apple_audio: error while reading %s "
"with ExtAudioFileRead (%s)\n", s->path,
getPrintableOSStatusError(errorstr, err));
- goto beach;
}
+ return loadedPackets;
+}
- short *data = (short*)s->bufferList.mBuffers[0].mData;
+void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to,
+ uint_t * read) {
+ uint_t c, v;
+ UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
+ uint_t length = aubio_source_validate_input_length("source_apple_audio",
+ s->path, s->block_size, read_to->length);
+ smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
- smpl_t *buf = read_to->data;
+ length = MIN(loadedPackets, length);
- for (v = 0; v < loadedPackets; v++) {
- buf[v] = 0.;
+ for (v = 0; v < length; v++) {
+ read_to->data[v] = 0.;
for (c = 0; c < s->channels; c++) {
- buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
+ read_to->data[v] += data[ v * s->channels + c];
}
- buf[v] /= (smpl_t)s->channels;
+ read_to->data[v] /= (smpl_t)s->channels;
}
// short read, fill with zeros
- if (loadedPackets < s->block_size) {
- for (v = loadedPackets; v < s->block_size; v++) {
- buf[v] = 0.;
- }
- }
+ aubio_source_pad_output(read_to, length);
- *read = (uint_t)loadedPackets;
- return;
-beach:
- *read = 0;
- return;
+ *read = (uint_t)length;
}
void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
- UInt32 c, v, loadedPackets = s->block_size;
- OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
- if (err) {
- char_t errorstr[20];
- AUBIO_ERROR("source_apple_audio: error while reading %s "
- "with ExtAudioFileRead (%s)\n", s->path,
- getPrintableOSStatusError(errorstr, err));
- goto beach;
+ uint_t c, v;
+ uint_t length = aubio_source_validate_input_length("source_apple_audio",
+ s->path, s->block_size, read_to->length);
+ uint_t channels = aubio_source_validate_input_channels("source_apple_audio",
+ s->path, s->channels, read_to->height);
+ UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
+ smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
+
+ length = MIN(loadedPackets, length);
+
+ for (v = 0; v < length; v++) {
+ for (c = 0; c < channels; c++) {
+ read_to->data[c][v] = data[ v * s->channels + c];
+ }
}
- short *data = (short*)s->bufferList.mBuffers[0].mData;
+ aubio_source_pad_multi_output(read_to, s->channels, (uint_t)length);
- smpl_t **buf = read_to->data;
-
- for (v = 0; v < loadedPackets; v++) {
- for (c = 0; c < read_to->height; c++) {
- buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
- }
- }
- // if read_data has more channels than the file
- if (read_to->height > s->channels) {
- // copy last channel to all additional channels
- for (v = 0; v < loadedPackets; v++) {
- for (c = s->channels; c < read_to->height; c++) {
- buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]);
- }
- }
- }
- // short read, fill with zeros
- if (loadedPackets < s->block_size) {
- for (v = loadedPackets; v < s->block_size; v++) {
- for (c = 0; c < read_to->height; c++) {
- buf[c][v] = 0.;
- }
- }
- }
- *read = (uint_t)loadedPackets;
- return;
-beach:
- *read = 0;
- return;
+ *read = (uint_t)length;
}
uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
{
OSStatus err = noErr;
- if (!s->audioFile) { return AUBIO_FAIL; }
+ if (!s->audioFile) { return AUBIO_OK; }
err = ExtAudioFileDispose(s->audioFile);
s->audioFile = NULL;
if (err) {
}
void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
+ AUBIO_ASSERT(s);
aubio_source_apple_audio_close (s);
+ if (s->path) AUBIO_FREE(s->path);
freeAudioBufferList(&s->bufferList);
AUBIO_FREE(s);
- return;
}
uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
goto beach;
}
// check if we are not seeking out of the file
- SInt64 fileLengthFrames = 0;
- UInt32 propSize = sizeof(fileLengthFrames);
- ExtAudioFileGetProperty(s->audioFile,
- kExtAudioFileProperty_FileLengthFrames, &propSize, &fileLengthFrames);
+ uint_t fileLengthFrames = aubio_source_apple_audio_get_duration(s);
// compute position in the source file, before resampling
smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
SInt64 resampled_pos = (SInt64)ROUND( pos * ratio );
}
// after a short read, the bufferList size needs to resetted to prepare for a full read
AudioBufferList *bufferList = &s->bufferList;
- bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short);
+ bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (smpl_t);
// do the actual seek
err = ExtAudioFileSeek(s->audioFile, resampled_pos);
if (err) {
return err;
}
-uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) {
+uint_t aubio_source_apple_audio_get_samplerate(const aubio_source_apple_audio_t * s) {
return s->samplerate;
}
-uint_t aubio_source_apple_audio_get_channels(aubio_source_apple_audio_t * s) {
+uint_t aubio_source_apple_audio_get_channels(const aubio_source_apple_audio_t * s) {
return s->channels;
}
+uint_t aubio_source_apple_audio_get_duration(const aubio_source_apple_audio_t * s) {
+ SInt64 fileLengthFrames = 0;
+ UInt32 propSize = sizeof(fileLengthFrames);
+ OSStatus err = ExtAudioFileGetProperty(s->audioFile,
+ kExtAudioFileProperty_FileLengthFrames, &propSize, &fileLengthFrames);
+ if (err) {
+ char_t errorstr[20];
+ AUBIO_ERROR("source_apple_audio: Failed getting %s duration, "
+ "error in ExtAudioFileGetProperty (%s)\n", s->path,
+ getPrintableOSStatusError(errorstr, err));
+ return 0;
+ }
+ return (uint_t)fileLengthFrames;
+}
+
#endif /* HAVE_SOURCE_APPLE_AUDIO */