BGC Tools
Data Structures | Public Types | Static Public Member Functions | Static Private Member Functions | Static Private Attributes
BGC.Audio.Calibration Class Reference

Manages calibration values and processes More...

Collaboration diagram for BGC.Audio.Calibration:
Collaboration graph
[legend]

Data Structures

struct  CalibrationPoint
 
class  Keys
 

Public Types

enum  Tone {
  Tone._250Hz = 0, Tone._500Hz, Tone._1000Hz, Tone._2000Hz,
  Tone._4000Hz, Tone._8000Hz, Tone.Noise, Tone.MAX
}
 
enum  Source {
  Source.Default = 0, Source.Custom, Source.Results, Source.InProgress,
  Source.MAX
}
 

Static Public Member Functions

static void Initialize ()
 
static void Serialize ()
 
static void FinishCalibration ()
 
static void InitiateCalibration (int[] testLevels)
 
static void SubmitCalibrationValue (int step, AudioChannel channel, double offset)
 
static void PushCalibrationResults ()
 
static void DropCalibrationResults (Source source)
 
static Source GetSourceForVerificationPanel ()
 
static void GetLevelOffset (double level, out double levelOffsetL, out double levelOffsetR, Source source=Source.Custom)
 
static string GetToneName (this Tone tone)
 
static double GetFrequency (this Tone tone)
 

Static Private Member Functions

static void DeserializeCalibration (JsonObject parsedValue)
 
static JsonObject SerializeCalibration ()
 

Static Private Attributes

static List< CalibrationPointcustomCalibration = null
 
static List< CalibrationPointresultsCalibration = null
 
static List< CalibrationPointinProgressCalibration = null
 
static readonly string dataDir = "System"
 
static readonly string configFileName = "Calibration.json"
 
static bool initialized = false
 

Detailed Description

Manages calibration values and processes

Definition at line 13 of file Calibration.cs.

Member Enumeration Documentation

◆ Source

Enumerator
Default 
Custom 
Results 
InProgress 
MAX 

Definition at line 36 of file Calibration.cs.

37  {
38  Default = 0,
39  Custom,
40  Results,
41  InProgress,
42  MAX
43  }

◆ Tone

Enumerator
_250Hz 
_500Hz 
_1000Hz 
_2000Hz 
_4000Hz 
_8000Hz 
Noise 
MAX 

Definition at line 24 of file Calibration.cs.

25  {
26  _250Hz = 0,
27  _500Hz,
28  _1000Hz,
29  _2000Hz,
30  _4000Hz,
31  _8000Hz,
32  Noise,
33  MAX
34  }

Member Function Documentation

◆ DeserializeCalibration()

static void BGC.Audio.Calibration.DeserializeCalibration ( JsonObject  parsedValue)
inlinestaticprivate

Definition at line 69 of file Calibration.cs.

References LightJson.JsonObject.Add(), LightJson.JsonArray.Count, BGC.Audio.Calibration.Keys.CustomCalibration, BGC.Audio.Calibration.Keys.LAdjustment, BGC.Audio.Calibration.Keys.Level, and BGC.Audio.Calibration.Keys.RAdjustment.

70  {
71  JsonArray calibrationValues = parsedValue[Keys.CustomCalibration].AsJsonArray;
72 
73  if (calibrationValues.Count == 0)
74  {
75  return;
76  }
77 
78  customCalibration = new List<CalibrationPoint>(calibrationValues.Count);
79  foreach (JsonObject calibrationPoint in calibrationValues)
80  {
81  customCalibration.Add(new CalibrationPoint(
82  levelIn: calibrationPoint[Keys.Level],
83  levelOffsetL: calibrationPoint[Keys.LAdjustment].AsNumber,
84  levelOffsetR: calibrationPoint[Keys.RAdjustment].AsNumber));
85  }
86  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
static List< CalibrationPoint > customCalibration
Definition: Calibration.cs:45
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
int Count
The number of values in this collection.
Definition: JsonArray.cs:16
Here is the call graph for this function:

◆ DropCalibrationResults()

static void BGC.Audio.Calibration.DropCalibrationResults ( Source  source)
inlinestatic

Definition at line 200 of file Calibration.cs.

201  {
202  switch (source)
203  {
204  case Source.Custom:
205  customCalibration = null;
206  Serialize();
207  break;
208 
209  case Source.Results:
210  resultsCalibration = null;
211  break;
212 
213  case Source.InProgress:
214  inProgressCalibration = null;
215  break;
216 
217  case Source.Default:
218  default:
219  Debug.LogError($"Unexpected Source: {source}");
220  break;
221  }
222  }
static List< CalibrationPoint > inProgressCalibration
Definition: Calibration.cs:47
static void Serialize()
Definition: Calibration.cs:88
static List< CalibrationPoint > customCalibration
Definition: Calibration.cs:45
static List< CalibrationPoint > resultsCalibration
Definition: Calibration.cs:46

◆ FinishCalibration()

static void BGC.Audio.Calibration.FinishCalibration ( )
inlinestatic

Definition at line 118 of file Calibration.cs.

119  {
121  inProgressCalibration = null;
122  }
static List< CalibrationPoint > inProgressCalibration
Definition: Calibration.cs:47
static List< CalibrationPoint > resultsCalibration
Definition: Calibration.cs:46

◆ GetFrequency()

static double BGC.Audio.Calibration.GetFrequency ( this Tone  tone)
inlinestatic

Definition at line 339 of file Calibration.cs.

340  {
341  switch (tone)
342  {
343  case Tone._250Hz: return 250;
344  case Tone._500Hz: return 500;
345  case Tone._1000Hz: return 1000;
346  case Tone._2000Hz: return 2000;
347  case Tone._4000Hz: return 4000;
348  case Tone._8000Hz: return 8000;
349 
350  default:
351  Debug.LogError($"Unexpected Tone: {tone}");
352  return 100;
353  }
354  }

◆ GetLevelOffset()

static void BGC.Audio.Calibration.GetLevelOffset ( double  level,
out double  levelOffsetL,
out double  levelOffsetR,
Source  source = Source.Custom 
)
inlinestatic

Definition at line 238 of file Calibration.cs.

References BGC.Mathematics.GeneralMath.Lerp().

Referenced by BGC.Audio.Normalization.CalculateRMSLevel(), and BGC.Audio.Normalization.SPLToAdjustmentDB().

243  {
244  List<CalibrationPoint> points;
245 
246  switch (source)
247  {
248  case Source.InProgress:
249  if (inProgressCalibration == null || inProgressCalibration.Count == 0)
250  {
251  goto case Source.Results;
252  }
253  points = inProgressCalibration;
254  break;
255 
256  case Source.Results:
257  if (resultsCalibration == null || resultsCalibration.Count == 0)
258  {
259  goto case Source.Custom;
260  }
261  points = resultsCalibration;
262  break;
263 
264  case Source.Custom:
265  if (customCalibration == null || customCalibration.Count == 0)
266  {
267  goto case Source.Default;
268  }
269  points = customCalibration;
270  break;
271 
272  case Source.Default:
273  levelOffsetL = 0.0;
274  levelOffsetR = 0.0;
275  return;
276 
277  default:
278  Debug.LogError($"Unexpected Source: {source}");
279  goto case Source.Default;
280  }
281 
282  if (points.Count == 1)
283  {
284  levelOffsetL = points[0].levelOffsetL;
285  levelOffsetR = points[0].levelOffsetR;
286  return;
287  }
288 
289  //If the requested level is before the first or after the last, use its assocaited offset
290  if (level <= points[0].levelIn)
291  {
292  levelOffsetL = points[0].levelOffsetL;
293  levelOffsetR = points[0].levelOffsetR;
294  return;
295  }
296 
297  //Else, we will LERP between the surrounding offsets
298  for (int index = 0; index < points.Count - 1; index++)
299  {
300  if (level >= points[index].levelIn && level < points[index + 1].levelIn)
301  {
302 
303  levelOffsetL = GeneralMath.Lerp(points[index].levelOffsetL,
304  points[index + 1].levelOffsetL,
305  (level - points[index].levelIn) / (points[index + 1].levelIn - points[index].levelIn));
306 
307  levelOffsetR = GeneralMath.Lerp(points[index].levelOffsetR,
308  points[index + 1].levelOffsetR,
309  (level - points[index].levelIn) / (points[index + 1].levelIn - points[index].levelIn));
310 
311  return;
312  }
313  }
314 
315  //Else, we return the last value
316  levelOffsetL = points[points.Count - 1].levelOffsetL;
317  levelOffsetR = points[points.Count - 1].levelOffsetR;
318  }
static List< CalibrationPoint > inProgressCalibration
Definition: Calibration.cs:47
static List< CalibrationPoint > customCalibration
Definition: Calibration.cs:45
static decimal Lerp(decimal initial, decimal final, decimal t)
Linearly interpolates between initial and final by t , clamps t to range [0,1] ...
Definition: GeneralMath.cs:232
static List< CalibrationPoint > resultsCalibration
Definition: Calibration.cs:46
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSourceForVerificationPanel()

static Source BGC.Audio.Calibration.GetSourceForVerificationPanel ( )
inlinestatic

Definition at line 224 of file Calibration.cs.

225  {
226  if (resultsCalibration != null)
227  {
228  return Source.Results;
229  }
230  else if (customCalibration != null)
231  {
232  return Source.Custom;
233  }
234 
235  return Source.Default;
236  }
static List< CalibrationPoint > customCalibration
Definition: Calibration.cs:45
static List< CalibrationPoint > resultsCalibration
Definition: Calibration.cs:46

◆ GetToneName()

static string BGC.Audio.Calibration.GetToneName ( this Tone  tone)
inlinestatic

Definition at line 320 of file Calibration.cs.

321  {
322  switch (tone)
323  {
324  case Tone._250Hz: return "250 Hz";
325  case Tone._500Hz: return "500 Hz";
326  case Tone._1000Hz: return "1000 Hz";
327  case Tone._2000Hz: return "2000 Hz";
328  case Tone._4000Hz: return "4000 Hz";
329  case Tone._8000Hz: return "8000 Hz";
330  case Tone.Noise: return "Noise";
331  case Tone.MAX: return "";
332 
333  default:
334  Debug.LogError($"Unexpected Tone: {tone}");
335  return "";
336  }
337  }

◆ Initialize()

static void BGC.Audio.Calibration.Initialize ( )
inlinestatic

Definition at line 54 of file Calibration.cs.

References BGC.IO.DataManagement.PathForDataFile(), and BGC.IO.FileReader.ReadJsonFile().

Referenced by BGC.Tests.SynthesisTests.FMTestBell(), BGC.Tests.SynthesisTests.FMTestBrass(), BGC.Tests.SynthesisTests.FMTestDrum(), BGC.Tests.SynthesisTests.FMTestOvertones(), BGC.Tests.SynthesisTests.FMTestPiano(), BGC.Tests.SynthesisTests.FMTestTrianglePiano(), BGC.Tests.SynthesisTests.SmallAnalyticFMTest(), BGC.Tests.SynthesisTests.SmallSineFMTest(), BGC.Tests.SynthesisTests.TestAllPassFilterSpeech(), BGC.Tests.SynthesisTests.TestAllPassFilterSynth(), BGC.Tests.SynthesisTests.TestBiQuadFilters(), BGC.Tests.SynthesisTests.TestCarrierModifiedFakeVoices(), BGC.Tests.MidiEncodingTests.TestContinuousFilter(), BGC.Tests.SynthesisTests.TestFakeVoice(), BGC.Tests.SynthesisTests.TestFDComposer(), BGC.Tests.SynthesisTests.TestFDComposerPure(), BGC.Tests.MidiEncodingTests.TestFlute(), BGC.Tests.SynthesisTests.TestFMFilterSynth(), BGC.Tests.SynthesisTests.TestFMFilterVoice(), BGC.Tests.SynthesisTests.TestFrequencyShifter(), BGC.Tests.SynthesisTests.TestFunFakeVoice(), BGC.Tests.MidiEncodingTests.TestGuitar(), BGC.Tests.MidiEncodingTests.TestOrgan(), BGC.Tests.SynthesisTests.TestPhaseReEncoder(), BGC.Tests.MidiEncodingTests.TestPulses(), BGC.Tests.MidiEncodingTests.TestRenderMidi(), BGC.Tests.MidiEncodingTests.TestRenderToccataMidi(), BGC.Tests.SynthesisTests.TestSawtoothWave(), BGC.Tests.SynthesisTests.TestSineWave(), BGC.Tests.SynthesisTests.TestSquareWave(), BGC.Tests.SynthesisTests.TestSTM(), and BGC.Tests.SynthesisTests.TestTriangleWave().

55  {
56  if (!initialized)
57  {
58  initialized = true;
59 
62  successCallback: DeserializeCalibration,
63  failCallback: Serialize,
64  fileNotFoundCallback: Serialize);
65  }
66  }
static bool ReadJsonFile(string path, Action< JsonObject > successCallback, Action failCallback=null, Action fileNotFoundCallback=null, Action< string, Exception > overrideExceptionHandling=null)
Offers two error callbacks for when a file is not found and for when a file failed to parse correctly...
Definition: FileReader.cs:24
static readonly string dataDir
Definition: Calibration.cs:49
static bool initialized
Definition: Calibration.cs:52
static void Serialize()
Definition: Calibration.cs:88
static readonly string configFileName
Definition: Calibration.cs:50
static void DeserializeCalibration(JsonObject parsedValue)
Definition: Calibration.cs:69
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitiateCalibration()

static void BGC.Audio.Calibration.InitiateCalibration ( int []  testLevels)
inlinestatic

Definition at line 124 of file Calibration.cs.

125  {
126  //Make a new inProgressList that's a copy of the appropriate values
128  {
129  case Source.Custom:
130  inProgressCalibration = new List<CalibrationPoint>(customCalibration);
131  break;
132 
133  case Source.Results:
134  inProgressCalibration = new List<CalibrationPoint>(resultsCalibration);
135  break;
136 
137  case Source.Default:
138  inProgressCalibration = new List<CalibrationPoint>(testLevels.Length);
139 
140  foreach (int level in testLevels)
141  {
142  inProgressCalibration.Add(new CalibrationPoint(
143  levelIn: level,
144  levelOffsetL: 0.0,
145  levelOffsetR: 0.0));
146  }
147  break;
148 
149  case Source.InProgress:
150  default:
151  Debug.LogError($"Unexpected Source: {GetSourceForVerificationPanel()}");
152  break;
153  }
154  }
static List< CalibrationPoint > inProgressCalibration
Definition: Calibration.cs:47
static List< CalibrationPoint > customCalibration
Definition: Calibration.cs:45
static Source GetSourceForVerificationPanel()
Definition: Calibration.cs:224
static List< CalibrationPoint > resultsCalibration
Definition: Calibration.cs:46

◆ PushCalibrationResults()

static void BGC.Audio.Calibration.PushCalibrationResults ( )
inlinestatic

Definition at line 186 of file Calibration.cs.

187  {
188  if (resultsCalibration == null)
189  {
190  Debug.LogError("Null results calibration");
191  return;
192  }
193 
195  resultsCalibration = null;
196 
197  Serialize();
198  }
static void Serialize()
Definition: Calibration.cs:88
static List< CalibrationPoint > customCalibration
Definition: Calibration.cs:45
static List< CalibrationPoint > resultsCalibration
Definition: Calibration.cs:46

◆ Serialize()

static void BGC.Audio.Calibration.Serialize ( )
inlinestatic

Definition at line 88 of file Calibration.cs.

References BGC.IO.DataManagement.PathForDataFile(), and BGC.IO.FileWriter.WriteJson().

89  {
92  createJson: SerializeCalibration,
93  pretty: true);
94  }
static readonly string dataDir
Definition: Calibration.cs:49
static bool WriteJson(string path, Func< JsonObject > createJson, bool pretty=false, Action failCallback=null, Action< string, Exception > overrideExceptionHandling=null)
Definition: FileWriter.cs:11
static JsonObject SerializeCalibration()
Definition: Calibration.cs:96
static readonly string configFileName
Definition: Calibration.cs:50
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
Here is the call graph for this function:

◆ SerializeCalibration()

static JsonObject BGC.Audio.Calibration.SerializeCalibration ( )
inlinestaticprivate

Definition at line 96 of file Calibration.cs.

References LightJson.JsonArray.Add(), BGC.Audio.Calibration.Keys.CustomCalibration, BGC.Audio.Calibration.Keys.LAdjustment, BGC.Audio.Calibration.Keys.Level, BGC.Audio.Calibration.CalibrationPoint.levelIn, BGC.Audio.Calibration.CalibrationPoint.levelOffsetL, BGC.Audio.Calibration.CalibrationPoint.levelOffsetR, and BGC.Audio.Calibration.Keys.RAdjustment.

97  {
98  JsonArray jsonCalibrationArray = new JsonArray();
99  if (customCalibration != null)
100  {
101  foreach (CalibrationPoint point in customCalibration)
102  {
103  jsonCalibrationArray.Add(new JsonObject()
104  {
105  { Keys.Level, point.levelIn },
106  { Keys.LAdjustment, point.levelOffsetL },
107  { Keys.RAdjustment, point.levelOffsetR }
108  });
109  }
110  }
111 
112  return new JsonObject()
113  {
114  { Keys.CustomCalibration, jsonCalibrationArray }
115  };
116  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
static List< CalibrationPoint > customCalibration
Definition: Calibration.cs:45
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
Here is the call graph for this function:

◆ SubmitCalibrationValue()

static void BGC.Audio.Calibration.SubmitCalibrationValue ( int  step,
AudioChannel  channel,
double  offset 
)
inlinestatic

Definition at line 156 of file Calibration.cs.

160  {
161  double left = inProgressCalibration[step].levelOffsetL;
162  double right = inProgressCalibration[step].levelOffsetR;
163 
164  switch (channel)
165  {
166  case AudioChannel.Left:
167  left += offset;
168  break;
169 
170  case AudioChannel.Right:
171  right += offset;
172  break;
173 
174  default:
175  Debug.LogError($"Unexpected Channel: {channel}");
176  break;
177  }
178 
179  inProgressCalibration[step] = new CalibrationPoint(
180  levelIn: inProgressCalibration[step].levelIn,
181  levelOffsetL: left,
182  levelOffsetR: right);
183 
184  }
static List< CalibrationPoint > inProgressCalibration
Definition: Calibration.cs:47

Field Documentation

◆ configFileName

readonly string BGC.Audio.Calibration.configFileName = "Calibration.json"
staticprivate

Definition at line 50 of file Calibration.cs.

◆ customCalibration

List<CalibrationPoint> BGC.Audio.Calibration.customCalibration = null
staticprivate

Definition at line 45 of file Calibration.cs.

◆ dataDir

readonly string BGC.Audio.Calibration.dataDir = "System"
staticprivate

Definition at line 49 of file Calibration.cs.

◆ initialized

bool BGC.Audio.Calibration.initialized = false
staticprivate

Definition at line 52 of file Calibration.cs.

◆ inProgressCalibration

List<CalibrationPoint> BGC.Audio.Calibration.inProgressCalibration = null
staticprivate

Definition at line 47 of file Calibration.cs.

◆ resultsCalibration

List<CalibrationPoint> BGC.Audio.Calibration.resultsCalibration = null
staticprivate

Definition at line 46 of file Calibration.cs.


The documentation for this class was generated from the following file: