Skip to content

Commit 27397c0

Browse files
committed
Attempting to build an AOT native-compatible DLL for the DFT
1 parent cf1aaa4 commit 27397c0

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

ColorChord.NET.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AudioFileSource", "Extensio
2424
{2851D19F-7B96-4EA3-8438-90F186C07CC0} = {2851D19F-7B96-4EA3-8438-90F186C07CC0}
2525
EndProjectSection
2626
EndProject
27-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageOutput", "Extensions\ImageOutput\ImageOutput.csproj", "{A1C693C2-8B33-44C8-8A06-F9D1C881FFA1}"
27+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageOutput", "Extensions\ImageOutput\ImageOutput.csproj", "{A1C693C2-8B33-44C8-8A06-F9D1C881FFA1}"
2828
ProjectSection(ProjectDependencies) = postProject
2929
{2851D19F-7B96-4EA3-8438-90F186C07CC0} = {2851D19F-7B96-4EA3-8438-90F186C07CC0}
3030
EndProjectSection
3131
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gen2DFTLib", "Gen2DFTLib\Gen2DFTLib.csproj", "{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}"
33+
EndProject
3234
Global
3335
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3436
Debug|Any CPU = Debug|Any CPU
@@ -123,6 +125,18 @@ Global
123125
{A1C693C2-8B33-44C8-8A06-F9D1C881FFA1}.Release|x64.Build.0 = Release|Any CPU
124126
{A1C693C2-8B33-44C8-8A06-F9D1C881FFA1}.Release|x86.ActiveCfg = Release|Any CPU
125127
{A1C693C2-8B33-44C8-8A06-F9D1C881FFA1}.Release|x86.Build.0 = Release|Any CPU
128+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
129+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
130+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Debug|x64.ActiveCfg = Debug|Any CPU
131+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Debug|x64.Build.0 = Debug|Any CPU
132+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Debug|x86.ActiveCfg = Debug|Any CPU
133+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Debug|x86.Build.0 = Debug|Any CPU
134+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
135+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Release|Any CPU.Build.0 = Release|Any CPU
136+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Release|x64.ActiveCfg = Release|Any CPU
137+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Release|x64.Build.0 = Release|Any CPU
138+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Release|x86.ActiveCfg = Release|Any CPU
139+
{DD8C6573-7C5B-4FD7-BC18-7E4735C665D3}.Release|x86.Build.0 = Release|Any CPU
126140
EndGlobalSection
127141
GlobalSection(SolutionProperties) = preSolution
128142
HideSolutionNode = FALSE

Gen2DFTLib/Gen2DFT.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

Gen2DFTLib/Gen2DFTLib.csproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
8+
<PublishAot>true</PublishAot>
9+
<DefineConstants>STANDALONE_DFT_LIB</DefineConstants>
10+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
11+
</PropertyGroup>
12+
13+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
14+
<IsAotCompatible>True</IsAotCompatible>
15+
</PropertyGroup>
16+
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
18+
<IsAotCompatible>True</IsAotCompatible>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<Compile Include="..\ColorChord.NET\NoteFinder\Gen2NoteFinderDFT.cs" Link="Gen2NoteFinderDFT.cs" />
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)