-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_osx.cpp
121 lines (97 loc) · 3.25 KB
/
audio_osx.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
#include <stdio.h>
#include <unistd.h>
#include <AssertMacros.h>
#include <CoreFoundation/CoreFoundation.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioUnit/AUComponent.h>
#include <AudioUnit/AudioComponent.h>
#include <math.h>
void synthProduceStream(short *buffer, int samples);
AudioUnit gOutputUnit;
UInt32 FinishedFrames;
OSStatus AudRenderCallback(void* inRefCon,
AudioUnitRenderActionFlags* Flags,
const AudioTimeStamp* ioActionFlags,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList* ioData)
{
synthProduceStream((short*)ioData->mBuffers[0].mData, inNumberFrames);
FinishedFrames += inNumberFrames;
for(UInt32 channel = 1; channel < ioData->mNumberBuffers; channel++)
memcpy(ioData->mBuffers[channel].mData, ioData->mBuffers[0].mData, ioData->mBuffers[0].mDataByteSize);
//FinishedFrames += inNumberFrames;
return noErr;
}
void beginStreamSound()
{
OSStatus err = noErr;
err = AudioOutputUnitStart(gOutputUnit);
verify(!err);
}
void endStreamSound()
{
verify_noerr(AudioOutputUnitStop(gOutputUnit));
AudioComponentInstanceDispose(gOutputUnit);
}
void setupSound()
{
FinishedFrames = 0;
OSStatus err = noErr;
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
#if TARGET_OS_IPHONE
desc.componentSubType = kAudioUnitSubType_RemoteIO;
#else
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
#endif
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
verify(comp);
err = AudioComponentInstanceNew(comp, &gOutputUnit);
verify(!err);
err = AudioUnitInitialize(gOutputUnit);
verify(!err);
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = 44100.0;
streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger
| kAudioFormatFlagsNativeEndian
| kLinearPCMFormatFlagIsPacked
| kAudioFormatFlagIsNonInterleaved;
streamFormat.mBytesPerPacket = 2;
streamFormat.mFramesPerPacket = 1;
streamFormat.mBytesPerFrame = 2;
streamFormat.mChannelsPerFrame = 2;
streamFormat.mBitsPerChannel = 16;
err = AudioUnitSetProperty(gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&streamFormat,
sizeof(AudioStreamBasicDescription));
verify(!err);
err = AudioUnitInitialize(gOutputUnit);
verify(!err);
AURenderCallbackStruct input;
input.inputProc = AudRenderCallback;
input.inputProcRefCon = NULL;
err = AudioUnitSetProperty (gOutputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&input,
sizeof(input));
verify(err);
//err = AudioUnitAddRenderNotify(gOutputUnit, AudRenderCallback, NULL);
//verify(!err);
}
void makeNoise()
{
setupSound();
beginStreamSound();
CFRunLoopRun();
endStreamSound();
}