source: src/io/source_apple_audio.c @ 98874a6

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

src/io/source_{sndfile,apple_audio}.c: quiet samplerate magic

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