-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAudioUnitQueueManager.cpp
147 lines (120 loc) · 4.45 KB
/
AudioUnitQueueManager.cpp
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
/*
Frodo, Commodore 64 emulator for the iPhone
Copyright (C) 2007-2010 Stuart Carnie
See gpl.txt for license information.
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SIDRenderer.h"
#if defined(USE_AUDIO_UNIT)
#include "AudioUnitQueueManager.h"
#define checkStatus(status)
#define kOutputBus 0
#define kInputBus 1
CAudioUnitQueueManager::CAudioUnitQueueManager(SIDRenderer *renderer, float sampleFrequency, SoundChannels channels):_renderer(renderer)
{
OSStatus status;
// Describe audio component
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
// Get component
AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);
// Get audio units
status = AudioComponentInstanceNew(inputComponent, &_audioUnit);
checkStatus(status);
// Enable IO for playback
UInt32 flag = 0;
status = AudioUnitSetProperty(_audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
kInputBus,
&flag,
sizeof(flag));
flag = 1;
status = AudioUnitSetProperty(_audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
kOutputBus,
&flag,
sizeof(flag));
checkStatus(status);
// Describe format
_audioFormat.mSampleRate = sampleFrequency;
_audioFormat.mFormatID = kAudioFormatLinearPCM;
_audioFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
//_audioFormat.mFormatFlags = kLinearPCMFormatFlagsAreAllClear;
_audioFormat.mFramesPerPacket = 1;
_audioFormat.mChannelsPerFrame = channels;
_audioFormat.mBitsPerChannel = 16;
_audioFormat.mBytesPerPacket = 2 * channels;
_audioFormat.mBytesPerFrame = 2 * channels;
// Apply format
status = AudioUnitSetProperty(_audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
kInputBus,
&_audioFormat,
sizeof(_audioFormat));
checkStatus(status);
status = AudioUnitSetProperty(_audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
kOutputBus,
&_audioFormat,
sizeof(_audioFormat));
checkStatus(status);
AURenderCallbackStruct callbackStruct;
// Set output callback
callbackStruct.inputProc = CAudioUnitQueueManager::playbackCallback;
callbackStruct.inputProcRefCon = this;
status = AudioUnitSetProperty(_audioUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Global,
kOutputBus,
&callbackStruct,
sizeof(callbackStruct));
checkStatus(status);
}
CAudioUnitQueueManager::~CAudioUnitQueueManager() {
}
OSStatus CAudioUnitQueueManager::playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
CAudioUnitQueueManager *THIS = (CAudioUnitQueueManager*)inRefCon;
THIS->_renderer->fill_buffer((uint8*)ioData->mBuffers[0].mData, (uint32*)&ioData->mBuffers[0].mDataByteSize);
if (!ioData->mBuffers[0].mDataByteSize)
*ioActionFlags |= kAudioUnitRenderAction_OutputIsSilence;
return noErr;
}
void CAudioUnitQueueManager::start() {
// Initialise
OSStatus status = AudioUnitInitialize(_audioUnit);
checkStatus(status);
status = AudioOutputUnitStart(_audioUnit);
}
void CAudioUnitQueueManager::stop() {
OSStatus status = AudioOutputUnitStop(_audioUnit);
AudioUnitUninitialize(_audioUnit);
}
void CAudioUnitQueueManager::pause() {
OSStatus status = AudioOutputUnitStop(_audioUnit);
}
void CAudioUnitQueueManager::resume() {
OSStatus status = AudioOutputUnitStart(_audioUnit);
}
#endif