[1223979] | 1 | /* |
---|
| 2 | Copyright (C) 2012 Paul Brossier <piem@aubio.org> |
---|
| 3 | |
---|
| 4 | This file is part of aubio. |
---|
| 5 | |
---|
| 6 | aubio is free software: you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | (at your option) any later version. |
---|
| 10 | |
---|
| 11 | aubio is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "config.h" |
---|
[dd5a052] | 22 | |
---|
| 23 | #ifdef __APPLE__ |
---|
| 24 | |
---|
[1223979] | 25 | #include "aubio_priv.h" |
---|
| 26 | #include "fvec.h" |
---|
| 27 | #include "io/sink_apple_audio.h" |
---|
| 28 | |
---|
| 29 | // CFURLRef, CFURLCreateWithFileSystemPath, ... |
---|
| 30 | #include <CoreFoundation/CoreFoundation.h> |
---|
| 31 | // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ... |
---|
| 32 | #include <AudioToolbox/AudioToolbox.h> |
---|
| 33 | |
---|
| 34 | #define FLOAT_TO_SHORT(x) (short)(x * 32768) |
---|
| 35 | |
---|
| 36 | extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize); |
---|
| 37 | extern void freeAudioBufferList(AudioBufferList *bufferList); |
---|
| 38 | extern CFURLRef getURLFromPath(const char * path); |
---|
[98a3887] | 39 | char_t *getPrintableOSStatusError(char_t *str, OSStatus error); |
---|
[1223979] | 40 | |
---|
| 41 | #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time |
---|
| 42 | |
---|
[8a7b344] | 43 | struct _aubio_sink_apple_audio_t { |
---|
[1223979] | 44 | uint_t samplerate; |
---|
| 45 | uint_t channels; |
---|
| 46 | char_t *path; |
---|
| 47 | |
---|
| 48 | uint_t max_frames; |
---|
| 49 | |
---|
| 50 | AudioBufferList bufferList; |
---|
| 51 | ExtAudioFileRef audioFile; |
---|
[4bc92c0] | 52 | bool async; |
---|
[1223979] | 53 | }; |
---|
| 54 | |
---|
| 55 | aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) { |
---|
| 56 | aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t); |
---|
[7b4ea49] | 57 | s->samplerate = samplerate; |
---|
[1223979] | 58 | s->channels = 1; |
---|
| 59 | s->path = uri; |
---|
| 60 | s->max_frames = MAX_SIZE; |
---|
[4bc92c0] | 61 | s->async = true; |
---|
[1223979] | 62 | |
---|
| 63 | AudioStreamBasicDescription clientFormat; |
---|
| 64 | memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription)); |
---|
| 65 | clientFormat.mFormatID = kAudioFormatLinearPCM; |
---|
| 66 | clientFormat.mSampleRate = (Float64)(s->samplerate); |
---|
| 67 | clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; |
---|
| 68 | clientFormat.mChannelsPerFrame = s->channels; |
---|
| 69 | clientFormat.mBitsPerChannel = sizeof(short) * 8; |
---|
| 70 | clientFormat.mFramesPerPacket = 1; |
---|
| 71 | clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8; |
---|
| 72 | clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame; |
---|
| 73 | clientFormat.mReserved = 0; |
---|
| 74 | |
---|
| 75 | AudioFileTypeID fileType = kAudioFileWAVEType; |
---|
| 76 | CFURLRef fileURL = getURLFromPath(uri); |
---|
| 77 | bool overwrite = true; |
---|
| 78 | OSStatus err = noErr; |
---|
| 79 | err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL, |
---|
| 80 | overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile); |
---|
| 81 | if (err) { |
---|
[98a3887] | 82 | char_t errorstr[20]; |
---|
| 83 | AUBIO_ERR("sink_apple_audio: error when trying to create %s with " |
---|
| 84 | "ExtAudioFileCreateWithURL (%s)\n", s->path, |
---|
| 85 | getPrintableOSStatusError(errorstr, err)); |
---|
[1223979] | 86 | goto beach; |
---|
| 87 | } |
---|
| 88 | if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) { |
---|
[98a3887] | 89 | AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, " |
---|
| 90 | "out of memory? \n", s->path); |
---|
[1223979] | 91 | goto beach; |
---|
| 92 | } |
---|
| 93 | return s; |
---|
| 94 | |
---|
| 95 | beach: |
---|
| 96 | AUBIO_FREE(s); |
---|
| 97 | return NULL; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) { |
---|
| 101 | OSStatus err = noErr; |
---|
| 102 | UInt32 c, v; |
---|
| 103 | short *data = (short*)s->bufferList.mBuffers[0].mData; |
---|
[8a7b344] | 104 | if (write > s->max_frames) { |
---|
| 105 | AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames); |
---|
[1223979] | 106 | write = s->max_frames; |
---|
| 107 | } |
---|
| 108 | smpl_t *buf = write_data->data; |
---|
| 109 | |
---|
| 110 | if (buf) { |
---|
| 111 | for (c = 0; c < s->channels; c++) { |
---|
| 112 | for (v = 0; v < write; v++) { |
---|
| 113 | data[v * s->channels + c] = |
---|
| 114 | FLOAT_TO_SHORT(buf[ v * s->channels + c]); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
[4bc92c0] | 118 | if (s->async) { |
---|
[1223979] | 119 | err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList); |
---|
[4bc92c0] | 120 | |
---|
| 121 | if (err) { |
---|
[98a3887] | 122 | char_t errorstr[20]; |
---|
| 123 | AUBIO_ERROR("sink_apple_audio: error while writing %s " |
---|
| 124 | "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path, |
---|
| 125 | getPrintableOSStatusError(errorstr, err)); |
---|
[4bc92c0] | 126 | s->async = false; |
---|
| 127 | } else { |
---|
| 128 | return; |
---|
| 129 | } |
---|
| 130 | |
---|
[1223979] | 131 | } else { |
---|
| 132 | err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList); |
---|
[4bc92c0] | 133 | |
---|
| 134 | if (err) { |
---|
[98a3887] | 135 | char_t errorstr[20]; |
---|
| 136 | AUBIO_ERROR("sink_apple_audio: error while writing %s " |
---|
| 137 | "in ExtAudioFileWrite (%s)\n", s->path, |
---|
| 138 | getPrintableOSStatusError(errorstr, err)); |
---|
[4bc92c0] | 139 | } |
---|
[1223979] | 140 | } |
---|
| 141 | return; |
---|
| 142 | } |
---|
| 143 | |
---|
[a9fd272] | 144 | uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) { |
---|
[1223979] | 145 | OSStatus err = noErr; |
---|
[a9fd272] | 146 | if (!s->audioFile) { |
---|
| 147 | return AUBIO_FAIL; |
---|
[93e3463] | 148 | } |
---|
[1223979] | 149 | err = ExtAudioFileDispose(s->audioFile); |
---|
[98a3887] | 150 | if (err) { |
---|
| 151 | char_t errorstr[20]; |
---|
| 152 | AUBIO_ERROR("sink_apple_audio: error while closing %s " |
---|
| 153 | "in ExtAudioFileDispose (%s)\n", s->path, |
---|
| 154 | getPrintableOSStatusError(errorstr, err)); |
---|
| 155 | } |
---|
[1223979] | 156 | s->audioFile = NULL; |
---|
[a9fd272] | 157 | return err; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) { |
---|
| 161 | if (s->audioFile) aubio_sink_apple_audio_close (s); |
---|
[1223979] | 162 | freeAudioBufferList(&s->bufferList); |
---|
| 163 | AUBIO_FREE(s); |
---|
| 164 | return; |
---|
| 165 | } |
---|
[dd5a052] | 166 | |
---|
| 167 | #endif /* __APPLE__ */ |
---|