BGC Tools
Public Member Functions
BGC.Tests.MidiEncodingTests Class Reference

Public Member Functions

void TestLoadSaveMidiFull ()
 
void TestRenderMidi ()
 
void TestRenderToccataMidi ()
 
void TestPulses ()
 
void TestGuitar ()
 
void TestOrgan ()
 
void TestFlute ()
 
void TestContinuousFilter ()
 
void TestLoadSaveMidiBare ()
 
void TestSnare ()
 
void TestHiHat ()
 

Detailed Description

Definition at line 19 of file MidiEncodingTests.cs.

Member Function Documentation

◆ TestContinuousFilter()

void BGC.Tests.MidiEncodingTests.TestContinuousFilter ( )
inline

Definition at line 177 of file MidiEncodingTests.cs.

References BGC.Audio.Calibration.Initialize(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

178  {
180 
181  IBGCStream noiseStream = new NoiseAudioClip(
182  duration: 4,
183  rms: 1.0,
184  freqLB: 20.0,
185  freqUB: 10000.0,
186  frequencyCount: 10000,
187  distribution: NoiseAudioClip.AmplitudeDistribution.Pink);
188 
189 
190  for (ContinuousFilter.FilterType filter = 0; filter < ContinuousFilter.FilterType.MAX; filter++)
191  {
193  filepath: DataManagement.PathForDataFile("Test", $"Sigmoid{filter}FilteredNoise.wav"),
194  stream: new ContinuousFilter(
195  stream: noiseStream,
196  filterEnvelope: new SigmoidEnvelope(4.0, 1.0),
197  filterType: filter,
198  freqLB: 20,
199  freqUB: 10000)
200  .Normalize(80),
201  overwrite: true);
202 
204  filepath: DataManagement.PathForDataFile("Test", $"Linear{filter}FilteredNoise.wav"),
205  stream: new ContinuousFilter(
206  stream: noiseStream,
207  filterEnvelope: new LinearEnvelope(4.0),
208  filterType: filter,
209  freqLB: 20,
210  freqUB: 10000)
211  .Normalize(80),
212  overwrite: true);
213 
215  filepath: DataManagement.PathForDataFile("Test", $"SlowSine{filter}FilteredNoise.wav"),
216  stream: new ContinuousFilter(
217  stream: noiseStream,
218  filterEnvelope: new SineWave(1.0, 1.0),
219  filterType: filter,
220  freqLB: 20,
221  freqUB: 10000)
222  .Normalize(80),
223  overwrite: true);
224 
226  filepath: DataManagement.PathForDataFile("Test", $"FastSine{filter}FilteredNoise.wav"),
227  stream: new ContinuousFilter(
228  stream: noiseStream,
229  filterEnvelope: new SineWave(1.0, 50.0),
230  filterType: filter,
231  freqLB: 20,
232  freqUB: 10000)
233  .Normalize(80),
234  overwrite: true);
235 
237  filepath: DataManagement.PathForDataFile("Test", $"SlowTriangle{filter}FilteredNoise.wav"),
238  stream: new ContinuousFilter(
239  stream: noiseStream,
240  filterEnvelope: new TriangleWave(1.0, 1.0),
241  filterType: filter,
242  freqLB: 20,
243  freqUB: 10000)
244  .Normalize(80),
245  overwrite: true);
246 
248  filepath: DataManagement.PathForDataFile("Test", $"FastTriangle{filter}FilteredNoise.wav"),
249  stream: new ContinuousFilter(
250  stream: noiseStream,
251  filterEnvelope: new TriangleWave(1.0, 50.0),
252  filterType: filter,
253  freqLB: 20,
254  freqUB: 10000)
255  .Normalize(80),
256  overwrite: true);
257 
258  }
259  }
static void Initialize()
Definition: Calibration.cs:54
Manages calibration values and processes
Definition: Calibration.cs:13
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
This implementation is based on a naive adaptation of the BiQuad filter in this pacakge, which is based on the one found in CSCore, which in turn was based on http://www.earlevel.com/main/2011/01/02/biquad-formulas/
Limited duration noise stream
Sine wave stream with specified amplitude, frequency, and initialPhase. One period is cached and the ...
Definition: SineWave.cs:15
Triangle wave stream with specified amplitude, frequency, and duty-cycle.
Definition: TriangleWave.cs:10
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestFlute()

void BGC.Tests.MidiEncodingTests.TestFlute ( )
inline

Definition at line 155 of file MidiEncodingTests.cs.

References BGC.Audio.Midi.Synth.InstrumentLookup.GetNote(), BGC.Audio.Calibration.Initialize(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

156  {
158 
159  for (int octave = 2; octave <= 8; octave++)
160  {
161  string guitarFile = DataManagement.PathForDataFile("Test", $"fluteE{octave}.wav");
162 
163  Assert.IsTrue(WaveEncoding.SaveStream(
164  filepath: guitarFile,
165  stream: InstrumentLookup.GetNote(
166  set: ReservedSoundSet.Flute,
167  note: (byte)(12 * octave + 4),
168  velocity: 0xF7)
169  .Window(4.0)
170  .SafeCache()
171  .SlowRangeFitter(),
172  overwrite: true));
173  }
174  }
static IBGCStream GetNote(NoteMidiEvent noteEvent, byte set)
static void Initialize()
Definition: Calibration.cs:54
Manages calibration values and processes
Definition: Calibration.cs:13
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
ReservedSoundSet
Offset to be 0-indexed Source: Complete MIDI Specs General MIDI System Level 1 - Pg 5 - Table 2 ...
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestGuitar()

void BGC.Tests.MidiEncodingTests.TestGuitar ( )
inline

Definition at line 112 of file MidiEncodingTests.cs.

References BGC.Audio.Midi.Synth.InstrumentLookup.GetNote(), BGC.Audio.Calibration.Initialize(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

113  {
115 
116  for (int octave = 2; octave <= 4; octave++)
117  {
118  string guitarFile = DataManagement.PathForDataFile("Test", $"guitarE{octave}.wav");
119 
120  Assert.IsTrue(WaveEncoding.SaveStream(
121  filepath: guitarFile,
122  stream: InstrumentLookup.GetNote(
123  set: ReservedSoundSet.ElectricGuitar_Jazz,
124  note: (byte)(12 * octave + 4),
125  velocity: 0xF7)
126  .SafeCache()
127  .SlowRangeFitter(),
128  overwrite: true));
129  }
130  }
static IBGCStream GetNote(NoteMidiEvent noteEvent, byte set)
static void Initialize()
Definition: Calibration.cs:54
Manages calibration values and processes
Definition: Calibration.cs:13
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
ReservedSoundSet
Offset to be 0-indexed Source: Complete MIDI Specs General MIDI System Level 1 - Pg 5 - Table 2 ...
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestHiHat()

void BGC.Tests.MidiEncodingTests.TestHiHat ( )
inline

Definition at line 298 of file MidiEncodingTests.cs.

References BGC.Audio.Midi.Synth.InstrumentLookup.GetPercussion(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

299  {
300  string closedFile = DataManagement.PathForDataFile("Test", "ClosedHiHat.wav");
301 
302  Assert.IsTrue(WaveEncoding.SaveStream(
303  filepath: closedFile,
304  stream: InstrumentLookup.GetPercussion(PercussionMap.ClosedHiHat, 0xF7)
305  .SafeCache()
306  .SlowRangeFitter(),
307  overwrite: true));
308 
309  string pedalFile = DataManagement.PathForDataFile("Test", "PedalHiHat.wav");
310 
311  Assert.IsTrue(WaveEncoding.SaveStream(
312  filepath: pedalFile,
313  stream: InstrumentLookup.GetPercussion(PercussionMap.PedalHiHat, 0xF7)
314  .SafeCache()
315  .SlowRangeFitter(),
316  overwrite: true));
317 
318  string openFile = DataManagement.PathForDataFile("Test", "OpenHiHat.wav");
319 
320  Assert.IsTrue(WaveEncoding.SaveStream(
321  filepath: openFile,
322  stream: InstrumentLookup.GetPercussion(PercussionMap.OpenHiHat, 0xF7)
323  .SafeCache()
324  .SlowRangeFitter(),
325  overwrite: true));
326  }
static IBGCStream GetPercussion(PercussionMap percussion, byte velocity)
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
PercussionMap
Already correctly maps to Frequency Source: Complete MIDI Specs General MIDI System Level 1 - Pg 6 - ...
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestLoadSaveMidiBare()

void BGC.Tests.MidiEncodingTests.TestLoadSaveMidiBare ( )
inline

Definition at line 262 of file MidiEncodingTests.cs.

References BGC.Audio.Midi.MidiEncoding.LoadFile(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.Midi.MidiEncoding.SaveFile().

263  {
264  string loadFile = DataManagement.PathForDataFile("Test", "MIDI_sample.mid");
265  string saveFile = DataManagement.PathForDataFile("Test", "MIDI_sample_barecopy.mid");
266 
267  Assert.IsTrue(MidiEncoding.LoadFile(
268  filePath: loadFile,
269  midiFile: out MidiFile midiFile,
270  retainAll: false));
271 
272  Assert.IsTrue(MidiEncoding.SaveFile(
273  filePath: saveFile,
274  midiFile: midiFile,
275  overwrite: true));
276 
277 
278  Assert.IsTrue(File.Exists(saveFile));
279  }
static bool SaveFile(string filePath, MidiFile midiFile, bool overwrite=false)
static bool LoadFile(string filePath, out MidiFile midiFile, bool retainAll=false)
Method to load a MIDI file an parse the data
Definition: MIDIEncoding.cs:77
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:

◆ TestLoadSaveMidiFull()

void BGC.Tests.MidiEncodingTests.TestLoadSaveMidiFull ( )
inline

Definition at line 22 of file MidiEncodingTests.cs.

References BGC.Audio.Midi.MidiEncoding.LoadFile(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.Midi.MidiEncoding.SaveFile().

23  {
24  string loadFile = DataManagement.PathForDataFile("Test", "MIDI_sample.mid");
25  string saveFile = DataManagement.PathForDataFile("Test", "MIDI_sample_copy.mid");
26 
27  Assert.IsTrue(MidiEncoding.LoadFile(
28  filePath: loadFile,
29  midiFile: out MidiFile midiFile,
30  retainAll: true));
31 
32  Debug.Log($"Format: {midiFile.headerInfo.format}");
33  Debug.Log($"FramesPerSecond: {midiFile.headerInfo.framesPerSecond}");
34  Debug.Log($"TicksPerFrame: {midiFile.headerInfo.ticksPerFrame}");
35  Debug.Log($"TicksPerQuarter: {midiFile.headerInfo.ticksPerQuarter}");
36  Debug.Log($"Tracks: {midiFile.headerInfo.tracks}");
37 
38  Debug.Log($"Samples Per Tick: {1E-6 * midiFile.tracks[0].Tempo * 44100 / midiFile.headerInfo.ticksPerQuarter}");
39 
40  Assert.IsTrue(MidiEncoding.SaveFile(
41  filePath: saveFile,
42  midiFile: midiFile,
43  overwrite: true));
44 
45  Assert.IsTrue(File.Exists(saveFile));
46  }
static bool SaveFile(string filePath, MidiFile midiFile, bool overwrite=false)
static bool LoadFile(string filePath, out MidiFile midiFile, bool retainAll=false)
Method to load a MIDI file an parse the data
Definition: MIDIEncoding.cs:77
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:

◆ TestOrgan()

void BGC.Tests.MidiEncodingTests.TestOrgan ( )
inline

Definition at line 133 of file MidiEncodingTests.cs.

References BGC.Audio.Midi.Synth.InstrumentLookup.GetNote(), BGC.Audio.Calibration.Initialize(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

134  {
136 
137  for (int octave = 2; octave <= 7; octave++)
138  {
139  string guitarFile = DataManagement.PathForDataFile("Test", $"organE{octave}.wav");
140 
141  Assert.IsTrue(WaveEncoding.SaveStream(
142  filepath: guitarFile,
143  stream: InstrumentLookup.GetNote(
144  set: ReservedSoundSet.CrutchOrgan,
145  note: (byte)(12 * octave + 4),
146  velocity: 0xF7)
147  .Window(4.0)
148  .SafeCache()
149  .SlowRangeFitter(),
150  overwrite: true));
151  }
152  }
static IBGCStream GetNote(NoteMidiEvent noteEvent, byte set)
static void Initialize()
Definition: Calibration.cs:54
Manages calibration values and processes
Definition: Calibration.cs:13
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
ReservedSoundSet
Offset to be 0-indexed Source: Complete MIDI Specs General MIDI System Level 1 - Pg 5 - Table 2 ...
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestPulses()

void BGC.Tests.MidiEncodingTests.TestPulses ( )
inline

Definition at line 93 of file MidiEncodingTests.cs.

References BGC.Audio.Calibration.Initialize(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

94  {
96 
97  double[] dutyCycles = new double[] { 0.1, 0.25, 0.5, 0.75, 0.9 };
98 
99  foreach (double dutyCycle in dutyCycles)
100  {
101  string saveFile = DataManagement.PathForDataFile("Test", $"squareWave({dutyCycle}).wav");
102 
103  Assert.IsTrue(WaveEncoding.SaveStream(
104  filepath: saveFile,
105  stream: new SquareWave(1.0, 400, dutyCycle: dutyCycle)
106  .Window(3.0),
107  overwrite: true));
108  }
109  }
Square wave stream.
Definition: SquareWave.cs:10
static void Initialize()
Definition: Calibration.cs:54
Manages calibration values and processes
Definition: Calibration.cs:13
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestRenderMidi()

void BGC.Tests.MidiEncodingTests.TestRenderMidi ( )
inline

Definition at line 49 of file MidiEncodingTests.cs.

References BGC.Audio.Calibration.Initialize(), BGC.Audio.Midi.MidiEncoding.LoadFile(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

50  {
52 
53  string loadFile = DataManagement.PathForDataFile("Test", "MIDI_sample.mid");
54  string saveFile = DataManagement.PathForDataFile("Test", "MIDI_sample.wav");
55 
56  Assert.IsTrue(MidiEncoding.LoadFile(
57  filePath: loadFile,
58  midiFile: out MidiFile midiFile,
59  retainAll: true));
60 
61  Assert.IsTrue(WaveEncoding.SaveStream(
62  filepath: saveFile,
63  stream: new SlowRangeFitterFilter(new MidiFileStream(midiFile).SafeCache()),
64  overwrite: true));
65 
66 
67  Assert.IsTrue(File.Exists(saveFile));
68  }
static void Initialize()
Definition: Calibration.cs:54
Manages calibration values and processes
Definition: Calibration.cs:13
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
This slow filter scans the whole sample and scales it between +1 and -1 so there is no clipping but m...
static bool LoadFile(string filePath, out MidiFile midiFile, bool retainAll=false)
Method to load a MIDI file an parse the data
Definition: MIDIEncoding.cs:77
Renders an underlying MidiFile
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestRenderToccataMidi()

void BGC.Tests.MidiEncodingTests.TestRenderToccataMidi ( )
inline

Definition at line 71 of file MidiEncodingTests.cs.

References BGC.Audio.Calibration.Initialize(), BGC.Audio.Midi.MidiEncoding.LoadFile(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

72  {
74 
75  string loadFile = DataManagement.PathForDataFile("Test", "toccata1.mid");
76  string saveFile = DataManagement.PathForDataFile("Test", "toccata1.wav");
77 
78  Assert.IsTrue(MidiEncoding.LoadFile(
79  filePath: loadFile,
80  midiFile: out MidiFile midiFile,
81  retainAll: true));
82 
83  Assert.IsTrue(WaveEncoding.SaveStream(
84  filepath: saveFile,
85  stream: new SlowRangeFitterFilter(new MidiFileStream(midiFile).SafeCache()),
86  overwrite: true));
87 
88 
89  Assert.IsTrue(File.Exists(saveFile));
90  }
static void Initialize()
Definition: Calibration.cs:54
Manages calibration values and processes
Definition: Calibration.cs:13
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
This slow filter scans the whole sample and scales it between +1 and -1 so there is no clipping but m...
static bool LoadFile(string filePath, out MidiFile midiFile, bool retainAll=false)
Method to load a MIDI file an parse the data
Definition: MIDIEncoding.cs:77
Renders an underlying MidiFile
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

◆ TestSnare()

void BGC.Tests.MidiEncodingTests.TestSnare ( )
inline

Definition at line 283 of file MidiEncodingTests.cs.

References BGC.Audio.Midi.Synth.InstrumentLookup.GetPercussion(), BGC.IO.DataManagement.PathForDataFile(), and BGC.Audio.WaveEncoding.SaveStream().

284  {
285  string saveFile = DataManagement.PathForDataFile("Test", "SnareTest.wav");
286 
287  Assert.IsTrue(WaveEncoding.SaveStream(
288  filepath: saveFile,
289  stream: InstrumentLookup.GetPercussion(PercussionMap.AcousticSnare, 0xF7)
290  .Normalize(80f)
291  .Window(1f)
292  .Center(1.5f),
293  overwrite: true));
294  }
static IBGCStream GetPercussion(PercussionMap percussion, byte velocity)
Operations for Loading and Saving WAV files Some documentation on the WAV format is available here: h...
Definition: WaveEncoding.cs:14
PercussionMap
Already correctly maps to Frequency Source: Complete MIDI Specs General MIDI System Level 1 - Pg 6 - ...
static string PathForDataFile(string dataDirectory, string fileName)
Returns the full path for specified datafile in a data directory
static bool SaveStream(string filepath, IBGCStream stream, bool overwrite=false)
Save the samples passed in as a WAVE file with the specified filepath. Returns success.
Here is the call graph for this function:

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