source: src/io/sink_apple_audio.c @ 98a3887

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

src/io/*apple*: improve error messages

  • Property mode set to 100644
File size: 5.1 KB
Line 
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"
22
23#ifdef __APPLE__
24
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
36extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
37extern void freeAudioBufferList(AudioBufferList *bufferList);
38extern CFURLRef getURLFromPath(const char * path);
39char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
40
41#define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
42
43struct _aubio_sink_apple_audio_t {
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;
52  bool async;
53};
54
55aubio_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);
57  s->samplerate = samplerate;
58  s->channels = 1;
59  s->path = uri;
60  s->max_frames = MAX_SIZE;
61  s->async = true;
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) {
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));
86    goto beach;
87  }
88  if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
89    AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
90        "out of memory? \n", s->path);
91    goto beach;
92  }
93  return s;
94
95beach:
96  AUBIO_FREE(s);
97  return NULL;
98}
99
100void 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;
104  if (write > s->max_frames) {
105    AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
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  }
118  if (s->async) {
119    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
120
121    if (err) {
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));
126      s->async = false;
127    } else {
128      return;
129    }
130
131  } else {
132    err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
133
134    if (err) {
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));
139    }
140  }
141  return;
142}
143
144void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
145  OSStatus err = noErr;
146  if (!s || !s->audioFile) {
147    AUBIO_ERR("sink_apple_audio: failed erasing\n");
148    return;
149  }
150  err = ExtAudioFileDispose(s->audioFile);
151  if (err) {
152    char_t errorstr[20];
153    AUBIO_ERROR("sink_apple_audio: error while closing %s "
154        "in ExtAudioFileDispose (%s)\n", s->path,
155        getPrintableOSStatusError(errorstr, err));
156  }
157  s->audioFile = NULL;
158  freeAudioBufferList(&s->bufferList);
159  AUBIO_FREE(s);
160  return;
161}
162
163#endif /* __APPLE__ */
Note: See TracBrowser for help on using the repository browser.