|
| 1 | +using ColorChord.NET.NoteFinder; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace Gen2DFTLib; |
| 5 | + |
| 6 | +public static unsafe class Gen2DFT |
| 7 | +{ |
| 8 | + private static Gen2NoteFinderDFT DFT; |
| 9 | + |
| 10 | + /// <summary> Prepares the DFT for use, initializing internal data structures. </summary> |
| 11 | + /// <param name="octaveCount"> The number of octaves to analyze </param> |
| 12 | + /// <param name="sampleRate"> The sample rate of the input audio </param> |
| 13 | + /// <param name="startFrequency"> The desired frequency of the lowest bin </param> |
| 14 | + /// <param name="loudnessCorrection"> The amount of human-modelled loudness equalization to apply to the output bins </param> |
| 15 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_Init")] |
| 16 | + public static void Init(uint octaveCount, uint sampleRate, float startFrequency, float loudnessCorrection) |
| 17 | + { |
| 18 | + DFT = new(octaveCount, sampleRate, startFrequency, loudnessCorrection, null); |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> Processes the given audio data into the DFT. </summary> |
| 22 | + /// <param name="newData"> The data to process, as full-range I16s </param> |
| 23 | + /// <param name="count"> The length of the data array to read and process </param> |
| 24 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_AddAudioData")] |
| 25 | + public static void AddAudioData(short* newData, uint count) |
| 26 | + { |
| 27 | + ReadOnlySpan<short> Data = new(newData, (int)count); |
| 28 | + DFT.AddAudioData(Data); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> Updates the output values for reading based on the audio data that has been added until now. </summary> |
| 32 | + /// <remarks> Read the audio data using <see cref="GetBinMagnitudes"/>. </remarks> |
| 33 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_CalculateOutput")] |
| 34 | + public static void CalculateOutput() |
| 35 | + { |
| 36 | + DFT.CalculateOutput(); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> Gets the number of output bins per octave. </summary> |
| 40 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_GetBinsPerOctave")] |
| 41 | + public static uint GetBinsPerOctave() => DFT.BinsPerOctave; |
| 42 | + |
| 43 | + /// <summary> Gets the number of bins in total, across all octaves. </summary> |
| 44 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_GetBinCount")] |
| 45 | + public static uint GetBinCount() => DFT.BinCount; |
| 46 | + |
| 47 | + /// <summary> Gets the current DFT output data. Make sure to call <see cref="CalculateOutput"/> before this. </summary> |
| 48 | + /// <returns> The raw magnitudes of each DFT bin, length is <see cref="GetBinCount"/>. </returns> |
| 49 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_GetBinMagnitudes")] |
| 50 | + public static float* GetBinMagnitudes() |
| 51 | + { |
| 52 | + fixed (float* BinMags = DFT.RawBinMagnitudes) { return BinMags; } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> Gets the central response frequencies of each DFT bin. </summary> |
| 56 | + /// <returns> The frequency of each DFT bin in Hz, length is <see cref="GetBinCount"/>. </returns> |
| 57 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_GetBinFrequencies")] |
| 58 | + public static float* GetBinFrequencies() |
| 59 | + { |
| 60 | + fixed (float* BinFreqs = DFT.RawBinFrequencies) { return BinFreqs; } |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> Gets the sensitivity range width of each bin, in number of bins. </summary> |
| 64 | + /// <returns> The width of each bin in number of bins, length is <see cref="GetBinCount"/>. A value of 2 would mean that this bin stops responding to frequencies around the center of the bin above and below (this bin's range = 1, plus 0.5 bins on either side) </returns> |
| 65 | + [UnmanagedCallersOnly(EntryPoint = "Gen2DFT_GetBinWidths")] |
| 66 | + public static float* GetBinWidths() |
| 67 | + { |
| 68 | + fixed (float* BinWidths = DFT.RawBinWidths) { return BinWidths; } |
| 69 | + } |
| 70 | +} |
0 commit comments