forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFormat.cs
More file actions
95 lines (83 loc) · 3.92 KB
/
FileFormat.cs
File metadata and controls
95 lines (83 loc) · 3.92 KB
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
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
namespace Spectrogram.Tests
{
class FileFormat
{
[Test]
public void Test_SFF_Linear()
{
(int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav");
int fftSize = 1 << 12;
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 700, maxFreq: 2000);
spec.SetWindow(FftSharp.Window.Hanning(fftSize / 3)); // sharper window than typical
spec.Add(audio);
spec.SaveData("../../../../../dev/sff/hal.sff");
var spec2 = new SFF("../../../../../dev/sff/hal.sff");
Assert.AreEqual(spec.SampleRate, spec2.SampleRate);
Assert.AreEqual(spec.StepSize, spec2.StepSize);
Assert.AreEqual(spec.Width, spec2.Width);
Assert.AreEqual(spec.FftSize, spec2.FftSize);
Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex);
Assert.AreEqual(spec.Height, spec2.Height);
Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz);
}
[Test]
public void Test_SFF_Mel()
{
(int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav");
int fftSize = 1 << 12;
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 700);
spec.SetWindow(FftSharp.Window.Hanning(fftSize / 3)); // sharper window than typical
spec.Add(audio);
Bitmap bmp = spec.GetBitmapMel(250, 3, true);
bmp.Save("../../../../../dev/sff/halMel.png", System.Drawing.Imaging.ImageFormat.Png);
spec.SaveData("../../../../../dev/sff/halMel.sff", melBinCount: 250);
var spec2 = new SFF("../../../../../dev/sff/halMel.sff");
Assert.AreEqual(spec.SampleRate, spec2.SampleRate);
Assert.AreEqual(spec.StepSize, spec2.StepSize);
Assert.AreEqual(spec.Width, spec2.Width);
Assert.AreEqual(spec.FftSize, spec2.FftSize);
Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex);
Assert.AreEqual(spec.Height, spec2.Height);
Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz);
}
[Test]
public void Test_SFF_Linear2()
{
// test creating SFF file from 16-bit 48kHz mono WAV file
// read the wav file
(int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/03-02-03-01-02-01-19.wav");
Assert.AreEqual(48000, sampleRate);
// save the SFF
int fftSize = 1 << 12;
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 300, maxFreq: 2000);
spec.Add(audio);
spec.SaveData("testDoor.sff");
// load the SFF and verify all the values are the same
var spec2 = new SFF("testDoor.sff");
Assert.AreEqual(spec.SampleRate, spec2.SampleRate);
Assert.AreEqual(spec.StepSize, spec2.StepSize);
Assert.AreEqual(spec.Width, spec2.Width);
Assert.AreEqual(spec.FftSize, spec2.FftSize);
Assert.AreEqual(spec.NextColumnIndex, spec2.FftFirstIndex);
Assert.AreEqual(spec.Height, spec2.Height);
Assert.AreEqual(spec.OffsetHz, spec2.OffsetHz);
Assert.AreEqual("SFF 701x170", spec2.ToString());
}
[Test]
public void Test_SFF_LinearBigMaxFreq()
{
// test creating SFF file from 16-bit 48kHz mono WAV file
(int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/03-02-03-01-02-01-19.wav");
Assert.AreEqual(48000, sampleRate);
int fftSize = 1 << 12;
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 300, maxFreq: 7999);
spec.Add(audio);
spec.SaveData("testDoorBig.sff");
}
}
}