-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrm_audiomixer.pas
367 lines (329 loc) · 9.18 KB
/
rm_audiomixer.pas
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
unit rm_audiomixer;
// NOTE: it's assumed that all input streams' sample rates are all equal
{
bass, midrange, & treble frequencies in octaves
bass range in Hz
sub bass 31 to 62 (and lower)
mid bass 62 to 125
upper bass 125 to 250
midrange
lower midrange 250 to 500
mid midrange 500 to 1K
upper midrange 1K to 2K
treble
lower treble 2K to 4K
mid treble 4K to 8K
upper treble 8K to 16K (and higher)
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, RadioSystem, UComplex, RadioMessage, RadioModule, formaudiomixer,
SignalBasic, Math;
const
CACHE_SIZE = 50 * 1024; // we should use a large buffer here
type
TAudioStreamSetting = record
Cache: array [0..CACHE_SIZE-1] of Complex;
Cursor: Integer;
MixLen: Integer;
TotalGain: Double;
BassGain: Double;
TrebleGain: Double;
MixMethod: Integer;
Bass: TIIRFilter;
Treble: TIIRFilter;
end;
{ TRadioAudioMixer }
TRadioAudioMixer = class(TRadioModule)
private
FRegulator: TStreamRegulator;
FRate: Integer;
FStreams: array of TAudioStreamSetting;
FConfig: TAudioMixerForm;
procedure ReceiveMixedData(const P: PComplex; const Len: Integer);
procedure Mixing;
procedure SetupBassFilter(const Index: Integer);
procedure SetupTrebleFilter(const Index: Integer);
protected
function RMSetSampleRate(const Msg: TRadioMessage; const Rate: Cardinal): Integer;
override;
procedure ProccessCustomMessage(const Msg: TRadioMessage; var Ret: Integer); override;
procedure DoConfigure; override;
procedure Describe(Strs: TStrings); override;
procedure DoSyncDestroy; override;
public
constructor Create(RunQueue: TRadioRunQueue); override;
destructor Destroy; override;
procedure ReceiveData(const P: PComplex; const Len: Integer); override;
procedure ReceiveData(const Port: Integer; const P: PComplex; const Len: Integer); override;
end;
implementation
{ TRadioAudioMixer }
procedure TRadioAudioMixer.ReceiveMixedData(const P: PComplex;
const Len: Integer);
var
O: PComplex;
I: Integer;
begin
if Len <> DefOutput.BufferSize then
begin
TRadioLogger.Report(llWarn, 'TRadioAudioMixer.ReceiveMixedData: Len <> DefOutput.BufferSize, data lost');
Exit;
end;
O := Alloc(DefOutput, I);
if Assigned(O) then
begin
Move(P^, O^, Len * SizeOf(O^));
DefOutput.Broadcast(I, FDataListeners);
end;
end;
procedure TRadioAudioMixer.Mixing;
var
L: Integer = -1;
I: Integer;
F: Integer = -1;
T: array of Complex;
begin
for I := 0 to High(FStreams) do
begin
with FStreams[I] do
begin
if MixMethod = AUDIOMIXER_STREAM_OUTPUT_OFF then Continue;
if L < 0 then L := Cursor
else
L := Min(L, Cursor);
if Cursor = CACHE_SIZE then F := I;
end;
end;
for I := 0 to High(FStreams) do
begin
with FStreams[I] do
begin
MixLen := L;
end;
end;
// *one* stream's cache is full, send it directly
if L = 0 then
begin
if F < 0 then raise Exception.Create('TRadioAudioMixer.Mixing: impossible');
FStreams[F].MixLen := CACHE_SIZE;
L := CACHE_SIZE;
end;
SetLength(T, L);
for I := 0 to High(FStreams) do
begin
with FStreams[I] do
begin
if MixMethod = AUDIOMIXER_STREAM_OUTPUT_OFF then Continue;
if MixLen < 1 then Continue;
case MixMethod of
AUDIOMIXER_STREAM_OUTPUT_IQ_IQ:
for F := 0 to L - 1 do
begin
T[F] := T[F] + Cache[F];
end;
AUDIOMIXER_STREAM_OUTPUT_QI_QI:
for F := 0 to L - 1 do
begin
T[F].re := T[F].re + Cache[F].im;
T[F].im := T[F].im + Cache[F].re;
end;
AUDIOMIXER_STREAM_OUTPUT_IQ_I :
for F := 0 to L - 1 do
begin
T[F].re := T[F].re + Cache[F].re + Cache[F].im;
end;
AUDIOMIXER_STREAM_OUTPUT_IQ_Q :
for F := 0 to L - 1 do
begin
T[F].im := T[F].im + Cache[F].re + Cache[F].im;
end;
AUDIOMIXER_STREAM_OUTPUT_I_I :
for F := 0 to L - 1 do
begin
T[F].re := T[F].re + Cache[F].re;
end;
AUDIOMIXER_STREAM_OUTPUT_I_Q :
for F := 0 to L - 1 do
begin
T[F].im := T[F].im + Cache[F].re;
end;
AUDIOMIXER_STREAM_OUTPUT_Q_I :
for F := 0 to L - 1 do
begin
T[F].re := T[F].re + Cache[F].im;
end;
AUDIOMIXER_STREAM_OUTPUT_Q_Q :
for F := 0 to L - 1 do
begin
T[F].im := T[F].im + Cache[F].im;
end;
end;
Move(Cache[MixLen], Cache[0], (Cursor - MixLen) * SizeOf(Cache[0]));
Dec(Cursor, MixLen);
end;
end;
FRegulator.ReceiveData(@T[0], L);
end;
procedure TRadioAudioMixer.SetupBassFilter(const Index: Integer);
begin
if FRate < 2 then Exit;
if Abs(FStreams[Index].BassGain) < 1 then
begin
FStreams[Index].BassGain := 0;
Exit;
end;
SetIIROrders(FStreams[Index].Bass, 2, 2);
ShelvingFilterDesign(FStreams[Index].BassGain, 250, FRate, Sqrt(2), sfBassShelf,
FStreams[Index].Bass.A, FStreams[Index].Bass.B);
end;
procedure TRadioAudioMixer.SetupTrebleFilter(const Index: Integer);
begin
if FRate < 2 then Exit;
if Abs(FStreams[Index].TrebleGain) < 1 then
begin
FStreams[Index].TrebleGain := 0;
Exit;
end;
SetIIROrders(FStreams[Index].Treble, 2, 2);
ShelvingFilterDesign(FStreams[Index].TrebleGain, 2000, FRate, Sqrt(2), sfTrebleShelf,
FStreams[Index].Treble.A, FStreams[Index].Treble.B);
end;
function TRadioAudioMixer.RMSetSampleRate(const Msg: TRadioMessage;
const Rate: Cardinal): Integer;
var
I: Integer;
begin
FRate := Rate;
for I := 0 to High(FStreams) do
begin
SetupBassFilter(I);
SetupTrebleFilter(I);
end;
Result := inherited;
end;
procedure TRadioAudioMixer.ProccessCustomMessage(const Msg: TRadioMessage;
var Ret: Integer);
var
I: Integer;
begin
case Msg.Id of
RM_AUDIOMIXER_CFG:
begin
case Msg.ParamH of
AUDIOMIXER_STREAM_NUM:
begin
I := Length(FStreams);
SetLength(FStreams, Msg.ParamL);
while I <= High(FStreams) do
begin
with FStreams[I] do
begin
TotalGain := 1.0;
end;
SetupBassFilter(I);
Inc(I);
end;
end;
end;
GraphInvalidate;
end;
RM_AUDIOMIXER_SET_STREAM_OUPUT:
begin
if Integer(Msg.ParamH) > High(FStreams) then Exit;
FStreams[Msg.ParamH].MixMethod := Msg.ParamL;
GraphInvalidate;
end;
RM_AUDIOMIXER_SET_STREAM_TOTAL_GAIN:
begin
if Integer(Msg.ParamH) > High(FStreams) then Exit;
FStreams[Msg.ParamH].TotalGain := Power(10, Integer(Msg.ParamL) / 10 / 20);
end;
RM_AUDIOMIXER_SET_STREAM_BASS_GAIN:
begin
if Integer(Msg.ParamH) > High(FStreams) then Exit;
FStreams[Msg.ParamH].BassGain := Integer(Msg.ParamL) / 10;
SetupBassFilter(Msg.ParamH);
end;
RM_AUDIOMIXER_SET_STREAM_TREBLE_GAIN:
begin
if Integer(Msg.ParamH) > High(FStreams) then Exit;
FStreams[Msg.ParamH].TrebleGain := Integer(Msg.ParamL) / 10;
SetupTrebleFilter(Msg.ParamH);
end;
else
inherited;
end;
end;
procedure TRadioAudioMixer.DoConfigure;
var
I: Integer;
begin
FConfig.ShowUI(Length(FStreams));
for I := 0 to High(FStreams) do
with FStreams[I] do
FConfig.ConfigChannel(I, MixMethod, Round(TotalGain), Round(BassGain), Round(TrebleGain));
end;
procedure TRadioAudioMixer.Describe(Strs: TStrings);
begin
Strs.Add(Format('^bChannel Number: ^n %d', [Length(FStreams)]));
Strs.Add(Format('^bSample Rate: ^n %d', [FRate]));
end;
procedure TRadioAudioMixer.DoSyncDestroy;
begin
FConfig.Free;
inherited DoSyncDestroy;
end;
constructor TRadioAudioMixer.Create(RunQueue: TRadioRunQueue);
begin
inherited Create(RunQueue);
FConfig := TAudioMixerForm.Create(nil);
FConfig.Module := Self;
FRegulator := TStreamRegulator.Create;
FRegulator.Size := DefOutput.BufferSize;
FRegulator.OnRegulatedData := @ReceiveMixedData;
end;
destructor TRadioAudioMixer.Destroy;
begin
inherited Destroy;
end;
procedure TRadioAudioMixer.ReceiveData(const P: PComplex; const Len: Integer);
begin
ReceiveData(0, P, Len);
end;
procedure TRadioAudioMixer.ReceiveData(const Port: Integer; const P: PComplex;
const Len: Integer);
var
N, M: Integer;
I: Integer = 0;
J: Integer;
begin
if (Port < 0) or (Port > High(FStreams)) then Exit;
if FStreams[Port].MixMethod = AUDIOMIXER_STREAM_OUTPUT_OFF then Exit;
N := Len;
while N >= 1 do
begin
M := Min(N, CACHE_SIZE - FStreams[Port].Cursor);
if M < 1 then
begin
Mixing;
Continue;
end;
with FStreams[Port] do
begin
for J := 0 to M - 1 do
Cache[Cursor + J] := P[I + J] * TotalGain;
if not IsZero(BassGain) then
IIRFilter(Bass, PComplex(@Cache[Cursor]), M);
if not IsZero(TrebleGain) then
IIRFilter(Treble, PComplex(@Cache[Cursor]), M);
end;
Inc(FStreams[Port].Cursor, M);
Inc(I, M);
Dec(N, M);
end;
end;
initialization
RegisterModule(TRadioModuleClass(TRadioAudioMixer.ClassType));
end.