From 222b176186fbbbed69a93f8c3f5065461078a242 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sun, 23 Feb 2014 12:56:54 -0300 Subject: [PATCH] src/io/sink_apple_audio.h: add do_multi, preset_samplerate, preset_channels, get_samplerate, and get_channels --- src/io/sink_apple_audio.c | 116 +++++++++++++++++++++++++++-- src/io/sink_apple_audio.h | 72 +++++++++++++++++- tests/src/io/test-sink_apple_audio-multi.c | 78 +++++++++++++++++++ 3 files changed, 258 insertions(+), 8 deletions(-) create mode 100644 tests/src/io/test-sink_apple_audio-multi.c diff --git a/src/io/sink_apple_audio.c b/src/io/sink_apple_audio.c index ebc94a27..12a97169 100644 --- a/src/io/sink_apple_audio.c +++ b/src/io/sink_apple_audio.c @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Paul Brossier + Copyright (C) 2012-2014 Paul Brossier This file is part of aubio. @@ -24,6 +24,7 @@ #include "aubio_priv.h" #include "fvec.h" +#include "fmat.h" #include "io/sink_apple_audio.h" // CFURLRef, CFURLCreateWithFileSystemPath, ... @@ -38,6 +39,8 @@ extern void freeAudioBufferList(AudioBufferList *bufferList); extern CFURLRef getURLFromPath(const char * path); char_t *getPrintableOSStatusError(char_t *str, OSStatus error); +uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s); + #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time struct _aubio_sink_apple_audio_t { @@ -54,12 +57,68 @@ struct _aubio_sink_apple_audio_t { aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) { aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t); - s->samplerate = samplerate; - s->channels = 1; s->path = uri; s->max_frames = MAX_SIZE; s->async = true; + s->samplerate = 0; + s->channels = 0; + + // negative samplerate given, abort + if ((sint_t)samplerate < 0) goto beach; + // zero samplerate given. do not open yet + if ((sint_t)samplerate == 0) return s; + + s->samplerate = samplerate; + s->channels = 1; + + if (aubio_sink_apple_audio_open(s) != AUBIO_OK) { + // open failed, abort + goto beach; + } + + return s; +beach: + AUBIO_FREE(s); + return NULL; +} + +uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate) +{ + if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL; + s->samplerate = samplerate; + // automatically open when both samplerate and channels have been set + if (s->samplerate != 0 && s->channels != 0) { + return aubio_sink_apple_audio_open(s); + } + return AUBIO_OK; +} + +uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels) +{ + if ((sint_t)(channels) <= 0) return AUBIO_FAIL; + s->channels = channels; + // automatically open when both samplerate and channels have been set + if (s->samplerate != 0 && s->channels != 0) { + return aubio_sink_apple_audio_open(s); + } + return AUBIO_OK; +} + +uint_t aubio_sink_apple_audio_get_samplerate(aubio_sink_apple_audio_t *s) +{ + return s->samplerate; +} + +uint_t aubio_sink_apple_audio_get_channels(aubio_sink_apple_audio_t *s) +{ + return s->channels; +} + +uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) { + + if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL; + AudioStreamBasicDescription clientFormat; memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription)); clientFormat.mFormatID = kAudioFormatLinearPCM; @@ -73,7 +132,7 @@ aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t sampl clientFormat.mReserved = 0; AudioFileTypeID fileType = kAudioFileWAVEType; - CFURLRef fileURL = getURLFromPath(uri); + CFURLRef fileURL = getURLFromPath(s->path); bool overwrite = true; OSStatus err = noErr; err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL, @@ -90,11 +149,10 @@ aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t sampl "out of memory? \n", s->path); goto beach; } - return s; + return AUBIO_OK; beach: - AUBIO_FREE(s); - return NULL; + return AUBIO_FAIL; } void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) { @@ -141,6 +199,50 @@ void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data return; } +void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) { + OSStatus err = noErr; + UInt32 c, v; + short *data = (short*)s->bufferList.mBuffers[0].mData; + if (write > s->max_frames) { + AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames); + write = s->max_frames; + } + smpl_t **buf = write_data->data; + + if (buf) { + for (c = 0; c < s->channels; c++) { + for (v = 0; v < write; v++) { + data[v * s->channels + c] = + FLOAT_TO_SHORT(buf[c][v]); + } + } + } + if (s->async) { + err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList); + + if (err) { + char_t errorstr[20]; + AUBIO_ERROR("sink_apple_audio: error while writing %s " + "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path, + getPrintableOSStatusError(errorstr, err)); + s->async = false; + } else { + return; + } + + } else { + err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList); + + if (err) { + char_t errorstr[20]; + AUBIO_ERROR("sink_apple_audio: error while writing %s " + "in ExtAudioFileWrite (%s)\n", s->path, + getPrintableOSStatusError(errorstr, err)); + } + } + return; +} + uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) { OSStatus err = noErr; if (!s->audioFile) { diff --git a/src/io/sink_apple_audio.h b/src/io/sink_apple_audio.h index f30e3ead..b9efe97f 100644 --- a/src/io/sink_apple_audio.h +++ b/src/io/sink_apple_audio.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2013 Paul Brossier + Copyright (C) 2012-2014 Paul Brossier This file is part of aubio. @@ -39,6 +39,7 @@ extern "C" { #endif +/** sink_apple_audio object */ typedef struct _aubio_sink_apple_audio_t aubio_sink_apple_audio_t; /** @@ -52,11 +53,69 @@ typedef struct _aubio_sink_apple_audio_t aubio_sink_apple_audio_t; Creates a new sink object. + If samplerate is set to 0, the creation of the file will be delayed until + both ::aubio_sink_preset_samplerate and ::aubio_sink_preset_channels have + been called. + */ aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate); /** + preset sink samplerate + + \param s sink, created with ::new_aubio_sink_apple_audio + \param samplerate samplerate to preset the sink to, in Hz + + \return 0 on success, 1 on error + + Preset the samplerate of the sink. The file should have been created using a + samplerate of 0. + + The file will be opened only when both samplerate and channels have been set. + +*/ +uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate); + +/** + + preset sink channels + + \param s sink, created with ::new_aubio_sink_apple_audio + \param channels number of channels to preset the sink to + + \return 0 on success, 1 on error + + Preset the samplerate of the sink. The file should have been created using a + samplerate of 0. + + The file will be opened only when both samplerate and channels have been set. + +*/ +uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels); + +/** + + get samplerate of sink object + + \param s sink object, created with ::new_aubio_sink_apple_audio + \return samplerate, in Hz + +*/ +uint_t aubio_sink_apple_audio_get_samplerate(aubio_sink_apple_audio_t *s); + +/** + + get channels of sink object + + \param s sink object, created with ::new_aubio_sink_apple_audio + \return number of channels + +*/ +uint_t aubio_sink_apple_audio_get_channels(aubio_sink_apple_audio_t *s); + +/** + write monophonic vector of length hop_size to sink \param s sink, created with ::new_aubio_sink_apple_audio @@ -68,6 +127,17 @@ void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data /** + write polyphonic vector of length hop_size to sink + + \param s sink, created with ::new_aubio_sink_apple_audio + \param write_data ::fmat_t samples to write to sink + \param write number of frames to write + +*/ +void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write); + +/** + close sink \param s sink_apple_audio object, created with ::new_aubio_sink_apple_audio diff --git a/tests/src/io/test-sink_apple_audio-multi.c b/tests/src/io/test-sink_apple_audio-multi.c new file mode 100644 index 00000000..a4058912 --- /dev/null +++ b/tests/src/io/test-sink_apple_audio-multi.c @@ -0,0 +1,78 @@ +#define AUBIO_UNSTABLE 1 +#include +#include "utils_tests.h" + +// this file uses the unstable aubio api, please use aubio_sink instead +// see src/io/sink.h and tests/src/sink/test-sink.c + +int main (int argc, char **argv) +{ + sint_t err = 0; + + if (argc < 3) { + err = 2; + PRINT_ERR("not enough arguments\n"); + PRINT_MSG("usage: %s [samplerate] [channels] [hop_size]\n", argv[0]); + return err; + } + +#ifdef __APPLE__ + uint_t samplerate = 0; + uint_t channels = 0; + uint_t hop_size = 512; + uint_t n_frames = 0, read = 0; + + char_t *source_path = argv[1]; + char_t *sink_path = argv[2]; + + if ( argc >= 4 ) samplerate = atoi(argv[3]); + if ( argc >= 5 ) channels = atoi(argv[4]); + if ( argc >= 6 ) hop_size = atoi(argv[5]); + if ( argc >= 7 ) { + err = 2; + PRINT_ERR("too many arguments\n"); + return err; + } + + aubio_source_t *i = new_aubio_source(source_path, samplerate, hop_size); + if (!i) { err = 1; goto beach_source; } + + if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(i); + if (channels == 0 ) channels = aubio_source_get_channels(i); + + fmat_t *mat = new_fmat(channels, hop_size); + if (!mat) { err = 1; goto beach_fmat; } + + aubio_sink_apple_audio_t *o = new_aubio_sink_apple_audio(sink_path, 0); + if (!o) { err = 1; goto beach_sink; } + err = aubio_sink_apple_audio_preset_samplerate(o, samplerate); + if (err) { goto beach; } + err = aubio_sink_apple_audio_preset_channels(o, channels); + if (err) { goto beach; } + + do { + aubio_source_do_multi(i, mat, &read); + aubio_sink_apple_audio_do_multi(o, mat, read); + n_frames += read; + } while ( read == hop_size ); + + PRINT_MSG("read %d frames at %dHz in %d channels (%d blocks) from %s written to %s\n", + n_frames, samplerate, channels, n_frames / hop_size, + source_path, sink_path); + PRINT_MSG("wrote %s with %dHz in %d channels\n", sink_path, + aubio_sink_apple_audio_get_samplerate(o), + aubio_sink_apple_audio_get_channels(o) ); + +beach: + del_aubio_sink_apple_audio(o); +beach_sink: + del_fmat(mat); +beach_fmat: + del_aubio_source(i); +beach_source: +#else + err = 3; + PRINT_ERR("aubio was not compiled with aubio_sink_apple_audio\n"); +#endif /* __APPLE__ */ + return err; +} -- 2.11.0