source: src/io/sink_apple_audio.c @ 1223979

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

src/io/sink_apple_audio.c: added apple_audio sink, merge apple stuff

  • Property mode set to 100644
File size: 4.4 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#include "aubio_priv.h"
23#include "fvec.h"
24#include "io/sink_apple_audio.h"
25
26// CFURLRef, CFURLCreateWithFileSystemPath, ...
27#include <CoreFoundation/CoreFoundation.h>
28// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
29#include <AudioToolbox/AudioToolbox.h>
30
31#define FLOAT_TO_SHORT(x) (short)(x * 32768)
32
33extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
34extern void freeAudioBufferList(AudioBufferList *bufferList);
35extern CFURLRef getURLFromPath(const char * path);
36
37#define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
38
39struct _aubio_sink_apple_audio_t { 
40  uint_t samplerate;
41  uint_t channels;
42  char_t *path;
43
44  uint_t max_frames;
45
46  AudioBufferList bufferList;
47  ExtAudioFileRef audioFile;
48};
49
50aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) {
51  aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
52  s->samplerate;
53  s->channels = 1;
54  s->path = uri;
55  s->max_frames = MAX_SIZE;
56
57  AudioStreamBasicDescription clientFormat;
58  UInt32 propSize = sizeof(clientFormat);
59  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
60  clientFormat.mFormatID         = kAudioFormatLinearPCM;
61  clientFormat.mSampleRate       = (Float64)(s->samplerate);
62  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
63  clientFormat.mChannelsPerFrame = s->channels;
64  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
65  clientFormat.mFramesPerPacket  = 1;
66  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
67  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
68  clientFormat.mReserved         = 0;
69
70  AudioFileTypeID fileType = kAudioFileWAVEType;
71  CFURLRef fileURL = getURLFromPath(uri);
72  bool overwrite = true;
73  OSStatus err = noErr;
74  err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
75     overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
76  if (err) {
77    AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err);
78    goto beach;
79  }
80  if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
81    AUBIO_ERR("error when creating buffer list for %s, out of memory? \n", s->path);
82    goto beach;
83  }
84  return s;
85
86beach:
87  AUBIO_FREE(s);
88  return NULL;
89}
90
91void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
92  OSStatus err = noErr;
93  UInt32 c, v;
94  bool async = true;
95  short *data = (short*)s->bufferList.mBuffers[0].mData;
96  if (write > s->max_frames) { 
97    write = s->max_frames;
98    AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
99      write, s->max_frames);
100  }
101  smpl_t *buf = write_data->data;
102
103  if (buf) {
104      for (c = 0; c < s->channels; c++) {
105          for (v = 0; v < write; v++) {
106              data[v * s->channels + c] =
107                  FLOAT_TO_SHORT(buf[ v * s->channels + c]);
108          }
109      }
110  }
111  if (async) {
112    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
113    if (err) { AUBIO_ERROR("error in ExtAudioFileWriteAsync, %d\n", (int)err); }
114  } else {
115    err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
116    if (err) { AUBIO_ERROR("error in ExtAudioFileWrite, %d\n", (int)err); }
117  }
118  return;
119}
120
121void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
122  OSStatus err = noErr;
123  if (!s || !s->audioFile) { return; }
124  err = ExtAudioFileDispose(s->audioFile);
125  if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
126  s->audioFile = NULL;
127  freeAudioBufferList(&s->bufferList);
128  AUBIO_FREE(s);
129  return;
130}
Note: See TracBrowser for help on using the repository browser.