source: src/io/source_apple_audio.c @ 6192ce7

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

src/io/source_apple_audio.{c,h}: added simple file read using ExtAudioFile?

  • Property mode set to 100644
File size: 8.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#ifdef __APPLE__
22#include "config.h"
23#include "aubio_priv.h"
24#include "fvec.h"
25#include "io/source_apple_audio.h"
26
27// CFURLRef, CFURLCreateWithFileSystemPath, ...
28#include <CoreFoundation/CoreFoundation.h>
29// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
30#include <AudioToolbox/AudioToolbox.h>
31
32#define RT_BYTE1( a )      ( (a) & 0xff )
33#define RT_BYTE2( a )      ( ((a) >> 8) & 0xff )
34#define RT_BYTE3( a )      ( ((a) >> 16) & 0xff )
35#define RT_BYTE4( a )      ( ((a) >> 24) & 0xff )
36
37#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
38
39struct _aubio_source_apple_audio_t {
40  uint_t channels;
41  uint_t samplerate;
42  uint_t block_size;
43
44  char_t *path;
45
46  ExtAudioFileRef audioFile;
47  AudioStreamBasicDescription fileFormat;
48  AudioBufferList bufferList;
49};
50
51static int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
52static void freeAudioBufferList(AudioBufferList *bufferList);
53static CFURLRef getURLFromPath(const char * path);
54
55aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size)
56{
57  aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
58
59  s->path = path;
60  s->samplerate = samplerate;
61  s->block_size = block_size;
62  s->channels = 1;
63
64  OSStatus err = noErr;
65  UInt32 propSize;
66
67  AudioStreamBasicDescription clientFormat;
68  propSize = sizeof(clientFormat);
69  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
70  clientFormat.mFormatID         = kAudioFormatLinearPCM;
71  clientFormat.mSampleRate       = (Float64)(s->samplerate);
72  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
73  clientFormat.mChannelsPerFrame = s->channels;
74  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
75  clientFormat.mFramesPerPacket  = 1;
76  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
77  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
78  clientFormat.mReserved         = 0;
79
80  // open the resource url
81  CFURLRef fileURL = getURLFromPath(path);
82  err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
83  if (err) { AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err); goto beach;}
84
85  // create an empty AudioStreamBasicDescription
86  AudioStreamBasicDescription fileFormat;
87  propSize = sizeof(fileFormat);
88  memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
89
90  // fill it with the file description
91  err = ExtAudioFileGetProperty(s->audioFile,
92      kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
93  if (err) { AUBIO_ERROR("error in ExtAudioFileGetProperty, %d\n", (int)err); goto beach;}
94
95  // set the client format description
96  err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
97      propSize, &clientFormat);
98  if (err) { fprintf(stderr, "error in ExtAudioFileSetProperty, %d\n", (int)err); goto beach;}
99
100  // print client and format descriptions
101  AUBIO_DBG("Opened %s\n", s->path);
102  AUBIO_DBG("file/client Format.mFormatID:        : %3c%c%c%c / %c%c%c%c\n",
103      (int)RT_BYTE4(fileFormat.mFormatID),   (int)RT_BYTE3(fileFormat.mFormatID),   (int)RT_BYTE2(fileFormat.mFormatID),   (int)RT_BYTE1(fileFormat.mFormatID),
104      (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
105      );
106
107  AUBIO_DBG("file/client Format.mSampleRate       : %6.0f / %.0f\n",     fileFormat.mSampleRate      ,      clientFormat.mSampleRate);
108  AUBIO_DBG("file/client Format.mFormatFlags      : %6d / %d\n",    (int)fileFormat.mFormatFlags     , (int)clientFormat.mFormatFlags);
109  AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n",    (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
110  AUBIO_DBG("file/client Format.mBitsPerChannel   : %6d / %d\n",    (int)fileFormat.mBitsPerChannel  , (int)clientFormat.mBitsPerChannel);
111  AUBIO_DBG("file/client Format.mFramesPerPacket  : %6d / %d\n",    (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
112  AUBIO_DBG("file/client Format.mBytesPerFrame    : %6d / %d\n",    (int)fileFormat.mBytesPerFrame   , (int)clientFormat.mBytesPerFrame);
113  AUBIO_DBG("file/client Format.mBytesPerPacket   : %6d / %d\n",    (int)fileFormat.mBytesPerPacket  , (int)clientFormat.mBytesPerPacket);
114  AUBIO_DBG("file/client Format.mReserved         : %6d / %d\n",    (int)fileFormat.mReserved        , (int)clientFormat.mReserved);
115
116  // compute the size of the segments needed to read the input file
117  UInt32 samples = s->block_size * clientFormat.mChannelsPerFrame;
118  Float64 rateRatio = clientFormat.mSampleRate / fileFormat.mSampleRate;
119  uint_t segmentSize= (uint_t)(samples * rateRatio + .5);
120  if (rateRatio < 1.) {
121    segmentSize = (uint_t)(samples / rateRatio + .5);
122  } else if (rateRatio > 1.) {
123    AUBIO_WRN("up-sampling %s from %0.2fHz to %0.2fHz\n", s->path, fileFormat.mSampleRate, clientFormat.mSampleRate);
124  } else {
125    assert (segmentSize == samples );
126    //AUBIO_DBG("not resampling, segmentSize %d, block_size %d\n", segmentSize, s->block_size);
127  }
128
129  // allocate the AudioBufferList
130  if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) err = -1;
131
132  return s;
133 
134beach:
135  AUBIO_FREE(s);
136  return NULL;
137}
138
139void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
140  UInt32 c, v, loadedPackets = s->block_size;
141  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
142  if (err) { AUBIO_ERROR("error in ExtAudioFileRead, %d\n", (int)err); goto beach;}
143
144  smpl_t *buf = read_to->data;
145
146  short *data = (short*)s->bufferList.mBuffers[0].mData;
147
148  if (buf) {
149      for (c = 0; c < s->channels; c++) {
150          for (v = 0; v < s->block_size; v++) {
151              if (v < loadedPackets) {
152                  buf[v * s->channels + c] =
153                      SHORT_TO_FLOAT(data[ v * s->channels + c]);
154              } else {
155                  buf[v * s->channels + c] = 0.f;
156              }
157          }
158      }
159  }
160  //if (loadedPackets < s->block_size) return EOF;
161  *read = (uint_t)loadedPackets;
162  return;
163beach:
164  *read = 0;
165  return;
166}
167
168static int createAubioBufferList(AudioBufferList * bufferList, int channels, int segmentSize) {
169  bufferList->mNumberBuffers = 1;
170  bufferList->mBuffers[0].mNumberChannels = channels;
171  bufferList->mBuffers[0].mData = (short *)malloc(segmentSize * sizeof(short));
172  bufferList->mBuffers[0].mDataByteSize = segmentSize * sizeof(short);
173  return 0;
174}
175
176static void freeAudioBufferList(AudioBufferList *bufferList) {
177  UInt32 i = 0;
178  if (!bufferList) return;
179  for (i = 0; i < bufferList->mNumberBuffers; i++) {
180    if (bufferList->mBuffers[i].mData) {
181      free (bufferList->mBuffers[i].mData);
182      bufferList->mBuffers[i].mData = NULL;
183    }
184  }
185  bufferList = NULL;
186}
187
188static CFURLRef getURLFromPath(const char * path) {
189  CFStringRef cfTotalPath = CFStringCreateWithCString (kCFAllocatorDefault,
190      path, kCFStringEncodingUTF8);
191
192  return CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfTotalPath,
193      kCFURLPOSIXPathStyle, false);
194}
195
196
197void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
198  OSStatus err = noErr;
199  if (!s || !s->audioFile) { return; }
200  err = ExtAudioFileDispose(s->audioFile);
201  if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
202  s->audioFile = NULL;
203  freeAudioBufferList(&s->bufferList);
204  AUBIO_FREE(s);
205  return;
206}
207
208#endif /* __APPLE__ */
Note: See TracBrowser for help on using the repository browser.