FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ConvolutionTest/ConvolutionUnit/ConvolutionUnitAudioUnit.mm at master · Pescolly/ConvolutionTest · GitHub
Pescolly
/
ConvolutionTest
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ConvolutionTest
/
ConvolutionUnit
/
ConvolutionUnitAudioUnit.mm
Copy path
More file actions
More file actions
Latest commit
History
History
History
175 lines (139 loc) · 5.4 KB
Breadcrumbs
ConvolutionTest
/
ConvolutionUnit
/
ConvolutionUnitAudioUnit.mm
Copy path
File metadata and controls
175 lines (139 loc) · 5.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
//
ConvolutionUnitAudioUnit.m
//
ConvolutionUnit
//
//
Created by armen karamian on 5/21/17.
//
Copyright © 2017 armen karamian. All rights reserved.
//
#
import
"
ConvolutionUnitAudioUnit.h
"
#
import
"
BufferedAudioBus.hpp
"
#
import
<
AVFoundation/AVFoundation.h
>
//
Define parameter addresses.
const
AudioUnitParameterID myParam1 =
0
;
@interface
ConvolutionUnitAudioUnit
()
@property
AUAudioUnitBus
*outputBus;
@property
AUAudioUnitBusArray
*inputBusArray;
@property
AUAudioUnitBusArray
*outputBusArray;
@property
(
nonatomic
,
readwrite
)
AUParameterTree
*parameterTree;
@end
@implementation
ConvolutionUnitAudioUnit
{
BufferedInputBus _inputBus1;
BufferedInputBus _inputBus2;
}
@synthesize
parameterTree = _parameterTree;
- (
instancetype
)
initWithComponentDescription
:
(AudioComponentDescription)
componentDescription
options
:
(AudioComponentInstantiationOptions)
options
error
:
(
NSError
**)
outError
{
self = [
super
initWithComponentDescription:
componentDescription
options:
options
error:
outError];
if
(self ==
nil
) {
return
nil
; }
AVAudioFormat *defaultFormat = [[AVAudioFormat
alloc
]
initWithCommonFormat:
AVAudioPCMFormatFloat32
sampleRate:
44100
channels:
2
interleaved:
false
];
_inputBus1.
init
(defaultFormat,
2
);
_inputBus2.
init
(defaultFormat,
2
);
_outputBus = [[
AUAudioUnitBus
alloc
]
initWithFormat:
defaultFormat
error:
nil
];
//
Create the input and output bus arrays.
_inputBusArray = [[
AUAudioUnitBusArray
alloc
]
initWithAudioUnit:
self
busType:
AUAudioUnitBusTypeInput
busses:
@[_inputBus1.bus, _inputBus2.bus]];
_outputBusArray = [[
AUAudioUnitBusArray
alloc
]
initWithAudioUnit:
self
busType:
AUAudioUnitBusTypeOutput
busses:
@[_outputBus]];
self.
maximumFramesToRender
=
512
;
return
self;
}
#
pragma mark
- AUAudioUnit Overrides
//
If an audio unit has input, an audio unit's audio input connection points.
//
Subclassers must override this property getter and should return the same object every time.
//
See sample code.
- (
AUAudioUnitBusArray
*)
inputBusses
{
#
warning
implementation must return non-nil AUAudioUnitBusArray
return
_inputBusArray;
}
//
An audio unit's audio output connection points.
//
Subclassers must override this property getter and should return the same object every time.
//
See sample code.
- (
AUAudioUnitBusArray
*)
outputBusses
{
#
warning
implementation must return non-nil AUAudioUnitBusArray
return
_outputBusArray;
}
//
Allocate resources required to render.
//
Subclassers should call the superclass implementation.
- (
BOOL
)
allocateRenderResourcesAndReturnError
:
(
NSError
**)
outError
{
if
(![
super
allocateRenderResourcesAndReturnError:
outError])
{
return
NO
;
}
//
Validate that the bus formats are compatible.
//
Allocate your resources.
return
YES
;
}
//
Deallocate resources allocated in allocateRenderResourcesAndReturnError:
//
Subclassers should call the superclass implementation.
- (
void
)
deallocateRenderResources
{
//
Deallocate your resources.
[
super
deallocateRenderResources
];
}
-(
bool
)
canPerformInput
{
return
true
;
}
#
pragma mark
- AUAudioUnit (AUAudioUnitImplementation)
//
Block which subclassers must provide to implement rendering.
- (
AUInternalRenderBlock
)
internalRenderBlock
{
//
Capture in locals to avoid ObjC member lookups. If "self" is captured in render, we're doing it wrong. See sample code.
//
__block BufferedInputBus *input = &_inputBus;
return
^
AUAudioUnitStatus
(AudioUnitRenderActionFlags *actionFlags,
const
AudioTimeStamp *timestamp,
AVAudioFrameCount frameCount,
NSInteger
outputBusNumber,
AudioBufferList *outputData,
const
AURenderEvent
*realtimeEventListHead,
AURenderPullInputBlock
pullInputBlock)
{
AudioUnitRenderActionFlags pullFlags =
0
;
/*
Important:
If the caller passed non-null output pointers (outputData->mBuffers[x].mData), use those.
If the caller passed null output buffer pointers, process in memory owned by the Audio Unit
and modify the (outputData->mBuffers[x].mData) pointers to point to this owned memory.
The Audio Unit is responsible for preserving the validity of this memory until the next call to render,
or deallocateRenderResources is called.
If your algorithm cannot process in-place, you will need to preallocate an output buffer
and use it here.
See the description of the canProcessInPlace property.
*/
//
If passed null output buffer pointers, process in-place in the input buffer.
//
AudioBufferList *outAudioBufferList = outputData;
//
if (outAudioBufferList->mBuffers[0].mData == nullptr)
//
{
//
for (UInt32 i = 0; i < outAudioBufferList->mNumberBuffers; ++i)
//
{
//
outAudioBufferList->mBuffers[i].mData = inAudioBufferList->mBuffers[i].mData;
//
}
//
}
//
test sin wave render
double
j =
0
;
double
cycleLength =
44100.0
/
40000
;
float
angleDelta = cycleLength *
2.0
*
M_PI
;
//
generate 440 hz tone and load into output bufferes
for
(
int
frame =
0
; frame < frameCount; ++frame)
{
Float32 * buff[frameCount];
outputData->
mBuffers
[
0
].
mData
= buff;
Float32 *data = (Float32*)outputData->
mBuffers
[
0
].
mData
;
(data)[frame] = (Float32)
sin
(j);
Float32 * buff2[frameCount];
outputData->
mBuffers
[
1
].
mData
= buff2;
Float32 *data2 = (Float32*)outputData->
mBuffers
[
1
].
mData
;
(data2)[frame] = (Float32)
sin
(j);
//
advance cout
j += angleDelta;
}
return
noErr;
};
}
@end
Back
|
FazBrowse Home
|
New Git URL