source: src/io/sink_apple_audio.c @ 8a7b344

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

src/io/sink_apple_audio.c: warn before fixing size

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