📄 ao_macosx.c
字号:
/* * * ao_macosx.c * * Original Copyright (C) Timothy J. Wood - Aug 2000 * * This file is part of libao, a cross-platform library. See * README for a history of this source code. * * libao is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * libao is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Make; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * *//* The MacOS X CoreAudio framework doesn't mesh as simply as some simpler frameworks do. This is due to the fact that CoreAudio pulls audio samples rather than having them pushed at it (which is nice when you are wanting to do good buffering of audio). */#include <CoreAudio/AudioHardware.h>#include <stdio.h>#include <pthread.h>#include "ao/ao.h"#include "ao/plugin.h"// Set this to 1 to see FIFO debugging messages#define DEBUG_PIPE 0//#define BUFFER_COUNT (323)#define BUFFER_COUNT (2)#ifndef MIN#define MIN(a,b) ((a) < (b) ? (a) : (b))#endif#define true 1#define false 0static ao_info ao_macosx_info ={ AO_TYPE_LIVE, "MacOS X output", "macosx", "Timothy J. Wood <tjw@omnigroup.com>", "", AO_FMT_NATIVE, 30, NULL, 0};typedef struct ao_macosx_internal{ // Stuff describing the CoreAudio device AudioDeviceID outputDeviceID; AudioStreamBasicDescription outputStreamBasicDescription; // The amount of data CoreAudio wants each time it calls our IO function UInt32 outputBufferByteCount; // Keep track of whether the output stream has actually been started/stopped Boolean started; Boolean isStopping; // Synchronization objects between the CoreAudio thread and the enqueuing thread pthread_mutex_t mutex; pthread_cond_t condition; // Our internal queue of samples waiting to be consumed by CoreAudio void *buffer; unsigned int bufferByteCount; unsigned int firstValidByteOffset; unsigned int validByteCount; // Temporary debugging state unsigned int bytesQueued; unsigned int bytesDequeued;} ao_macosx_internal;// The function that the CoreAudio thread calls when it wants more datastatic OSStatus audioDeviceIOProc(AudioDeviceID inDevice, const AudioTimeStamp *inNow, const AudioBufferList *inInputData, const AudioTimeStamp *inInputTime, AudioBufferList *outOutputData, const AudioTimeStamp *inOutputTime, void *inClientData);int ao_plugin_test(){ if (/* FIXME */ 0 ) return 0; /* Cannot use this plugin with default parameters */ else { return 1; /* This plugin works in default mode */ }}ao_info *ao_plugin_driver_info(void){ return &ao_macosx_info;}int ao_plugin_device_init(ao_device *device){ ao_macosx_internal *internal; internal = (ao_macosx_internal *) malloc(sizeof(ao_macosx_internal)); if (internal == NULL) return 0; /* Could not initialize device memory */ device->internal = internal; return 1; /* Memory alloc successful */}int ao_plugin_set_option(ao_device *device, const char *key, const char *value){ ao_macosx_internal *internal = (ao_macosx_internal *) device->internal; /* No options */ return 1;}int ao_plugin_open(ao_device *device, ao_sample_format *format){ ao_macosx_internal *internal = (ao_macosx_internal *) device->internal; OSStatus status; UInt32 propertySize; int rc; if (format->rate != 44100) { fprintf(stderr, "ao_macosx_open: Only support 44.1kHz right now\n"); return 0; } if (format->channels != 2) { fprintf(stderr, "ao_macosx_open: Only two channel audio right now\n"); return 0; } propertySize = sizeof(internal->outputDeviceID); status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &propertySize, &(internal->outputDeviceID)); if (status) { fprintf(stderr, "ao_macosx_open: AudioHardwareGetProperty returned %d\n", (int)status); return 0; } if (internal->outputDeviceID == kAudioDeviceUnknown) { fprintf(stderr, "ao_macosx_open: AudioHardwareGetProperty: outputDeviceID is kAudioDeviceUnknown\n"); return 0; } propertySize = sizeof(internal->outputStreamBasicDescription); status = AudioDeviceGetProperty(internal->outputDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &propertySize, &internal->outputStreamBasicDescription); if (status) { fprintf(stderr, "ao_macosx_open: AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyStreamFormat\n", (int)status); return 0; } fprintf(stderr, "hardware format...\n"); fprintf(stderr, "%f mSampleRate\n", internal->outputStreamBasicDescription.mSampleRate); fprintf(stderr, "%c%c%c%c mFormatID\n", (int)(internal->outputStreamBasicDescription.mFormatID & 0xff000000) >> 24, (int)(internal->outputStreamBasicDescription.mFormatID & 0x00ff0000) >> 16, (int)(internal->outputStreamBasicDescription.mFormatID & 0x0000ff00) >> 8, (int)(internal->outputStreamBasicDescription.mFormatID & 0x000000ff) >> 0); fprintf(stderr, "%5d mBytesPerPacket\n", (int)internal->outputStreamBasicDescription.mBytesPerPacket); fprintf(stderr, "%5d mFramesPerPacket\n", (int)internal->outputStreamBasicDescription.mFramesPerPacket); fprintf(stderr, "%5d mBytesPerFrame\n", (int)internal->outputStreamBasicDescription.mBytesPerFrame); fprintf(stderr, "%5d mChannelsPerFrame\n", (int)internal->outputStreamBasicDescription.mChannelsPerFrame); if (internal->outputStreamBasicDescription.mFormatID != kAudioFormatLinearPCM) { fprintf(stderr, "ao_macosx_open: Default Audio Device doesn't support Linear PCM!\n"); return 0; } propertySize = sizeof(internal->outputBufferByteCount); internal->outputBufferByteCount = 8192; status = AudioDeviceSetProperty(internal->outputDeviceID, 0, 0, false, kAudioDevicePropertyBufferSize, propertySize, &internal->outputBufferByteCount); status = AudioDeviceGetProperty(internal->outputDeviceID, 0, false, kAudioDevicePropertyBufferSize, &propertySize, &internal->outputBufferByteCount); if (status) { fprintf(stderr, "ao_macosx_open: AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n", (int)status); return 0; } fprintf(stderr, "%5d outputBufferByteCount\n", (int)internal->outputBufferByteCount); // It appears that AudioDeviceGetProperty lies about this property in DP4 // Set the actual value //internal->outputBufferByteCount = 32768; // Set the IO proc that CoreAudio will call when it needs data, but don't start // the stream yet. internal->started = false; status = AudioDeviceAddIOProc(internal->outputDeviceID, audioDeviceIOProc, internal); if (status) { fprintf(stderr, "ao_macosx_open: AudioDeviceAddIOProc returned %d\n", (int)status); return 0; } rc = pthread_mutex_init(&internal->mutex, NULL); if (rc) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -