source: src/io/sink_apple_audio.c @ 00c9444

feature/autosinkfeature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 00c9444 was 00c9444, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[sink_apple_audio] preset_format does not fail on empty format string

  • Property mode set to 100644
File size: 12.2 KB
RevLine 
[1223979]1/*
[222b176]2  Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org>
[1223979]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
[33d0242]21#include "aubio_priv.h"
[dd5a052]22
[9209c79]23#ifdef HAVE_SINK_APPLE_AUDIO
[1223979]24#include "fvec.h"
[222b176]25#include "fmat.h"
[1223979]26#include "io/sink_apple_audio.h"
[cf19b8a]27#include "io/ioutils.h"
[1223979]28
29// CFURLRef, CFURLCreateWithFileSystemPath, ...
30#include <CoreFoundation/CoreFoundation.h>
31// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
32#include <AudioToolbox/AudioToolbox.h>
33
[7b5e1a5]34extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
[1223979]35extern void freeAudioBufferList(AudioBufferList *bufferList);
[2da7526]36extern CFURLRef createURLFromPath(const char * path);
[98a3887]37char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
[1223979]38
[222b176]39uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s);
40
[2b208a8]41uint_t aubio_str_extension_matches(const char_t *ext,
42    const char_t *pattern);
43const char_t *aubio_str_get_extension(const char_t *filename);
44
[1223979]45#define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
46
[9fa9b86]47void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write);
48
[8a7b344]49struct _aubio_sink_apple_audio_t {
[1223979]50  uint_t samplerate;
51  uint_t channels;
52  char_t *path;
53
54  uint_t max_frames;
55
56  AudioBufferList bufferList;
57  ExtAudioFileRef audioFile;
[4bc92c0]58  bool async;
[2b208a8]59  AudioFileTypeID fileType;
[1223979]60};
61
[ae5d58a]62aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(const char_t * uri, uint_t samplerate) {
[1223979]63  aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
64  s->max_frames = MAX_SIZE;
[9fa9b86]65  s->async = false;
[1223979]66
[cf387e3]67  if ( (uri == NULL) || (strnlen(uri, PATH_MAX) < 1) ) {
[9657163]68    AUBIO_ERROR("sink_apple_audio: Aborted opening null path\n");
69    goto beach;
70  }
[cf387e3]71
[d2be104]72  s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
73  strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1);
[9657163]74
[222b176]75  s->samplerate = 0;
76  s->channels = 0;
77
[2b208a8]78  aubio_sink_apple_audio_preset_format(s, aubio_str_get_extension(uri));
79
[222b176]80  // zero samplerate given. do not open yet
[cf19b8a]81  if ((sint_t)samplerate == 0) {
82    return s;
83  }
[7b5e1a5]84
[cf19b8a]85  // invalid samplerate given, abort
86  if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
87    goto beach;
88  }
[222b176]89
90  s->samplerate = samplerate;
91  s->channels = 1;
92
93  if (aubio_sink_apple_audio_open(s) != AUBIO_OK) {
94    // open failed, abort
95    goto beach;
96  }
97
98  return s;
99beach:
[cf387e3]100  del_aubio_sink_apple_audio(s);
[222b176]101  return NULL;
102}
103
104uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
105{
[cf19b8a]106  if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
107    return AUBIO_FAIL;
108  }
[222b176]109  s->samplerate = samplerate;
110  // automatically open when both samplerate and channels have been set
[cf387e3]111  if (/* s->samplerate != 0 && */ s->channels != 0) {
[222b176]112    return aubio_sink_apple_audio_open(s);
113  }
114  return AUBIO_OK;
115}
116
117uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
118{
[cf19b8a]119  if (aubio_io_validate_channels("sink_apple_audio", s->path, channels)) {
120    return AUBIO_FAIL;
121  }
[222b176]122  s->channels = channels;
123  // automatically open when both samplerate and channels have been set
[cf387e3]124  if (s->samplerate != 0 /* && s->channels != 0 */) {
[222b176]125    return aubio_sink_apple_audio_open(s);
126  }
127  return AUBIO_OK;
128}
129
[2b208a8]130uint_t aubio_sink_apple_audio_preset_format(aubio_sink_apple_audio_t *s,
131    const char_t *fmt)
132{
133  if (aubio_str_extension_matches(fmt, "wav")) {
134    s->fileType = kAudioFileWAVEType;
135  } else if (aubio_str_extension_matches(fmt, "m4a")
136      || aubio_str_extension_matches(fmt, "mp4") ) {
137    // use alac for "mp4" and "m4a"
138    s->fileType = kAudioFileM4AType;
139  } else if (aubio_str_extension_matches(fmt, "aac") ) {
140    // only use lossy codec for "aac"
141    s->fileType = kAudioFileMPEG4Type;
142  } else if (aubio_str_extension_matches(fmt, "aiff") ) {
143    // only use lossy codec for "aac"
144    s->fileType = kAudioFileAIFFType;
145  } else {
146    s->fileType = kAudioFileWAVEType;
[00c9444]147    if (fmt && strnlen(fmt, PATH_MAX)) {
148      AUBIO_WRN("sink_apple_audio: could not guess format for %s,"
149         " using default (wav)\n", s->path);
150      return AUBIO_FAIL;
151    }
[2b208a8]152  }
153  return AUBIO_OK;
154}
155
156static void aubio_sink_apple_audio_set_client_format(aubio_sink_apple_audio_t* s,
157    AudioStreamBasicDescription *clientFormat)
158{
159  memset(clientFormat, 0, sizeof(AudioStreamBasicDescription));
160  // always set samplerate and channels first
161  clientFormat->mSampleRate       = (Float64)(s->samplerate);
162  clientFormat->mChannelsPerFrame = s->channels;
163
164  switch (s->fileType) {
165    case kAudioFileM4AType:
166      clientFormat->mFormatID         = kAudioFormatAppleLossless;
167      break;
168    case kAudioFileMPEG4Type:
169      clientFormat->mFormatID         = kAudioFormatMPEG4AAC;
170      clientFormat->mFormatFlags      = kMPEG4Object_AAC_Main;
171      clientFormat->mFormatFlags     |= kAppleLosslessFormatFlag_16BitSourceData;
172      clientFormat->mFramesPerPacket  = 1024;
173      break;
174    case kAudioFileWAVEType:
175      clientFormat->mFormatID         = kAudioFormatLinearPCM;
176      clientFormat->mFormatFlags      = kAudioFormatFlagIsSignedInteger;
177      clientFormat->mFormatFlags     |= kAudioFormatFlagIsPacked;
178      clientFormat->mBitsPerChannel   = sizeof(short) * 8;
179      clientFormat->mFramesPerPacket  = 1;
180      clientFormat->mBytesPerFrame    = clientFormat->mBitsPerChannel * clientFormat->mChannelsPerFrame / 8;
181      clientFormat->mBytesPerPacket   = clientFormat->mFramesPerPacket * clientFormat->mBytesPerFrame;
182      break;
183    case kAudioFileAIFFType:
184      clientFormat->mFormatID         = kAudioFormatLinearPCM;
185      clientFormat->mFormatFlags      = kAudioFormatFlagIsSignedInteger;
186      clientFormat->mFormatFlags     |= kAudioFormatFlagIsPacked;
187      clientFormat->mFormatFlags     |= kAudioFormatFlagIsBigEndian;
188      clientFormat->mBitsPerChannel   = sizeof(short) * 8;
189      clientFormat->mFramesPerPacket  = 1;
190      clientFormat->mBytesPerFrame    = clientFormat->mBitsPerChannel * clientFormat->mChannelsPerFrame / 8;
191      clientFormat->mBytesPerPacket   = clientFormat->mFramesPerPacket * clientFormat->mBytesPerFrame;
192      break;
193    default:
194      break;
195  }
196}
197
[ae5d58a]198uint_t aubio_sink_apple_audio_get_samplerate(const aubio_sink_apple_audio_t *s)
[222b176]199{
200  return s->samplerate;
201}
202
[ae5d58a]203uint_t aubio_sink_apple_audio_get_channels(const aubio_sink_apple_audio_t *s)
[222b176]204{
205  return s->channels;
206}
207
208uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
209
210  if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
211
[2da7526]212  CFURLRef fileURL = createURLFromPath(s->path);
[1223979]213  bool overwrite = true;
[7b5e1a5]214
215  // set the in-memory format
216  AudioStreamBasicDescription inputFormat;
217  memset(&inputFormat, 0, sizeof(AudioStreamBasicDescription));
218  inputFormat.mFormatID         = kAudioFormatLinearPCM;
219  inputFormat.mSampleRate       = (Float64)(s->samplerate);
220  inputFormat.mFormatFlags      = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
221  inputFormat.mChannelsPerFrame = s->channels;
222  inputFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
223  inputFormat.mFramesPerPacket  = 1;
224  inputFormat.mBytesPerFrame    = inputFormat.mBitsPerChannel * inputFormat.mChannelsPerFrame / 8;
225  inputFormat.mBytesPerPacket   = inputFormat.mFramesPerPacket * inputFormat.mBytesPerFrame;
[2b208a8]226
227  // get the in-file format
228  AudioStreamBasicDescription clientFormat;
229  aubio_sink_apple_audio_set_client_format(s, &clientFormat);
230
[1223979]231  OSStatus err = noErr;
[2b208a8]232  err = ExtAudioFileCreateWithURL(fileURL, s->fileType, &clientFormat, NULL,
[1223979]233     overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
[2da7526]234  CFRelease(fileURL);
[1223979]235  if (err) {
[98a3887]236    char_t errorstr[20];
237    AUBIO_ERR("sink_apple_audio: error when trying to create %s with "
238        "ExtAudioFileCreateWithURL (%s)\n", s->path,
239        getPrintableOSStatusError(errorstr, err));
[1223979]240    goto beach;
241  }
[7b5e1a5]242
[99365e9]243#if defined(kAppleSoftwareAudioCodecManufacturer)
244  // on iOS, set software based encoding before setting clientDataFormat
245  UInt32 codecManf = kAppleSoftwareAudioCodecManufacturer;
246  err = ExtAudioFileSetProperty(s->audioFile,
247      kExtAudioFileProperty_CodecManufacturer,
248      sizeof(UInt32), &codecManf);
249  if (err) {
250    char_t errorstr[20];
251    AUBIO_ERR("sink_apple_audio: error when trying to set sofware codec on %s "
252        "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err));
253    goto beach;
254  }
255#endif
256
[7b5e1a5]257  err = ExtAudioFileSetProperty(s->audioFile,
258      kExtAudioFileProperty_ClientDataFormat,
259      sizeof(AudioStreamBasicDescription), &inputFormat);
260  if (err) {
261    char_t errorstr[20];
262    AUBIO_ERR("sink_apple_audio: error when trying to set output format on %s "
263        "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err));
264    goto beach;
265  }
266
267  if (createAudioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
[98a3887]268    AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
269        "out of memory? \n", s->path);
[1223979]270    goto beach;
271  }
[222b176]272  return AUBIO_OK;
[1223979]273
274beach:
[222b176]275  return AUBIO_FAIL;
[1223979]276}
277
278void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
279  UInt32 c, v;
[7b5e1a5]280  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
[4eb48e6]281  uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
282      s->max_frames, write_data->length, write);
283
284  for (c = 0; c < s->channels; c++) {
285    for (v = 0; v < length; v++) {
[7b5e1a5]286      data[v * s->channels + c] = write_data->data[v];
[4eb48e6]287    }
[1223979]288  }
[4eb48e6]289
290  aubio_sink_apple_audio_write(s, length);
[222b176]291}
292
293void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
294  UInt32 c, v;
[7b5e1a5]295  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
[4eb48e6]296  uint_t channels = aubio_sink_validate_input_channels("sink_apple_audio",
297      s->path, s->channels, write_data->height);
298  uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
299      s->max_frames, write_data->length, write);
300
301  for (c = 0; c < channels; c++) {
302    for (v = 0; v < length; v++) {
[7b5e1a5]303      data[v * s->channels + c] = write_data->data[c][v];
[4eb48e6]304    }
[222b176]305  }
[4eb48e6]306
307  aubio_sink_apple_audio_write(s, length);
[9fa9b86]308}
309
310void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) {
311  OSStatus err = noErr;
[7b5e1a5]312  // set mDataByteSize to match the number of frames to be written
313  // see https://www.mail-archive.com/coreaudio-api@lists.apple.com/msg01109.html
314  s->bufferList.mBuffers[0].mDataByteSize = write * s->channels
315    * sizeof(smpl_t);
[222b176]316  if (s->async) {
317    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
318    if (err) {
319      char_t errorstr[20];
[9fa9b86]320      if (err == kExtAudioFileError_AsyncWriteBufferOverflow) {
321        sprintf(errorstr,"buffer overflow");
322      } else if (err == kExtAudioFileError_AsyncWriteTooLarge) {
323        sprintf(errorstr,"write too large");
324      } else {
325        // unknown error
326        getPrintableOSStatusError(errorstr, err);
327      }
[222b176]328      AUBIO_ERROR("sink_apple_audio: error while writing %s "
[9fa9b86]329                  "in ExtAudioFileWriteAsync (%s)\n", s->path, errorstr);
[4bc92c0]330    }
[1223979]331  } else {
332    err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
[4bc92c0]333    if (err) {
[98a3887]334      char_t errorstr[20];
335      AUBIO_ERROR("sink_apple_audio: error while writing %s "
336          "in ExtAudioFileWrite (%s)\n", s->path,
337          getPrintableOSStatusError(errorstr, err));
[4bc92c0]338    }
[1223979]339  }
340}
341
[a9fd272]342uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
[1223979]343  OSStatus err = noErr;
[a9fd272]344  if (!s->audioFile) {
345    return AUBIO_FAIL;
[93e3463]346  }
[1223979]347  err = ExtAudioFileDispose(s->audioFile);
[98a3887]348  if (err) {
349    char_t errorstr[20];
350    AUBIO_ERROR("sink_apple_audio: error while closing %s "
351        "in ExtAudioFileDispose (%s)\n", s->path,
352        getPrintableOSStatusError(errorstr, err));
353  }
[1223979]354  s->audioFile = NULL;
[a9fd272]355  return err;
356}
357
358void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
[cf387e3]359  AUBIO_ASSERT(s);
360  if (s->audioFile)
361    aubio_sink_apple_audio_close (s);
362  if (s->path)
363    AUBIO_FREE(s->path);
[1223979]364  freeAudioBufferList(&s->bufferList);
365  AUBIO_FREE(s);
366}
[dd5a052]367
[9209c79]368#endif /* HAVE_SINK_APPLE_AUDIO */
Note: See TracBrowser for help on using the repository browser.