summaryrefslogtreecommitdiff
path: root/audio_coreaudio.c
blob: 6bfaf93006640021b8e5af3b521a5309b7420ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461

/*
 * for CoreAudio
 *
 * If the playback/recording sound device is not stereo and its sample
 * size is not float (32 bit), this will not work.
 *
 * Please let me know if there is such a case.
 *
 * Shawn Hsiao <phsiao@mac.com>
 * Some enhancement by Masanori Sekino <msek@users.sourceforge.net>
 */

#include <CoreAudio/CoreAudio.h>
#include <limits.h>
#include <pthread.h>

#define BUF_SIZE ESD_BUF_SIZE

static AudioDeviceID gOutputDeviceID, gInputDeviceID;

static float OutputDataBuf[BUF_SIZE];
static float InputDataBuf[BUF_SIZE];
static int OutputWroteSamples = 0;
static int InputReadSamples = 0;

static pthread_mutex_t mutexOutput, mutexInput;
static pthread_cond_t condOutput, condInput;

static int audioPlaybackStarted = 0, audioRecordStarted = 0;
static int coreaudio_has_output_device = 0;
static int coreaudio_has_input_device = 0;
static int num_ca_input_channel = 0;
static int coreaudio_init = 0;

OSStatus PlaybackIOProc(AudioDeviceID inDevice,
			const AudioTimeStamp *inNow,
			const AudioBufferList *inInputData,
			const AudioTimeStamp *inInputTime,
			AudioBufferList *outOutputData,
			const AudioTimeStamp *inOutputTime,
			void *inClientData)
{
  float *bufPtr = outOutputData->mBuffers[0].mData;
  int i;

  pthread_mutex_lock(&mutexOutput);

  for (i = 0; i < OutputWroteSamples; i++)
    bufPtr[i] = OutputDataBuf[i];
  for ( ; i < BUF_SIZE; i++)
    bufPtr[i] = 0;
  OutputWroteSamples = 0;

  pthread_mutex_unlock(&mutexOutput);
  pthread_cond_signal(&condOutput);

  return (kAudioHardwareNoError);
}

OSStatus RecordIOProc(AudioDeviceID inDevice,
		      const AudioTimeStamp *inNow,
		      const AudioBufferList *inInputData,
		      const AudioTimeStamp *inInputTime,
		      AudioBufferList *outOutputData,
		      const AudioTimeStamp *inOutputTime,
		      void *inClientData)
{
  float *bufPtr = inInputData->mBuffers[0].mData;
  int i;

  pthread_mutex_lock(&mutexInput);

  if (num_ca_input_channel == 2) {
    for (i = 0; i < BUF_SIZE; i++)
      InputDataBuf[i] = bufPtr[i];
  }
  else {
    for (i = 0; i < BUF_SIZE/2; i++)
      InputDataBuf[2*i] = bufPtr[i];
  }
  InputReadSamples = 0;

  pthread_mutex_unlock(&mutexInput);
  pthread_cond_signal(&condInput);

  return (kAudioHardwareNoError);
}

#define ARCH_esd_audio_devices
const char *esd_audio_devices()
{
    return "coreaudio API only";
}

/*
 * This is called to reset the device status.
 * Returns -2 to indicate the device failed to initialize;
 * returns  -1 means any of rate/size/{mono,stereo} mismatched.
 */
#define ARCH_esd_audio_open
int esd_audio_open()
{
#define LEN_DEVICE_NAME 128
  OSStatus status;
  UInt32 propertySize, bufferByteCount;
  char deviceName[LEN_DEVICE_NAME];
  struct AudioStreamBasicDescription streamDesc;
  int rval;

  /*
   * We only need to do this once, the rest are taken cared by
   * disable/enable calback.
   */
  if (coreaudio_init) {
    return (0);
  }

  /********************** playback section ***************************/
  /* get default output device */
  propertySize = sizeof(gOutputDeviceID);
  status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
	                            &propertySize,
				    &gOutputDeviceID);
  if (status) {
    fprintf(stderr, "get default output device failed, status = %d\n",
	    (int)status);
    return (-2);
  }

  if (gOutputDeviceID != kAudioDeviceUnknown) {
    /* got default output device */
    coreaudio_has_output_device = 1;

    /* get output device name */
    propertySize = sizeof(char)*LEN_DEVICE_NAME;
    status = AudioDeviceGetProperty(gOutputDeviceID,
				    1,
				    0,
				    kAudioDevicePropertyDeviceName,
				    &propertySize,
				    deviceName);
    if (status) {
      fprintf(stderr, "get device name failed, status = %d\n",
	      (int)status);
      return (-2);
    }

    /* get output format */
    propertySize = sizeof(struct AudioStreamBasicDescription);
    status = AudioDeviceGetProperty(gOutputDeviceID,
				    1,
				    0,
				    kAudioDevicePropertyStreamFormat,
				    &propertySize,
				    &streamDesc);
    if (status) {
      fprintf(stderr, "get device property failed, status = %d\n",
	      (int)status);
      return (-2);
    }

    if ((streamDesc.mSampleRate != 44100.0) ||
        (streamDesc.mFormatID != kAudioFormatLinearPCM) ||
       !(streamDesc.mFormatFlags & kLinearPCMFormatFlagIsFloat) ||
        (streamDesc.mChannelsPerFrame != 2))
    {
      fprintf (stderr, "unsupported device.\n");
      return (-2);
    }

    /* set buffer size */
    bufferByteCount = BUF_SIZE * sizeof(float);
    propertySize = sizeof(bufferByteCount);
    status = AudioDeviceSetProperty(gOutputDeviceID,
                                    0,
                                    0,
                                    0,
                                    kAudioDevicePropertyBufferSize,
                                    propertySize,
                                    &bufferByteCount);
    if (status) {
      fprintf(stderr, "set device property failed, status = %d\n",
              (int)status);
    }

    fprintf(stderr, "using device %s for output:\n", deviceName);
    fprintf(stderr, "\twith sample rate %f, %ld channels and %ld-bit sample\n",
	    streamDesc.mSampleRate,
	    streamDesc.mChannelsPerFrame,
	    streamDesc.mBitsPerChannel);

    rval = pthread_mutex_init(&mutexOutput, NULL);
    if (rval) {
      fprintf(stderr, "mutex init failed\n");
      return (-1);
    }
    rval = pthread_cond_init(&condOutput, NULL);
    if (rval) {
      fprintf(stderr, "condition init failed\n");
      return (-1);
    }

    /* Registers PlaybackIOProc with the device without activating it. */
    status = AudioDeviceAddIOProc(gOutputDeviceID, PlaybackIOProc, (void *)1);
  }

  /********************** record section ***************************/
  /* get default input device */
  propertySize = sizeof(gInputDeviceID);
  status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice,
	                            &propertySize,
				    &gInputDeviceID);
  if (status) {
    fprintf(stderr, "get default input device failed, status = %d\n",
	    (int)status);
    return (-2);
  }

  if (gInputDeviceID != kAudioDeviceUnknown) {
    /* got default input device */
    coreaudio_has_input_device = 1;

    /* get input device name */
    propertySize = sizeof(char)*64;
    status = AudioDeviceGetProperty(gInputDeviceID,
				    1,
				    1,
				    kAudioDevicePropertyDeviceName,
				    &propertySize,
				    deviceName);
    if (status) {
      fprintf(stderr, "get device name failed, status = %d\n",
	      (int)status);
      return (-2);
    }

    /* get input format */
    propertySize = sizeof(struct AudioStreamBasicDescription);
    status = AudioDeviceGetProperty(gInputDeviceID,
				    1,
				    1,
				    kAudioDevicePropertyStreamFormat,
				    &propertySize,
				    &streamDesc);
    if (status) {
      fprintf(stderr, "get device property failed, status = %d\n",
	      (int)status);
      return (-2);
    }

    if ((streamDesc.mSampleRate != 44100.0) ||
        (streamDesc.mFormatID != kAudioFormatLinearPCM) ||
       !(streamDesc.mFormatFlags & kLinearPCMFormatFlagIsFloat) ||
        (streamDesc.mChannelsPerFrame != 2 &&
	 streamDesc.mChannelsPerFrame != 1))
    {
      fprintf (stderr, "unsupported device.\n");
      return (-2);
    }

    num_ca_input_channel = streamDesc.mChannelsPerFrame;

    /* set buffer size */
    bufferByteCount = BUF_SIZE / (2/num_ca_input_channel) * sizeof(float);
    propertySize = sizeof(bufferByteCount);
    status = AudioDeviceSetProperty(gInputDeviceID,
                                    0,
                                    0,
                                    1,
                                    kAudioDevicePropertyBufferSize,
                                    propertySize,
                                    &bufferByteCount);
    if (status) {
      fprintf(stderr, "set device property failed, status = %d\n",
              (int)status);
    }

    fprintf(stderr, "using device %s for input:\n", deviceName);
    fprintf(stderr, "\twith sample rate %f, %ld channels and %ld-bit sample\n",
	    streamDesc.mSampleRate,
	    streamDesc.mChannelsPerFrame,
	    streamDesc.mBitsPerChannel);

    rval = pthread_mutex_init(&mutexInput, NULL);
    if (rval) {
      fprintf(stderr, "mutex init failed\n");
      return (-1);
    }
    rval = pthread_cond_init(&condInput, NULL);
    if (rval) {
      fprintf(stderr, "condition init failed\n");
      return (-1);
    }

    /* Registers PlaybackIOProc with the device without activating it. */
    status = AudioDeviceAddIOProc(gInputDeviceID, RecordIOProc, (void *)1);
  }

  if (!coreaudio_has_output_device) {
    fprintf(stderr, "unknown output device.\n");
    return (-2);
  }
  /* Allow lack of recording device */
  /*
  if (!coreaudio_has_input_device) {
    fprintf(stderr, "unknown input device.\n");
    return (-2);
  }
  */

  /* Indicates the initialization is done */
  coreaudio_init = 1;

  esd_audio_fd = 0; /* this is meaningless anyway */

  return 0;
}

/*
 * This is called to reset the device status.
 * (before calls esd_audio_open again)
 */
#define ARCH_esd_audio_close
void esd_audio_close()
{
  OSStatus status;

  /* deactivate both of them */
  if (coreaudio_has_output_device && audioPlaybackStarted) {
    status = AudioDeviceStop(gOutputDeviceID, PlaybackIOProc);
    audioPlaybackStarted = 0;
  }

  if (coreaudio_has_input_device && audioRecordStarted) {
    status = AudioDeviceStop(gInputDeviceID, RecordIOProc);
    audioRecordStarted = 0;
  }

  return;
}


#define ARCH_esd_audio_pause
void esd_audio_pause()
{
  OSStatus status;

  if (coreaudio_has_output_device && audioPlaybackStarted) {
    status = AudioDeviceStop(gOutputDeviceID, PlaybackIOProc);
    audioPlaybackStarted = 0;
  }

  if (coreaudio_has_input_device && audioRecordStarted) {
    status = AudioDeviceStop(gInputDeviceID, RecordIOProc);
    audioRecordStarted = 0;
  }

  return;
}


#define ARCH_esd_audio_write
/*******************************************************************/
/* dump a buffer to the sound device */
int esd_audio_write( void *buffer, int buf_size )
{
  OSStatus status;
  float scale = 1.0 / SHRT_MAX;
  int remain_to_write = buf_size;

  if (!coreaudio_has_output_device)
    return -1;

  if (!audioPlaybackStarted) {
    status = AudioDeviceStart(gOutputDeviceID, PlaybackIOProc);
    audioPlaybackStarted = 1;
  }

  while (remain_to_write)
  {
    pthread_mutex_lock(&mutexOutput);

    while(OutputWroteSamples == BUF_SIZE)
      pthread_cond_wait(&condOutput, &mutexOutput);

    {
      short *src_data = (short *)buffer + (buf_size - remain_to_write) / sizeof(short);
      float *dst_data = OutputDataBuf + OutputWroteSamples;
      int src_samples = remain_to_write / sizeof(short);
      int dst_samples = BUF_SIZE - OutputWroteSamples;
      int n = (dst_samples < src_samples) ? dst_samples : src_samples;
      int i;

      for (i = 0; i < n; i++)
        dst_data[i] = scale * src_data[i];

      OutputWroteSamples += n;
      remain_to_write -= n * sizeof(short);
    }

    pthread_mutex_unlock(&mutexOutput);
  }

  return (buf_size);
}

#define ARCH_esd_audio_read
/*******************************************************************/
/* read a chunk from the sound device */
int esd_audio_read( void *buffer, int buf_size )
{
  OSStatus status;
  float scale = SHRT_MAX;
  int remain_to_read = buf_size;

  if (!coreaudio_has_input_device)
    return -1;

  if (!audioRecordStarted) {
    status = AudioDeviceStart(gInputDeviceID, RecordIOProc);
    audioRecordStarted = 1;
  }

  while (remain_to_read)
  {
    pthread_mutex_lock(&mutexInput);

    while(InputReadSamples == BUF_SIZE)
      pthread_cond_wait(&condInput, &mutexInput);

    {
      float *src_data = InputDataBuf + InputReadSamples;
      short *dst_data = (short *)buffer + (buf_size - remain_to_read) / sizeof(short);
      int src_samples = BUF_SIZE - InputReadSamples;
      int dst_samples = remain_to_read / sizeof(short);
      int n = (dst_samples < src_samples) ? dst_samples : src_samples;
      int i;

      for (i = 0; i < n; i++)
        dst_data[i] = (short)(scale * src_data[i]);

      InputReadSamples += n;
      remain_to_read -= n * sizeof(short);
    }

    pthread_mutex_unlock(&mutexInput);
  }

  return (buf_size);
}


#define ARCH_esd_audio_flush
/*******************************************************************/
/* flush the audio buffer */
void esd_audio_flush()
{
  /* nothing needed */
  return;
}