-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioEffect.cs
618 lines (503 loc) · 18.4 KB
/
AudioEffect.cs
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using CSCore;
using CSCore.Codecs;
using CSCore.DSP;
using CSCore.MediaFoundation;
using CSCore.SoundOut;
using CSCore.Streams;
using KiriMusicHelper.Annotations;
using Xabe.FFmpeg;
using Xabe.FFmpeg.Events;
namespace KiriMusicHelper
{
public interface IAudioEffectSource
{
Task<ISampleSource> GetSource();
Task<int> GetSampleRate();
}
class AudioEffectSourceConversion : IAudioEffectSource
{
string inputPath;
string tempFilePath;
bool ready = false;
public AudioEffectSourceConversion(string path)
{
inputPath = path;
tempFilePath = TempFileManager.Instance.GetNewTempFileExt(".flac");
}
public async Task<int> GetSampleRate()
{
var info = await AudioEncoder.GetInfo(inputPath);
return info.AudioStreams.FirstOrDefault()?.SampleRate ?? 0; //#TODO cache samplerate
}
public async Task<ISampleSource> GetSource()
{
if (!ready) {
var info = await AudioEncoder.GetInfo(inputPath);
IStream audioStream = info.AudioStreams.FirstOrDefault()
?.SetCodec(AudioCodec.flac);
var conversion = FFmpeg.Conversions.New()
.AddStream(audioStream)
.SetOutput(tempFilePath);
var result = await conversion.Start();
}
var source = CodecFactory.Instance.GetCodec(tempFilePath)
.ToSampleSource();
return source;
}
}
class AudioEffectSourceDirect : IAudioEffectSource
{
string inputPath;
public AudioEffectSourceDirect(string path)
{
inputPath = path;
}
public async Task<int> GetSampleRate()
{
var info = await AudioEncoder.GetInfo(inputPath);
return info.AudioStreams.FirstOrDefault()?.SampleRate ?? 0;
}
public async Task<ISampleSource> GetSource()
{
var source = CodecFactory.Instance.GetCodec(inputPath)
.ToSampleSource();
return source;
}
}
// https://github.com/filoe/cscore/blob/master/Samples/FfmpegSample/Program.cs
//class AudioEffectSourceFFMPEG : IAudioEffectSource
//{
// string inputPath;
// public AudioEffectSourceDirect(string path)
// {
// inputPath = path;
// }
//
// public async Task<int> GetSampleRate()
// {
// var info = await AudioEncoder.GetInfo(inputPath);
// return info.AudioStreams.FirstOrDefault()?.SampleRate ?? 0;
// }
//
// public async Task<ISampleSource> GetSource()
// {
// IWaveSource ffmpegDecoder =
// new CSCore.ffFfmpeg.FfmpegDecoder(inputPath)
//
// return source;
// }
//}
public class AudioEffect//: AudioEffectLivePlay
{
public static AudioEffect Instance = new AudioEffect();
public static IAudioEffectSource CreateSource(string path)
{
if (path == null) return null;
var ext = Path.GetExtension(path);//.TrimStart('.');
var supportedExtensions = CodecFactory.Instance.GetSupportedFileExtensions();
if (supportedExtensions.Contains(ext))
return new AudioEffectSourceDirect(path);
else
return new AudioEffectSourceConversion(path);
}
public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
public static float TFARSignalLossToErrorLevel(float signalLoss)
{
List<float> levels = new List<float>{ 0.0f, 0.150000006f, 0.300000012f, 0.600000024f, 0.899999976f, 0.950000048f, 0.960000038f, 0.970000029f, 0.980000019f, 0.995000005f, 0.997799993f, 0.998799993f, 0.99999f };
var part = Clamp((int)(signalLoss * 10), 0, levels.Count - 2);
var from = levels[part];
var to = levels[part + 1];
var result = from + (to - from) * (signalLoss - part / 10);
return result;
}
public static async Task DoStuff()
{
var test = CodecFactory.SupportedFilesFilterEn;
OpenFileDialog openFileDialog = new OpenFileDialog { Filter = CodecFactory.SupportedFilesFilterEn };
if (openFileDialog.ShowDialog() != DialogResult.OK)
return;
var effect = Instance;
await effect.SetSource(AudioEffect.CreateSource(openFileDialog.FileName));
effect.Filters.Add(new HighpassFilter(effect.SampleRate, 520)); // Console.WriteLine("HighpassFilter @4kHz");
effect.Filters.Add(new LowpassFilter(effect.SampleRate, 1300)); // Console.WriteLine("LowpassFilter @1kHz");
//effect.Filters.Add(new PeakFilter(effect.SampleRate, 2000, 15, 10)); // Console.WriteLine("PeakFilter @2kHz; bandWidth = 15; gain = 10dB");
effect.Filters.Add(new FoldbackFilter()); // Console.WriteLine("PeakFilter @2kHz; bandWidth = 15; gain = 10dB");
effect.Filters.Add(new RingmodulationFilter(effect.SampleRate)); // Console.WriteLine("PeakFilter @2kHz; bandWidth = 15; gain = 10dB");
//effect.StartLivePlay();
//var outPath = TempFileManager.Instance.GetNewTempFileExt(".wav");
//effect.source.ToWaveSource().WriteToFile(outPath);
//
//AudioEncoder encoder = new AudioEncoder();
//
//var queue = new ConcurrentQueue<AudioConversionItem>();
//queue.Enqueue(new AudioConversionItem
//{
// Bitrate = 192000,
// InputFile = new FileInfo(outPath),
// OutputPath = TempFileManager.Instance.GetNewTempFileExt(".ogg"),
// SampleRate = effect.SampleRate
//});
//
//await encoder.AudioConvert(queue, CancellationToken.None);
}
public BiQuadFilterSource source;
public ObservableCollection<IFilter> Filters { get; set; } = new ObservableCollection<IFilter>();
public int SampleRate => source.WaveFormat.SampleRate;
public void AddFilter(IFilter newFilter)
{
lock (Filters)
{
Filters.Add(newFilter);
}
if (Filters.Any(x => x.NeedFullPass)) DoFilterFullPass();
}
public void DoFilterFullPass()
{
int index = 0;
foreach (var filter in Filters)
{
if (filter.NeedFullPass)
{
source.DoFullPass(index);
filter.FullPassFinished();
}
index++;
}
}
public async Task SetSource(IAudioEffectSource src)
{
if (src == null) return;
var newSource = (await src.GetSource()).AppendSource(x => new BiQuadFilterSource(x, this));
lock (Filters)
{
source = newSource;
//base.SetLiveSource(source);
}
}
public async Task<AudioEffect> CopyFor(FileInfo audioFileInputFile)
{
var result = new AudioEffect();
await result.SetSource(AudioEffect.CreateSource(audioFileInputFile.FullName));
foreach (var filter in Filters)
{
result.Filters.Add(filter.Clone());
}
result.DoFilterFullPass();
return result;
}
public string ProcessIntoTempFile()
{
var outPath = TempFileManager.Instance.GetNewTempFileExt(".wav");
source.ToWaveSource().WriteToFile(outPath);
return outPath;
}
}
public class BiQuadFilterSource : SampleAggregatorBase
{
private AudioEffect _effect;
public BiQuadFilterSource(ISampleSource source, AudioEffect effect) : base(source)
{
_effect = effect;
}
public override int Read(float[] buffer, int offset, int count)
{
if (BaseSource == null)
return 0;
lock (_effect.Filters)
{
int read = base.Read(buffer, offset, count);
foreach (var filter in _effect.Filters)
{
if (filter != null)
{
filter.PreProcess(buffer, offset, count);
for (int i = 0; i < read; i++)
{
buffer[i + offset] = filter.Process(buffer[i + offset]);
}
}
};
return read;
}
}
public void DoFullPass(int numberOfFilters)
{
if (BaseSource == null) return;
int filterIndex = 0;
var buffer = new float[base.Length];
int read = base.Read(buffer, 0, buffer.Length);
Position = 0;
foreach (var filter in _effect.Filters)
{
filter.PreProcess(buffer, 0, read);
if (filterIndex == numberOfFilters) filter.FullPassProgress(buffer, read);
else
{
for (int i = 0; i < read; i++)
{
buffer[i] = filter.Process(buffer[i]);
}
}
filterIndex++;
if (filterIndex > numberOfFilters) break;
};
}
}
public class FoldbackFilter: IFilter
{
public float SignalLoss { get; set; } = 0.5f;
public float errorLevel => AudioEffect.TFARSignalLossToErrorLevel(SignalLoss); //errorLevel
public float threshold;
public float Process(float input)
{
if (threshold < 0.00001) return 0.0f;
if (input > threshold || input < -threshold) {
input = Math.Abs(Math.Abs((float)Math.IEEERemainder(input - threshold, threshold * 4)) - threshold * 2) - threshold;
}
return input;
}
public void PreProcess(float[] input, int offset, int sampleCount)
{
float acc = 0.0f;
for (int i = 0; i < sampleCount; i++) acc += Math.Abs(input[i + offset]);
var avg = acc / sampleCount;
var _base = 0.005;
var x = avg / _base;
threshold = (float)(0.3f * (1.0f - errorLevel) * x);
}
public void FullPassProgress(float[] input, int sampleCount) { }
public void FullPassFinished() { }
public bool NeedFullPass => false;
public IFilter Clone()
{
return new FoldbackFilter { SignalLoss = SignalLoss };
}
}
public class RingmodulationFilter : IFilter
{
public float SignalLoss { get; set; } = 0.5f;
public float mixRate => AudioEffect.TFARSignalLossToErrorLevel(SignalLoss); //errorLevel
private float phase = 0;
private float sampleRate;
public RingmodulationFilter(int sampleRate)
{
this.sampleRate = sampleRate;
}
public float Process(float input)
{
float multiple = (float)(input * Math.Sin(phase * 1.57079632679489661923f)); //#define PI_2 1.57079632679489661923f
phase += (90.0f * 1.0f / sampleRate);
if (phase > 1.0f) phase = 0.0f;
return input * (1.0f - mixRate) + multiple * mixRate;
}
public void PreProcess(float[] input, int offset, int sampleCount) { }
public void FullPassProgress(float[] input, int sampleCount) { }
public void FullPassFinished() { }
public bool NeedFullPass => false;
public IFilter Clone()
{
return new RingmodulationFilter((int)sampleRate) { SignalLoss = SignalLoss };
}
}
public class RandomLossFilter : IFilter
{
private Random randGen = new Random();
public float SignalLoss { get; set; } = 0.5f;
public float errorLevel => AudioEffect.TFARSignalLossToErrorLevel(SignalLoss);
public RandomLossFilter()
{
}
public float Process(float input)
{
if (randGen.NextDouble() < errorLevel) return 0;
return input;
}
public void PreProcess(float[] input, int offset, int sampleCount) { }
public void FullPassProgress(float[] input, int sampleCount) { }
public void FullPassFinished() { }
public bool NeedFullPass => false;
public IFilter Clone()
{
return new RandomLossFilter { SignalLoss = SignalLoss };
}
}
public class VolumeFilter : IFilter
{
public float Volume { get; set; } = 1;
public VolumeFilter()
{
}
public float Process(float input)
{
return input * Volume;
}
public void PreProcess(float[] input, int offset, int sampleCount) { }
public void FullPassProgress(float[] input, int sampleCount) { }
public void FullPassFinished() { }
public bool NeedFullPass => false;
public IFilter Clone()
{
return this;
}
}
public class VolumeMonitor : IFilter, INotifyPropertyChanged
{
public float Volume { get; private set; } = 0;
private float tempVolume = 0;
public VolumeMonitor()
{
}
public float Process(float input)
{
return input;
}
public void PreProcess(float[] input, int offset, int sampleCount) { }
public void FullPassProgress(float[] input, int sampleCount)
{
for (var i = 0; i < sampleCount; i++)
{
var abs = Math.Abs(input[i]);
if (abs > tempVolume) tempVolume = abs;
}
}
public void FullPassFinished()
{
Volume = tempVolume;
tempVolume = 0;
}
public bool NeedFullPass => true;
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public IFilter Clone()
{
return this;
}
}
public class VolumeSetFilter : IFilter, INotifyPropertyChanged
{
public float Volume { get; set; } = 1;
private float actualVolume = 0;
private float factor = 1;
public VolumeSetFilter()
{
}
public float Process(float input)
{
return input * factor;
}
public void PreProcess(float[] input, int offset, int sampleCount) { }
public void FullPassProgress(float[] input, int sampleCount)
{
for (var i = 0; i < sampleCount; i++)
{
var abs = Math.Abs(input[i]);
if (abs > actualVolume) actualVolume = abs;
}
}
public void FullPassFinished()
{
factor = Volume / actualVolume;
}
public bool NeedFullPass => true;
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (propertyName == nameof(Volume))
factor = Volume / actualVolume;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public IFilter Clone()
{
return this;
}
}
public class HighpassFilter : CSCore.DSP.HighpassFilter, IFilter
{
public int MaxFrequency => base.SampleRate / 2;
public HighpassFilter(int sampleRate, double frequency)
: base(sampleRate, frequency)
{
}
public void PreProcess(float[] input, int offset, int sampleCount) { }
public void FullPassProgress(float[] input, int sampleCount) { }
public void FullPassFinished() { }
public bool NeedFullPass => false;
public IFilter Clone()
{
return new HighpassFilter(SampleRate, Frequency);
}
}
public class LowpassFilter : CSCore.DSP.LowpassFilter, IFilter
{
public int MaxFrequency => base.SampleRate / 2;
public LowpassFilter(int sampleRate, double frequency)
: base(sampleRate, frequency)
{
}
public void PreProcess(float[] input, int offset, int sampleCount) {}
public void FullPassProgress(float[] input, int sampleCount) {}
public void FullPassFinished() {}
public bool NeedFullPass => false;
public IFilter Clone()
{
return new LowpassFilter(SampleRate, Frequency);
}
}
public interface IFilter
{
void PreProcess(float[] input, int offset, int sampleCount);
float Process(float input);
void FullPassProgress(float[] input, int sampleCount);
void FullPassFinished();
bool NeedFullPass { get; }
IFilter Clone();
}
public class AudioEffectLivePlay: IDisposable
{
private ISoundOut soundOutput = new WasapiOut();
ISampleSource source;
//https://github.com/filoe/cscore/tree/master/Samples/CSCoreWaveform
protected void SetLiveSource(ISampleSource src)
{
source = src;
}
public void StartLivePlay()
{
soundOutput.Stop();
soundOutput.Initialize(source.ToWaveSource());
soundOutput.Play();
}
public void StopLivePlay()
{
soundOutput.Stop();
}
public void Dispose()
{
soundOutput.Dispose();
}
}
}