source: src/io/sink_apple_audio.c @ bb941e9

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since bb941e9 was 222b176, checked in by Paul Brossier <piem@piem.org>, 11 years ago

src/io/sink_apple_audio.h: add do_multi, preset_samplerate, preset_channels, get_samplerate, and get_channels

  • Property mode set to 100644
File size: 7.9 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
21#include "config.h"
[dd5a052]22
23#ifdef __APPLE__
24
[1223979]25#include "aubio_priv.h"
26#include "fvec.h"
[222b176]27#include "fmat.h"
[1223979]28#include "io/sink_apple_audio.h"
29
30// CFURLRef, CFURLCreateWithFileSystemPath, ...
31#include <CoreFoundation/CoreFoundation.h>
32// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
33#include <AudioToolbox/AudioToolbox.h>
34
35#define FLOAT_TO_SHORT(x) (short)(x * 32768)
36
37extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
38extern void freeAudioBufferList(AudioBufferList *bufferList);
39extern CFURLRef getURLFromPath(const char * path);
[98a3887]40char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
[1223979]41
[222b176]42uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s);
43
[1223979]44#define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
45
[8a7b344]46struct _aubio_sink_apple_audio_t {
[1223979]47  uint_t samplerate;
48  uint_t channels;
49  char_t *path;
50
51  uint_t max_frames;
52
53  AudioBufferList bufferList;
54  ExtAudioFileRef audioFile;
[4bc92c0]55  bool async;
[1223979]56};
57
58aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) {
59  aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
60  s->path = uri;
61  s->max_frames = MAX_SIZE;
[4bc92c0]62  s->async = true;
[1223979]63
[222b176]64  s->samplerate = 0;
65  s->channels = 0;
66
67  // negative samplerate given, abort
68  if ((sint_t)samplerate < 0) goto beach;
69  // zero samplerate given. do not open yet
70  if ((sint_t)samplerate == 0) return s;
71
72  s->samplerate = samplerate;
73  s->channels = 1;
74
75  if (aubio_sink_apple_audio_open(s) != AUBIO_OK) {
76    // open failed, abort
77    goto beach;
78  }
79
80  return s;
81beach:
82  AUBIO_FREE(s);
83  return NULL;
84}
85
86uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
87{
88  if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
89  s->samplerate = samplerate;
90  // automatically open when both samplerate and channels have been set
91  if (s->samplerate != 0 && s->channels != 0) {
92    return aubio_sink_apple_audio_open(s);
93  }
94  return AUBIO_OK;
95}
96
97uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
98{
99  if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
100  s->channels = channels;
101  // automatically open when both samplerate and channels have been set
102  if (s->samplerate != 0 && s->channels != 0) {
103    return aubio_sink_apple_audio_open(s);
104  }
105  return AUBIO_OK;
106}
107
108uint_t aubio_sink_apple_audio_get_samplerate(aubio_sink_apple_audio_t *s)
109{
110  return s->samplerate;
111}
112
113uint_t aubio_sink_apple_audio_get_channels(aubio_sink_apple_audio_t *s)
114{
115  return s->channels;
116}
117
118uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
119
120  if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
121
[1223979]122  AudioStreamBasicDescription clientFormat;
123  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
124  clientFormat.mFormatID         = kAudioFormatLinearPCM;
125  clientFormat.mSampleRate       = (Float64)(s->samplerate);
126  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
127  clientFormat.mChannelsPerFrame = s->channels;
128  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
129  clientFormat.mFramesPerPacket  = 1;
130  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
131  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
132  clientFormat.mReserved         = 0;
133
134  AudioFileTypeID fileType = kAudioFileWAVEType;
[222b176]135  CFURLRef fileURL = getURLFromPath(s->path);
[1223979]136  bool overwrite = true;
137  OSStatus err = noErr;
138  err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
139     overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
140  if (err) {
[98a3887]141    char_t errorstr[20];
142    AUBIO_ERR("sink_apple_audio: error when trying to create %s with "
143        "ExtAudioFileCreateWithURL (%s)\n", s->path,
144        getPrintableOSStatusError(errorstr, err));
[1223979]145    goto beach;
146  }
147  if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
[98a3887]148    AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
149        "out of memory? \n", s->path);
[1223979]150    goto beach;
151  }
[222b176]152  return AUBIO_OK;
[1223979]153
154beach:
[222b176]155  return AUBIO_FAIL;
[1223979]156}
157
158void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
159  OSStatus err = noErr;
160  UInt32 c, v;
161  short *data = (short*)s->bufferList.mBuffers[0].mData;
[8a7b344]162  if (write > s->max_frames) {
163    AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
[1223979]164    write = s->max_frames;
165  }
166  smpl_t *buf = write_data->data;
167
168  if (buf) {
169      for (c = 0; c < s->channels; c++) {
170          for (v = 0; v < write; v++) {
171              data[v * s->channels + c] =
172                  FLOAT_TO_SHORT(buf[ v * s->channels + c]);
173          }
174      }
175  }
[4bc92c0]176  if (s->async) {
[1223979]177    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
[4bc92c0]178
179    if (err) {
[98a3887]180      char_t errorstr[20];
181      AUBIO_ERROR("sink_apple_audio: error while writing %s "
182          "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path,
183          getPrintableOSStatusError(errorstr, err));
[222b176]184      s->async = false;
185    } else {
186      return;
187    }
188
189  } else {
190    err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
191
192    if (err) {
193      char_t errorstr[20];
194      AUBIO_ERROR("sink_apple_audio: error while writing %s "
195          "in ExtAudioFileWrite (%s)\n", s->path,
196          getPrintableOSStatusError(errorstr, err));
197    }
198  }
199  return;
200}
201
202void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
203  OSStatus err = noErr;
204  UInt32 c, v;
205  short *data = (short*)s->bufferList.mBuffers[0].mData;
206  if (write > s->max_frames) {
207    AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
208    write = s->max_frames;
209  }
210  smpl_t **buf = write_data->data;
211
212  if (buf) {
213      for (c = 0; c < s->channels; c++) {
214          for (v = 0; v < write; v++) {
215              data[v * s->channels + c] =
216                  FLOAT_TO_SHORT(buf[c][v]);
217          }
218      }
219  }
220  if (s->async) {
221    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
222
223    if (err) {
224      char_t errorstr[20];
225      AUBIO_ERROR("sink_apple_audio: error while writing %s "
226          "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path,
227          getPrintableOSStatusError(errorstr, err));
[4bc92c0]228      s->async = false;
229    } else {
230      return;
231    }
232
[1223979]233  } else {
234    err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
[4bc92c0]235
236    if (err) {
[98a3887]237      char_t errorstr[20];
238      AUBIO_ERROR("sink_apple_audio: error while writing %s "
239          "in ExtAudioFileWrite (%s)\n", s->path,
240          getPrintableOSStatusError(errorstr, err));
[4bc92c0]241    }
[1223979]242  }
243  return;
244}
245
[a9fd272]246uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
[1223979]247  OSStatus err = noErr;
[a9fd272]248  if (!s->audioFile) {
249    return AUBIO_FAIL;
[93e3463]250  }
[1223979]251  err = ExtAudioFileDispose(s->audioFile);
[98a3887]252  if (err) {
253    char_t errorstr[20];
254    AUBIO_ERROR("sink_apple_audio: error while closing %s "
255        "in ExtAudioFileDispose (%s)\n", s->path,
256        getPrintableOSStatusError(errorstr, err));
257  }
[1223979]258  s->audioFile = NULL;
[a9fd272]259  return err;
260}
261
262void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
263  if (s->audioFile) aubio_sink_apple_audio_close (s);
[1223979]264  freeAudioBufferList(&s->bufferList);
265  AUBIO_FREE(s);
266  return;
267}
[dd5a052]268
269#endif /* __APPLE__ */
Note: See TracBrowser for help on using the repository browser.