Swadge ESP32-S2
APIs to develop for the Magfest Swadge
Toggle main menu visibility
Loading...
Searching...
No Matches
DFT32.h
Go to the documentation of this file.
1
// Copyright 2015 <>< Charles Lohr under the ColorChord License.
2
3
#ifndef _DFT32_H
4
#define _DFT32_H
5
6
#include <stdint.h>
7
#include "
ccconfig.h
"
8
9
// A 32-bit version of the DFT used for ColorChord.
10
// This header makes it convenient to use for an embedded system.
11
// The 32-bit DFT avoids some bit shifts, however it uses slightly
12
// more RAM and it uses a lot of 32-bit arithmetic.
13
//
14
// This is basically a clone of "ProgressiveIntegerSkippy" and changes
15
// made here should be back-ported there as well.
16
17
// You can # define these to be other things elsewhere.
18
19
// Will used simple approximation of norm rather than
20
// sum squares and approx sqrt
21
#ifndef APPROX_NORM
22
#define APPROX_NORM 1
23
#endif
24
25
#ifndef OCTAVES
26
#define OCTAVES 5
27
#endif
28
29
#ifndef FIX_B_PER_O
30
#define FIX_B_PER_O 24
31
#endif
32
33
// Don't configure this.
34
#define FIX_BINS (FIX_B_PER_O * OCTAVES)
35
#define BIN_CYCLE (1 << OCTAVES)
36
37
// You may increase this past 5 but if you do, the amplitude of your incoming
38
// signal must decrease. Increasing this value makes responses slower. Lower
39
// values are more responsive.
40
#ifndef DFT_IIR
41
#define DFT_IIR 6
42
#endif
43
44
// Everything the integer one buys, except it only calculates 2 octaves worth of
45
// notes per audio frame.
46
// This is sort of working, but still have some quality issues.
47
// It would theoretically be fast enough to work on an AVR.
48
// NOTE: This is the only DFT available to the embedded port of ColorChord
49
#ifndef CC_EMBEDDED
50
void
DoDFTProgressive32(
dft32_data
* dd,
float
* outBins,
float
* frequencies,
int
bins,
const
float
* dataBuffer,
51
int
place_in_data_buffer,
int
size_of_data_buffer,
float
q,
float
speedup);
52
#endif
53
54
typedef
struct
55
{
56
// Whenever you need to read the bins, you can do it from here.
57
// These outputs are limited to 0..~2047, this makes it possible
58
// for you to process with uint16_t's more easily.
59
// This is updated every time the DFT hits the octave count, or 1/32 updates.
60
uint16_t
embeddedBins32
[
FIX_BINS
];
61
62
// NOTES to self:
63
//
64
// Let's say we want to try this on an AVR.
65
// 24 bins, 5 octaves = 120 bins.
66
// 20 MHz clock / 4.8k sps = 4096 IPS = 34 clocks per bin = :(
67
// We can do two at the same time, this frees us up some
68
uint8_t
sDoneFirstRun
;
69
70
// (advances,places) full revolution is 256. 8bits integer part 8bit fractional
71
uint16_t
sDatSpace32A
[
FIX_BINS
* 2];
72
// (isses,icses)
73
int32_t
sDatSpace32B
[
FIX_BINS
* 2];
74
75
// This is updated every time the DFT hits the octave count, or 1 out of
76
// (1<<OCTAVES) times which is (1<<(OCTAVES-1)) samples
77
// (isses,icses)
78
int32_t
sDatSpace32BOut
[
FIX_BINS
* 2];
79
80
// Sdo_this_octave is a scheduling state for the running SIN/COS states for
81
// each bin. We have to execute the highest octave every time, however, we can
82
// get away with updating the next octave down every-other-time, then the next
83
// one down yet, every-other-time from that one. That way, no matter how many
84
// octaves we have, we only need to update FIX_B_PER_O*2 DFT bins.
85
uint8_t
Sdo_this_octave
[
BIN_CYCLE
];
86
87
int32_t
sAccum_octave_bins
[
OCTAVES
];
88
uint8_t
sWhichOctavePlace
;
89
90
uint16_t
embeddedBins
[
FIX_BINS
];
91
}
dft32_data
;
92
93
// It's actually split into a few functions, which you can call on your own:
94
int
SetupDFTProgressive32
(
dft32_data
* dd);
// Call at start. Returns nonzero if error.
95
void
UpdateBins32
(
dft32_data
* dd,
const
uint16_t* frequencies);
96
97
// Call this to push on new frames of sound.
98
// Though it accepts an int16, it actually only takes -4095 to +4095. (13-bit)
99
// Any more and you will exceed the accumulators and it will cause an overflow.
100
void
PushSample32
(
dft32_data
* dd, int16_t dat);
101
102
#ifndef CC_EMBEDDED
103
// ColorChord regular uses this to pass in floats.
104
void
UpdateBinsForDFT32(
dft32_data
* dd,
const
float
* frequencies);
// Update the frequencies
105
#endif
106
107
// This takes the current sin/cos state of ColorChord and output to
108
// embeddedBins32.
109
void
UpdateOutputBins32
(
dft32_data
* dd);
110
111
#endif
dft32_data::Sdo_this_octave
uint8_t Sdo_this_octave[BIN_CYCLE]
Definition
DFT32.h:85
BIN_CYCLE
#define BIN_CYCLE
Definition
DFT32.h:35
OCTAVES
#define OCTAVES
Definition
DFT32.h:26
UpdateOutputBins32
void UpdateOutputBins32(dft32_data *dd)
TODO.
Definition
DFT32.c:135
SetupDFTProgressive32
int SetupDFTProgressive32(dft32_data *dd)
TODO.
Definition
DFT32.c:249
dft32_data::sDoneFirstRun
uint8_t sDoneFirstRun
Definition
DFT32.h:68
dft32_data::sWhichOctavePlace
uint8_t sWhichOctavePlace
Definition
DFT32.h:88
dft32_data::sDatSpace32B
int32_t sDatSpace32B[FIX_BINS *2]
Definition
DFT32.h:73
FIX_BINS
#define FIX_BINS
Definition
DFT32.h:34
dft32_data::sAccum_octave_bins
int32_t sAccum_octave_bins[OCTAVES]
Definition
DFT32.h:87
dft32_data::embeddedBins
uint16_t embeddedBins[FIX_BINS]
Definition
DFT32.h:90
dft32_data::sDatSpace32A
uint16_t sDatSpace32A[FIX_BINS *2]
Definition
DFT32.h:71
UpdateBins32
void UpdateBins32(dft32_data *dd, const uint16_t *frequencies)
TODO.
Definition
DFT32.c:290
PushSample32
void PushSample32(dft32_data *dd, int16_t dat)
TODO.
Definition
DFT32.c:311
dft32_data::sDatSpace32BOut
int32_t sDatSpace32BOut[FIX_BINS *2]
Definition
DFT32.h:78
dft32_data::embeddedBins32
uint16_t embeddedBins32[FIX_BINS]
Definition
DFT32.h:60
dft32_data
Definition
DFT32.h:55
ccconfig.h
main
utils
colorchord
DFT32.h
Generated by
1.17.0