This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReaEQ.cs
132 lines (118 loc) · 3.81 KB
/
ReaEQ.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
using System;
using System.Collections.Generic;
using System.IO;
using CommonUtils;
namespace REWEQ2EQPreset
{
/// <summary>
/// ReaEQ Preset Class for saving a ReaEq Preset file (fxp)
/// </summary>
public static class ReaEQ
{
public static bool Convert2ReaEQ(REWEQFilters filters, string filePath) {
List<ReaEQBand> ReaEqBands = new List<ReaEQBand>();
foreach (REWEQBand filter in filters) {
ReaEQBand band = new ReaEQBand();
band.LogScaleAutoFreq = true;
band.FilterFreq = filter.FilterFreq;
band.FilterGain = filter.FilterGain;
band.FilterBWOct = filter.FilterBWOct;
band.Enabled = filter.Enabled;
switch (filter.FilterType) {
case REWEQFilterType.PK:
band.FilterType = ReaEQFilterType.Band;
break;
case REWEQFilterType.LP:
band.FilterType = ReaEQFilterType.LowPass;
break;
case REWEQFilterType.HP:
band.FilterType = ReaEQFilterType.HighPass;
break;
case REWEQFilterType.LS:
band.FilterType = ReaEQFilterType.LowShelf;
break;
case REWEQFilterType.HS:
band.FilterType = ReaEQFilterType.HighShelf;
break;
default:
band.FilterType = ReaEQFilterType.Band;
break;
}
ReaEqBands.Add(band);
}
// store to file
FXP fxp = new FXP();
fxp.ChunkMagic = "CcnK";
fxp.ByteSize = 0; // will be set correctly by FXP class
fxp.FxMagic = "FPCh"; // FPCh = FXP (preset), FBCh = FXB (bank)
fxp.Version = 1; // Format Version (should be 1)
fxp.FxID = "reeq";
fxp.FxVersion = 1100;
fxp.ProgramCount = 1;
fxp.Name = "";
using(MemoryStream memStream = new MemoryStream(10))
{
BinaryFile binFile = new BinaryFile(memStream, BinaryFile.ByteOrder.LittleEndian);
binFile.Write((int)33);
binFile.Write((int)ReaEqBands.Count);
foreach (ReaEQBand band in ReaEqBands) {
binFile.Write((int) band.FilterType);
binFile.Write((int) (band.Enabled ? 1 : 0) );
binFile.Write((double) band.FilterFreq);
binFile.Write((double) Decibel2AmplitudeRatio(band.FilterGain));
binFile.Write((double) band.FilterBWOct);
binFile.Write((byte) 1);
}
binFile.Write((int)1);
binFile.Write((int)1);
binFile.Write((double) Decibel2AmplitudeRatio(0.00));
binFile.Write((int)0);
memStream.Flush();
byte[] chunkData = memStream.GetBuffer();
fxp.ChunkSize = chunkData.Length;
fxp.ChunkDataByteArray = chunkData;
}
fxp.WriteFile(filePath);
return true;
}
// Amplitude ratio to dB conversion
// For amplitude of waves like voltage, current and sound pressure level:
// GdB = 20 * log10(A2 / A1)
// A2 is the amplitude level.
// A1 is the referenced amplitude level.
// GdB is the amplitude ratio or gain in dB.
public static double AmplitudeRatio2Decibel(double value) {
return 20 * Math.Log10(value);
}
// dB to amplitude ratio conversion
// A2 = A1 * 10^(GdB / 20)
// A2 is the amplitude level.
// A1 is the referenced amplitude level.
public static double Decibel2AmplitudeRatio(double value) {
return Math.Pow(10, value / 20);
}
}
public enum ReaEQFilterType {
LowShelf = 0,
HighShelf = 1,
Band = 8,
LowPass = 3,
HighPass = 4,
AllPass = 5,
Notch = 6,
BandPass = 7,
Band_alt = 9,
Band_alt2 = 2,
}
public class ReaEQBand {
public ReaEQFilterType FilterType { get; set; }
public bool Enabled { get; set; }
public double FilterFreq { get; set; } // value range 20.0 -> 24000.0 Hz
public double FilterGain { get; set; } // value range -90.0 (inf) -> 24.0) dB. Store the inverse=10^(dBVal/20)
public double FilterBWOct { get; set; } // value range 0.01 -> 4.00
public bool LogScaleAutoFreq { get; set; }
public override string ToString() {
return String.Format("{0}: {1} Hz {2} dB BWOct: {3}", FilterType, FilterFreq, FilterGain, FilterBWOct);
}
}
}