Skip to content

Commit 21451f3

Browse files
committed
Information about Gen2DFT DLL
1 parent 2f18444 commit 21451f3

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

Gen2DFTLib/readme.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
## Gen2DFTLib
2+
For ColorChord.NET, I developed a custom DFT algorithm ["Gen2DFT"](https://github.com/CaiB/ColorChord.NET/blob/master/ColorChord.NET/NoteFinder/Gen2NoteFinderDFT.cs) to replace the original ColorChord ["BaseDFT"](https://github.com/CaiB/ColorChord.NET/blob/master/ColorChord.NET/NoteFinder/BaseNoteFinderDFT.cs) one. My implementation provides cleaner output and has lower latency than a traditional DFT. It is able to accomplish this without requiring a window function, yet allows for a sliding window. It is also computationally efficient.
3+
4+
Other people have expressed interest in using the algorithm in their own projects, and as such I made it a standalone module that can be AOT compiled to a native DLL. This means that while it is written in C#, it is usable from any language that supports C-style DLL calls, and it also does not require .NET to be installed to use it.
5+
6+
I also co-authored a conference paper detailing the algorithm. If you use this algorithm in your research, we would highly appreciate a citation.
7+
TODO: Add citation info (paper under review)
8+
9+
[`Gen2DFT.cs`](https://github.com/CaiB/ColorChord.NET/blob/master/Gen2DFTLib/Gen2DFT.cs) in this directory is responsible for translating the C# API into one that is usable from unmanaged code. All of the functions exported in the DLL are defined and documented in this file. Documentation is also exported to an XML file included with the DLL download for use by your IDE to provide inline documentation.
10+
11+
Pre-built Gen2DFTLib DLLs are available, simply download the `Gen2DFTLib-*.zip` file from the latest [release on GitHub](https://github.com/CaiB/ColorChord.NET/releases).
12+
13+
Generally, the intended usage is as follows (example code written in C, use the equivalent in your language):
14+
```c
15+
// May be compiled with:
16+
// cl.exe Gen2DFTExample.c /FC /W3 /WX /Zi /link /opt:ref
17+
18+
// C clutter
19+
#define WIN32_LEAN_AND_MEAN
20+
#include <windows.h>
21+
#include <stdio.h>
22+
#include <assert.h>
23+
#include <stdlib.h>
24+
#include <malloc.h>
25+
#define _USE_MATH_DEFINES
26+
#include <math.h>
27+
28+
typedef INT32 (*Gen2DFT_Init_Func)(UINT32 octaveCount, UINT32 binsPerOctave, UINT32 sampleRate, FLOAT startFrequency, FLOAT loudnessCorrection);
29+
typedef UINT32 (*Gen2DFT_GetBinCount_Func)(void);
30+
typedef FLOAT* (*Gen2DFT_GetBinMagnitudes_Func)(void);
31+
typedef void (*Gen2DFT_AddAudioData_Func)(INT16* newData, UINT32 count);
32+
typedef void (*Gen2DFT_CalculateOutput_Func)(void);
33+
34+
void main(void)
35+
{
36+
// Start by determining if the system you are executing on supports AVX2
37+
// Anything other than C/C++ should have a much more pleasant built-in way to handle this
38+
BOOL SupportsAVX2 = FALSE;
39+
INT32 CPUInfo[4];
40+
__cpuid(CPUInfo, 0);
41+
if (CPUInfo[0] >= 7)
42+
{
43+
__cpuid(CPUInfo, 7);
44+
SupportsAVX2 = (CPUInfo[1] >> 5) & 1;
45+
}
46+
47+
// Load the DLL using whatever mechanism is appropriate for your language
48+
HMODULE Gen2DFTLibHandle = LoadLibraryW(SupportsAVX2 ? L".\\Gen2DFTLib_Win_x64_AVX2.dll" : L".\\Gen2DFTLib_Win_x64_Baseline.dll");
49+
assert(Gen2DFTLibHandle != NULL);
50+
51+
// Load function pointers from the DLL
52+
Gen2DFT_Init_Func Gen2DFT_Init = (Gen2DFT_Init_Func)GetProcAddress(Gen2DFTLibHandle, "Gen2DFT_Init");
53+
assert(Gen2DFT_Init != NULL);
54+
Gen2DFT_GetBinCount_Func Gen2DFT_GetBinCount = (Gen2DFT_GetBinCount_Func)GetProcAddress(Gen2DFTLibHandle, "Gen2DFT_GetBinCount");
55+
assert(Gen2DFT_GetBinCount != NULL);
56+
Gen2DFT_GetBinMagnitudes_Func Gen2DFT_GetBinMagnitudes = (Gen2DFT_GetBinMagnitudes_Func)GetProcAddress(Gen2DFTLibHandle, "Gen2DFT_GetBinMagnitudes");
57+
assert(Gen2DFT_GetBinMagnitudes != NULL);
58+
Gen2DFT_AddAudioData_Func Gen2DFT_AddAudioData = (Gen2DFT_AddAudioData_Func)GetProcAddress(Gen2DFTLibHandle, "Gen2DFT_AddAudioData");
59+
assert(Gen2DFT_AddAudioData != NULL);
60+
Gen2DFT_CalculateOutput_Func Gen2DFT_CalculateOutput = (Gen2DFT_CalculateOutput_Func)GetProcAddress(Gen2DFTLibHandle, "Gen2DFT_CalculateOutput");
61+
assert(Gen2DFT_CalculateOutput != NULL);
62+
63+
// Call the DLL's exported Gen2DFT_Init() function once
64+
const UINT32 OCTAVES = 3;
65+
const UINT32 BINS_PER_OCTAVE = 12;
66+
const UINT32 SAMPLE_RATE = 48000;
67+
INT32 InitResult = Gen2DFT_Init(OCTAVES, BINS_PER_OCTAVE, SAMPLE_RATE, 55.0, 0.0);
68+
assert(InitResult >= 0);
69+
70+
// Get information about the output by calling other exported functions once
71+
UINT32 BinCount = Gen2DFT_GetBinCount();
72+
assert(BinCount == OCTAVES * BINS_PER_OCTAVE);
73+
FLOAT* OutputData = Gen2DFT_GetBinMagnitudes();
74+
75+
// The rest of this code is expected to run in some sort of loop to process data
76+
// Generate and input sine wave to the algorithm as an example
77+
const INT32 INPUT_COUNT = 4800;
78+
INT16* InputData = malloc(sizeof(INT16) * INPUT_COUNT);
79+
for (INT32 i = 0; i < INPUT_COUNT; i++) { InputData[i] = (INT16)(sinf(155.56F * (FLOAT)M_PI * 2.0F * i / SAMPLE_RATE) * 32000.0F); }
80+
Gen2DFT_AddAudioData(InputData, INPUT_COUNT);
81+
82+
// After you've added some audio, calculate and display the output data
83+
Gen2DFT_CalculateOutput();
84+
for (UINT32 o = 0; o < BinCount / BINS_PER_OCTAVE; o++)
85+
{
86+
for (UINT32 b = 0; b < BINS_PER_OCTAVE; b++) { printf("%.2f ", OutputData[(o * BINS_PER_OCTAVE) + b]); }
87+
printf("\n");
88+
}
89+
}
90+
```
91+
92+
The above example code outputs:
93+
```
94+
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
95+
0.00 0.00 0.00 0.00 0.00 0.58 1.07 0.62 0.00 0.00 0.00 0.00
96+
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
97+
```

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
## See the [ColorChord.NET Website](https://www.colorchord.net) for documentation/install instructions.
66

7-
**[Click here for downloads](https://github.com/CaiB/ColorChord.NET/releases)**
7+
- **[Click here for downloads](https://github.com/CaiB/ColorChord.NET/releases)**
8+
- **[Information about the standalone Gen2DFT library usable from other software](https://github.com/CaiB/ColorChord.NET/tree/master/Gen2DFTLib/readme.md)**
89

910
### Info
1011

0 commit comments

Comments
 (0)