|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace Spectrogram |
| 9 | +{ |
| 10 | + public class SFF |
| 11 | + { |
| 12 | + public readonly byte VersionMajor = 1; |
| 13 | + public readonly byte VersionMinor = 1; |
| 14 | + |
| 15 | + // time information |
| 16 | + int SampleRate = 44100; |
| 17 | + int StepSize = 1024; |
| 18 | + int StepCount = 123; |
| 19 | + |
| 20 | + // frequency information |
| 21 | + int FftSize = 1024; |
| 22 | + int FftFirstIndex = 100; |
| 23 | + int FftHeight = 824; |
| 24 | + int OffsetHz = 0; |
| 25 | + |
| 26 | + public SFF() |
| 27 | + { |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + public void Load(string filePath) |
| 32 | + { |
| 33 | + byte[] bytes = File.ReadAllBytes(filePath); |
| 34 | + |
| 35 | + // ensure the first 4 bytes match what we expect |
| 36 | + int magicNumber = BitConverter.ToInt32(bytes, 0); |
| 37 | + if (magicNumber != 1179014099) |
| 38 | + throw new InvalidDataException("not a valid SFF file"); |
| 39 | + |
| 40 | + // read file version |
| 41 | + byte versionMajor = bytes[40]; |
| 42 | + byte versionMinor = bytes[41]; |
| 43 | + Console.WriteLine($"SFF version {versionMajor}.{versionMinor}"); |
| 44 | + |
| 45 | + // read time information |
| 46 | + int sampleRate = BitConverter.ToInt32(bytes, 42); |
| 47 | + int stepSize = BitConverter.ToInt32(bytes, 46); |
| 48 | + int stepCount = BitConverter.ToInt32(bytes, 50); |
| 49 | + Console.WriteLine($"Sample rate: {sampleRate} Hz"); |
| 50 | + Console.WriteLine($"Step size: {stepSize} samples"); |
| 51 | + Console.WriteLine($"Step count: {stepCount} steps"); |
| 52 | + |
| 53 | + // read frequency information |
| 54 | + int fftSize = BitConverter.ToInt32(bytes, 54); |
| 55 | + int fftFirstIndex = BitConverter.ToInt32(bytes, 58); |
| 56 | + int fftHeight = BitConverter.ToInt32(bytes, 62); |
| 57 | + int offsetHz = BitConverter.ToInt32(bytes, 66); |
| 58 | + Console.WriteLine($"FFT size: {fftSize}"); |
| 59 | + Console.WriteLine($"FFT first index: {fftFirstIndex}"); |
| 60 | + Console.WriteLine($"FFT height: {fftHeight}"); |
| 61 | + Console.WriteLine($"FFT offset: {offsetHz} Hz"); |
| 62 | + |
| 63 | + // data format |
| 64 | + byte valuesPerPoint = bytes[70]; |
| 65 | + bool isComplex = valuesPerPoint == 2; |
| 66 | + byte bytesPerValue = bytes[71]; |
| 67 | + bool decibels = bytes[72] == 1; |
| 68 | + Console.WriteLine($"Values per point: {valuesPerPoint}"); |
| 69 | + Console.WriteLine($"Complex values: {isComplex}"); |
| 70 | + Console.WriteLine($"Bytes per point: {bytesPerValue}"); |
| 71 | + Console.WriteLine($"Decibels: {decibels}"); |
| 72 | + |
| 73 | + // recording start time |
| 74 | + DateTime dt = new DateTime(bytes[74] + 2000, bytes[75], bytes[76], bytes[77], bytes[78], bytes[79]); |
| 75 | + Console.WriteLine($"Recording start (UTC): {dt}"); |
| 76 | + |
| 77 | + // data storage |
| 78 | + UInt32 firstDataByte = BitConverter.ToUInt32(bytes, 80); |
| 79 | + Console.WriteLine($"First data byte: {firstDataByte}"); |
| 80 | + } |
| 81 | + |
| 82 | + public void Save(string filePath) |
| 83 | + { |
| 84 | + byte[] header = new byte[256]; |
| 85 | + |
| 86 | + // file type designator |
| 87 | + header[0] = 211; // intentionally non-ASCII |
| 88 | + header[1] = (byte)'S'; |
| 89 | + header[2] = (byte)'F'; |
| 90 | + header[3] = (byte)'F'; |
| 91 | + header[4] = (byte)'\r'; |
| 92 | + header[5] = (byte)'\n'; |
| 93 | + header[6] = (byte)' '; |
| 94 | + header[7] = (byte)'\n'; |
| 95 | + |
| 96 | + int magicNumber = BitConverter.ToInt32(header, 0); |
| 97 | + if (magicNumber != 1179014099) |
| 98 | + throw new InvalidDataException("magic number for SFF files is 1179014099"); |
| 99 | + |
| 100 | + // plain text helpful for people who open this file in a text editor |
| 101 | + string fileInfo = $"Spectrogram File Format {VersionMajor}.{VersionMinor}\r\n"; |
| 102 | + byte[] fileInfoBytes = Encoding.UTF8.GetBytes(fileInfo); |
| 103 | + if (fileInfoBytes.Length > 32) |
| 104 | + throw new InvalidDataException("file info cannot exceed 32 bytes"); |
| 105 | + Array.Copy(fileInfoBytes, 0, header, 8, fileInfoBytes.Length); |
| 106 | + |
| 107 | + // version |
| 108 | + header[40] = VersionMajor; |
| 109 | + header[41] = VersionMinor; |
| 110 | + |
| 111 | + // time information |
| 112 | + Array.Copy(BitConverter.GetBytes(SampleRate), 0, header, 42, 4); |
| 113 | + Array.Copy(BitConverter.GetBytes(StepSize), 0, header, 46, 4); |
| 114 | + Array.Copy(BitConverter.GetBytes(StepCount), 0, header, 50, 4); |
| 115 | + |
| 116 | + // frequency information |
| 117 | + Array.Copy(BitConverter.GetBytes(FftSize), 0, header, 54, 4); |
| 118 | + Array.Copy(BitConverter.GetBytes(FftFirstIndex), 0, header, 58, 4); |
| 119 | + Array.Copy(BitConverter.GetBytes(FftHeight), 0, header, 62, 4); |
| 120 | + Array.Copy(BitConverter.GetBytes(OffsetHz), 0, header, 66, 4); |
| 121 | + |
| 122 | + // data encoding details |
| 123 | + byte valuesPerPoint = 2; // 1 for magnitude or power data, 2 for complex data |
| 124 | + byte bytesPerValue = 8; // a double is 8 bytes |
| 125 | + byte decibelUnits = 0; // 1 if units are in dB |
| 126 | + byte dataExtraByte = 0; // unused |
| 127 | + header[70] = valuesPerPoint; |
| 128 | + header[71] = bytesPerValue; |
| 129 | + header[72] = decibelUnits; |
| 130 | + header[73] = dataExtraByte; |
| 131 | + |
| 132 | + // source file date and time |
| 133 | + header[74] = (byte)(DateTime.UtcNow.Year - 2000); // 2-digit year |
| 134 | + header[75] = (byte)DateTime.UtcNow.Month; |
| 135 | + header[76] = (byte)DateTime.UtcNow.Day; |
| 136 | + header[77] = (byte)DateTime.UtcNow.Hour; |
| 137 | + header[78] = (byte)DateTime.UtcNow.Minute; |
| 138 | + header[79] = (byte)DateTime.UtcNow.Second; |
| 139 | + |
| 140 | + // binary data location |
| 141 | + int firstDataByte = header.Length; |
| 142 | + Array.Copy(BitConverter.GetBytes(firstDataByte), 0, header, 80, 4); |
| 143 | + |
| 144 | + // create bytes to write to file |
| 145 | + int dataPointCount = FftHeight * StepCount; |
| 146 | + int bytesPerPoint = bytesPerValue * valuesPerPoint; |
| 147 | + byte[] fileBytes = new byte[header.Length + dataPointCount * bytesPerPoint]; |
| 148 | + Array.Copy(header, 0, fileBytes, 0, header.Length); |
| 149 | + |
| 150 | + // copy data into byte area |
| 151 | + int bytesPerColumn = FftHeight * bytesPerPoint; |
| 152 | + for (int x = 0; x < StepCount; x++) |
| 153 | + { |
| 154 | + int columnOffset = bytesPerColumn * x; |
| 155 | + for (int y = 0; y < FftHeight; y++) |
| 156 | + { |
| 157 | + int rowOffset = y * bytesPerPoint; |
| 158 | + int valueOffset = firstDataByte + columnOffset + rowOffset; |
| 159 | + double value = double.Parse($"{x}.{y}"); |
| 160 | + Array.Copy(BitConverter.GetBytes(value), 0, fileBytes, valueOffset, 8); |
| 161 | + Array.Copy(BitConverter.GetBytes(-value), 0, fileBytes, valueOffset + 8, 8); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // write file to disk |
| 166 | + File.WriteAllBytes(filePath, fileBytes); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments