summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2010-07-25 09:55:44 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2010-07-25 10:07:55 +0800
commit359f4c5513cc2534a8478528ceff5cdcfbbde96b (patch)
treebc792862b3fa5b0ed9b1039d4dce6f3186603874 /src
parente81cf6e4f5242d4a1f40c4921d1861fe2f6c9421 (diff)
coreaudio: cleanup a bit
Diffstat (limited to 'src')
-rw-r--r--src/device_coreaudio.cpp57
1 files changed, 34 insertions, 23 deletions
diff --git a/src/device_coreaudio.cpp b/src/device_coreaudio.cpp
index 691af65..3e27894 100644
--- a/src/device_coreaudio.cpp
+++ b/src/device_coreaudio.cpp
@@ -10,6 +10,8 @@
#include "device_coreaudio.h"
#include "debug.h"
+#define kOutputBus 0
+#define kInputBus 1
namespace audiere {
@@ -22,17 +24,14 @@ namespace audiere {
memset(&desc, 0, sizeof(desc));
desc.mFormatID = kAudioFormatLinearPCM;
desc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
- desc.mChannelsPerFrame = 2;
- desc.mSampleRate = 44100;
-
- desc.mBitsPerChannel = 16;
desc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
+ desc.mSampleRate = 44100;
+ desc.mChannelsPerFrame = 2;
+ desc.mBitsPerChannel = 16;
desc.mFramesPerPacket = 1;
- desc.mBytesPerFrame = desc.mBitsPerChannel * \
- desc.mChannelsPerFrame / 8;
- desc.mBytesPerPacket = desc.mBytesPerFrame * \
- desc.mFramesPerPacket;
+ desc.mBytesPerFrame = desc.mBitsPerChannel * desc.mChannelsPerFrame / 8;
+ desc.mBytesPerPacket = desc.mBytesPerFrame * desc.mFramesPerPacket;
#ifndef __IPHONE_OS_VERSION_MIN_REQUIRED
// Locate the default output audio unit
@@ -86,7 +85,7 @@ namespace audiere {
/* Open & initialize the audio unit */
result = OpenAComponent(comp, &unit);
- if (!result) {
+ if (result != noErr) {
ADR_LOG("CoreAudio: Couldn't open a audio unit.\n");
return 0;
}
@@ -118,7 +117,7 @@ namespace audiere {
/* Open & initialize the audio unit */
result = AudioComponentInstanceNew(comp, &unit);
- if (!result) {
+ if (result != noErr) {
ADR_LOG("CoreAudio: Couldn't open a audio unit.\n");
return 0;
}
@@ -126,46 +125,48 @@ namespace audiere {
UInt32 enableIO = 0;
result = AudioUnitSetProperty(unit,
kAudioOutputUnitProperty_EnableIO,
- kAudioUnitScope_Input, 0,
+ kAudioUnitScope_Input, kInputBus,
&enableIO, sizeof(enableIO));
- if (!result) {
- ADR_LOG("CoreAudio: Couldn't enable io for the audio unit.\n");
+ if (result != noErr) {
+ ADR_LOG("CoreAudio: Couldn't disable input for the audio unit.\n");
return 0;
}
enableIO = 1;
result = AudioUnitSetProperty(unit,
kAudioOutputUnitProperty_EnableIO,
- kAudioUnitScope_Output, 1,
+ kAudioUnitScope_Output, kOutputBus,
&enableIO, sizeof(enableIO));
- if (!result) {
- ADR_LOG("CoreAudio: Couldn't enable io for the audio unit.\n");
+ if (result != noErr) {
+ ADR_LOG("CoreAudio: Couldn't enable output for the audio unit.\n");
return 0;
}
#endif
+
// Set the input format of the audio unit.
result = AudioUnitSetProperty(unit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
- 0,
+ kOutputBus,
&desc,
sizeof(desc));
if (result != noErr) {
- ADR_LOG ("CoreAudio: Failed to set AudioUnitSetProperty.");
+ ADR_LOG ("CoreAudio: Couldn't to set stream format.");
#ifndef __IPHONE_OS_VERSION_MIN_REQUIRED
CloseComponent(unit);
#endif
return 0;
}
-#ifndef __IPHONE_OS_VERSION_MIN_REQUIRED
result = AudioUnitInitialize(unit);
if (result != noErr) {
ADR_LOG ("CoreAudio: Couldn't initialize the audio unit.");
+#ifndef __IPHONE_OS_VERSION_MIN_REQUIRED
CloseComponent(unit);
+#endif
return 0;
}
-#endif
+
return new CAAudioDevice(unit);
}
@@ -184,7 +185,8 @@ namespace audiere {
callback.inputProcRefCon = this;
result = AudioUnitSetProperty(m_unit,
kAudioUnitProperty_SetRenderCallback,
- kAudioUnitScope_Input, 0, &callback, sizeof(callback));
+ kAudioUnitScope_Input, kOutputBus,
+ &callback, sizeof(callback));
if (result != noErr)
ADR_LOG("CoreAudio: Couldn't set render callback.\n");
@@ -204,10 +206,13 @@ namespace audiere {
memset(&callback, 0, sizeof(AURenderCallbackStruct));
result = AudioUnitSetProperty(m_unit,
kAudioUnitProperty_SetRenderCallback,
- kAudioUnitScope_Input, 0, &callback, sizeof(callback));
+ kAudioUnitScope_Input, kOutputBus,
+ &callback, sizeof(callback));
#ifndef __IPHONE_OS_VERSION_MIN_REQUIRED
result = CloseComponent(m_unit);
+#else
+ result = AudioUnitUninitialize(m_unit);
#endif
}
@@ -219,6 +224,7 @@ namespace audiere {
AudioBufferList * ioData) {
CAAudioDevice* device = static_cast<CAAudioDevice*>(inRefCon);
+ ADR_LOG("fillInput\n");
for (UInt32 i = 0; i < ioData->mNumberBuffers; i++) {
AudioBuffer *abuf;
UInt32 remaining, len;
@@ -229,7 +235,12 @@ namespace audiere {
ptr = abuf->mData;
while (remaining > 0) {
- len = device->read(remaining / 4, ptr);
+ if (device) {
+ len = device->read(remaining / 4, ptr);
+ } else {
+ len = remaining / 4;
+ memset(ptr, 0, remaining);
+ }
ptr = (char *)ptr + len;
remaining -= len * 4;
}