Skip to content

Commit 0e0de2d

Browse files
committed
SFF ImageHeight and ImageWidth properties
#23
1 parent d757b66 commit 0e0de2d

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/Spectrogram/SFF.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,27 @@ public class SFF
2929
public bool Decibels { get; private set; }
3030
public bool IsMel { get { return MelBinCount > 0; } }
3131

32-
// FFT details
33-
public int FftHeight { get { return Ffts[0].Length; } }
34-
public int FftWidth { get { return Ffts.Count; } }
32+
// image details
3533
public List<double[]> Ffts { get; private set; }
34+
public int ImageHeight { get { return (Ffts is null) ? 0 : Ffts[0].Length; } }
35+
public int ImageWidth { get { return (Ffts is null) ? 0 : Ffts.Count; } }
36+
37+
[Obsolete("use ImageWidth", error: false)]
38+
public int FftWidth { get { return ImageWidth; } }
39+
40+
[Obsolete("use ImageHeight", error: false)]
41+
public int FftHeight { get { return ImageHeight; } }
3642

3743
public SFF()
3844
{
3945

4046
}
4147

48+
public override string ToString()
49+
{
50+
return $"SFF {ImageWidth}x{ImageHeight}";
51+
}
52+
4253
public SFF(string loadFilePath)
4354
{
4455
Load(loadFilePath);
@@ -191,25 +202,25 @@ public void Save(string filePath)
191202

192203
// ADD NEW VALUES HERE (after byte 80)
193204
Array.Copy(BitConverter.GetBytes(MelBinCount), 0, header, 84, 4);
194-
Array.Copy(BitConverter.GetBytes(FftHeight), 0, header, 88, 4);
195-
Array.Copy(BitConverter.GetBytes(FftWidth), 0, header, 92, 4);
205+
Array.Copy(BitConverter.GetBytes(ImageHeight), 0, header, 88, 4);
206+
Array.Copy(BitConverter.GetBytes(ImageWidth), 0, header, 92, 4);
196207

197208
// binary data location (keep this at byte 80)
198209
int firstDataByte = header.Length;
199210
Array.Copy(BitConverter.GetBytes(firstDataByte), 0, header, 80, 4);
200211

201212
// create bytes to write to file
202-
int dataPointCount = FftHeight * FftWidth;
213+
int dataPointCount = ImageHeight * ImageWidth;
203214
int bytesPerPoint = bytesPerValue * valuesPerPoint;
204215
byte[] fileBytes = new byte[header.Length + dataPointCount * bytesPerPoint];
205216
Array.Copy(header, 0, fileBytes, 0, header.Length);
206217

207218
// copy data into byte area
208-
int bytesPerColumn = FftHeight * bytesPerPoint;
209-
for (int x = 0; x < FftWidth; x++)
219+
int bytesPerColumn = ImageHeight * bytesPerPoint;
220+
for (int x = 0; x < ImageWidth; x++)
210221
{
211222
int columnOffset = bytesPerColumn * x;
212-
for (int y = 0; y < FftHeight; y++)
223+
for (int y = 0; y < ImageHeight; y++)
213224
{
214225
int rowOffset = y * bytesPerPoint;
215226
int valueOffset = firstDataByte + columnOffset + rowOffset;
@@ -228,11 +239,11 @@ public void Save(string filePath)
228239

229240
double maxFreq = SampleRate / 2;
230241
double maxMel = FftSharp.Transform.MelFromFreq(maxFreq);
231-
double frac = (FftHeight - y) / (double)FftHeight;
242+
double frac = (ImageHeight - y) / (double)ImageHeight;
232243
double freq = IsMel ? FftSharp.Transform.MelToFreq(frac * maxMel) : frac * maxFreq;
233244

234245
double mag = double.NaN;
235-
try { mag = Ffts[x][FftHeight - y - 1]; } catch { }
246+
try { mag = Ffts[x][ImageHeight - y - 1]; } catch { }
236247

237248
return (timeSec, freq, mag);
238249
}

src/Spectrogram/Tools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static double GetPeakFrequency(SFF sff, bool firstFftOnly = false)
6464
}
6565

6666
double maxFreq = sff.SampleRate / 2;
67-
double frac = peakIndex / (double)sff.FftHeight;
67+
double frac = peakIndex / (double)sff.ImageHeight;
6868

6969
if (sff.MelBinCount > 0)
7070
{

0 commit comments

Comments
 (0)