-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAudioCoreDriverNew.cpp
More file actions
executable file
·311 lines (264 loc) · 12.2 KB
/
Copy pathAudioCoreDriverNew.cpp
File metadata and controls
executable file
·311 lines (264 loc) · 12.2 KB
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
/*
*
* Copyright (c) 2023 Alexander Coers
* All rights reserved.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "PlayerLibSidplayWrapper.h"
#include "AudioCoreDriverNew.h"
// ----------------------------------------------------------------------------
AudioCoreDriverNew::AudioCoreDriverNew()
// ----------------------------------------------------------------------------
{
mIsInitialized = false;
}
// ----------------------------------------------------------------------------
AudioCoreDriverNew::~AudioCoreDriverNew()
// ----------------------------------------------------------------------------
{
stopPlayback();
AudioUnitUninitialize (gOutputUnit);
AudioComponentInstanceDispose (gOutputUnit);
delete[] mSampleBuffer;
mIsInitialized = false;
}
// ----------------------------------------------------------------------------
void AudioCoreDriverNew::initialize(PlayerLibSidplayWrapper* player, int sampleRate, int bitsPerSample)
// ----------------------------------------------------------------------------
{
//printf("init core audio\n");
mPlayer = player;
mIsPlaying = false;
mIsPlayingPreRenderedBuffer = false;
mBufferUnderrunDetected = false;
mBufferUnderrunCount = 0;
mPreRenderedBuffer = NULL;
mPreRenderedBufferSampleCount = 0;
mPreRenderedBufferPlaybackPosition = 0;
mSampleRate = DEFAULT_SAMPLERATE; //sampleRate
mNumSamplesInAudioBuffer = 512;
numberOfBytesInAudioBuffer = mNumSamplesInAudioBuffer * sizeof(short);
if (!mIsInitialized)
{
// Set up our audio format -- signed interleaved shorts (-32767 -> 32767), 16 bit stereo
// The iphone does not want to play back float32s.
// set up audio unit description
mDesc.componentType = kAudioUnitType_Output;
mDesc.componentSubType = kAudioUnitSubType_DefaultOutput;
mDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
mDesc.componentFlags = 0;
mDesc.componentFlagsMask = 0;
// get next component
AudioComponent comp = AudioComponentFindNext(NULL, &mDesc);
if (comp == NULL) { printf ("No audio component found.\n"); return; }
OSStatus err = AudioComponentInstanceNew(comp, &gOutputUnit);
if (err) { printf (" open component failed %d\n", (int)err); return;}
// Set up a callback function to generate output to the output unit
AURenderCallbackStruct input;
input.inputProc = MyRenderer;
input.inputProcRefCon = (void*)this;
err = AudioUnitSetProperty (gOutputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&input,
sizeof(input));
if (err) { printf ("AudioUnitSetProperty-CB=%ld\n", (long int)err); return; }
UInt32 streamDescSize = sizeof( mStreamFormat );
err = AudioUnitGetProperty( gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Global,
0,
&mStreamFormat,
&streamDescSize);
if (err) { printf ("AudioUnitGetProperty-CB=%ld\n", (long int)err); return; }
mStreamFormat.mSampleRate = DEFAULT_SAMPLERATE;
mStreamFormat.mFormatID = kAudioFormatLinearPCM;
mStreamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
mStreamFormat.mBytesPerPacket = 4; //2 * sizeof(short);
mStreamFormat.mFramesPerPacket = 1; // this means each packet in the AQ has two samples, one for each channel -> 4 bytes/frame/packet
mStreamFormat.mBytesPerFrame = 4; //2 * sizeof(short);
mStreamFormat.mChannelsPerFrame = 2;
mStreamFormat.mBitsPerChannel = 16;
numberOfChannelsPerFrame = mStreamFormat.mChannelsPerFrame;
err = AudioUnitSetProperty (gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&mStreamFormat,
sizeof(AudioStreamBasicDescription));
if (err) { printf ("AudioUnitSetProperty-SF=%4.4s, %ld\n", (char*)&err, (long int)err); return; }
err = AudioUnitAddPropertyListener (gOutputUnit,
kAudioUnitProperty_LastRenderError,
AUPropertyListener,
this);
if (err) { printf ("AudioUnitAddPropertyListener-CB=%ld\n", (long int)err); return; }
// Initialize unit
err = AudioUnitInitialize(gOutputUnit);
if (err) { printf ("AudioUnitInitialize=%ld\n", (long int)err); return; }
// alloc sample buffer
mSampleBuffer = new short[mNumSamplesInAudioBuffer];
memset(mSampleBuffer, 0, numberOfBytesInAudioBuffer);
}
//printf("init: OK\n");
mVolume = 1.0f;
mIsInitialized = true;
}
// ----------------------------------------------------------------------------
void AudioCoreDriverNew::fillBuffer()
// ----------------------------------------------------------------------------
{
if (!mIsPlaying)
{
return;
}
[mPlayer fillBuffer:mSampleBuffer withLen:numberOfBytesInAudioBuffer];
// reset value to full buffer
numberOfSamplesInAudioBuffer = mNumSamplesInAudioBuffer;
}
// ----------------------------------------------------------------------------
bool AudioCoreDriverNew::startPlayback()
// ----------------------------------------------------------------------------
{
//printf("trying to start playback\n");
if (!mIsInitialized)
return false;
if (mIsPlaying)
stopPlayback();
mIsPlaying = true;
//memset(mSampleBuffer, 0, sizeof(short) * mNumSamplesInBuffer);
fillBuffer();
// Start the rendering
// The DefaultOutputUnit will do any format conversions to the format of the default device
OSStatus err = AudioOutputUnitStart (gOutputUnit);
if (err) { printf ("AudioOutputUnitStart=%ld\n", (long int)err); return false; }
return true;
}
// ----------------------------------------------------------------------------
void AudioCoreDriverNew::stopPlayback()
// ----------------------------------------------------------------------------
{
if (!mIsInitialized)
return;
if (!mIsPlaying)
return;
// REALLY after you're finished playing STOP THE AUDIO OUTPUT UNIT!!!!!!
// but we never get here because we're running until the process is nuked...
OSStatus err = AudioOutputUnitStop (gOutputUnit);
if (err) { printf ("AudioOutputUnitStop=%ld\n", (long int)err); return; }
mIsPlaying = false;
}
// ----------------------------------------------------------------------------
void AudioCoreDriverNew::setVolume(float volume)
// ----------------------------------------------------------------------------
{
mVolume = volume;
}
#pragma mark Core Audio functions
// ________________________________________________________________________________
//
// Audio Unit Disconnect Handler
//
void AudioCoreDriverNew::AUPropertyListener(void *inRefCon,
AudioUnit inUnit,
AudioUnitPropertyID inID,
AudioUnitScope inScope,
AudioUnitElement inElement)
{
return;
}
// ________________________________________________________________________________
//
// Audio Unit Renderer!!!
//
OSStatus AudioCoreDriverNew::MyRenderer(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
// Get the info struct and a pointer to our output data
AudioCoreDriverNew* driverInstance = reinterpret_cast<AudioCoreDriverNew*>(inRefCon);
short* outAudioBuffer = (short*) ioData->mBuffers[0].mData;
short* inAudioBuffer = (short*) driverInstance->getSampleBuffer();
unsigned int numberOfBytesOutAudioBuffer = ioData->mBuffers[0].mDataByteSize;
float sample = 0.0f;
// available samples in outBuffer
// need to divide by 2, since we have left/right channel
unsigned int numberOfSamplesOutAudioBuffer = numberOfBytesOutAudioBuffer / sizeof(short) /2;
// set buffers...
short *audioOut = outAudioBuffer;
short *audioIn = &inAudioBuffer[driverInstance->mNumSamplesInAudioBuffer-driverInstance->numberOfSamplesInAudioBuffer];
// out buffer shall be organized like sample 0, sample 0, sample 1, sample 1
// duplicating the samples (audio buffer, make stereo left/right out of mono
// we always need to fill the entire outBuffer
// case 1
if (driverInstance->numberOfSamplesInAudioBuffer > numberOfSamplesOutAudioBuffer)
{
for (int x=0;x<numberOfSamplesOutAudioBuffer;x++) {
sample = driverInstance->mVolume * (*audioIn++);
*audioOut++ = (short)sample; // left
*audioOut++ = (short)sample; // right
}
driverInstance->numberOfSamplesInAudioBuffer -= numberOfSamplesOutAudioBuffer;
// NSLog(@"Case 1: copied %d samples (%d bytes)",numberOfSamplesOutAudioBuffer,numberOfBytesOutAudioBuffer);
} else
// case 2
if (driverInstance->numberOfSamplesInAudioBuffer < numberOfSamplesOutAudioBuffer)
{
int numberOfSamplesToCopy = numberOfSamplesOutAudioBuffer;
int tempSampleCopy = driverInstance->numberOfSamplesInAudioBuffer;
while (numberOfSamplesToCopy >0 ) {
for (int x=0;x<tempSampleCopy;x++) {
sample = driverInstance->mVolume * (*audioIn++);
*audioOut++ = (short)sample; // left
*audioOut++ = (short)sample; // right
}
// entire in buffer was copied?
if (tempSampleCopy == driverInstance->numberOfSamplesInAudioBuffer) {
//fill buffer up with fresh samples
driverInstance->fillBuffer();
audioIn = inAudioBuffer;
} else
driverInstance->numberOfSamplesInAudioBuffer -= tempSampleCopy;
//NSLog(@"Case 2: copied %d samples.",tempSampleCopy);
//numberOfBytesInAudioBuffer = sizeof(short) * numberOfSamplesInAudioBuffer;
numberOfSamplesToCopy -= tempSampleCopy;
if (numberOfSamplesToCopy >= driverInstance->numberOfSamplesInAudioBuffer)
tempSampleCopy = driverInstance->numberOfSamplesInAudioBuffer;
else
tempSampleCopy = numberOfSamplesToCopy;
}
} else
//case 3
if (driverInstance->numberOfSamplesInAudioBuffer == numberOfSamplesOutAudioBuffer)
{
for (int x=0;x<numberOfSamplesOutAudioBuffer;x++) {
sample = driverInstance->mVolume * (*audioIn++);
*audioOut++ = (short)sample; // left
*audioOut++ = (short)sample; // right
}
//fill buffer up with fresh samples
driverInstance->fillBuffer();
//audioIn = inAudioBuffer;
//NSLog(@"Case 3: copied %d samples (%d bytes)",numberOfSamplesOutAudioBuffer,numberOfBytesOutAudioBuffer);
}
return noErr;
}