Skip to content

Commit 1f467bf

Browse files
committed
better spectrum view + FFT settings
1 parent e2ed4af commit 1f467bf

13 files changed

Lines changed: 325 additions & 95 deletions

File tree

include/Kaixo/SpectralRotator/Gui/MainView.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@ namespace Kaixo::Gui {
2525

2626
MainView(Context c);
2727

28+
// ------------------------------------------------
29+
30+
void resized() override;
31+
2832
// ------------------------------------------------
2933

3034
Processing::InterfaceStorage<Processing::FileInterface> inputFileInterface;
3135
Processing::InterfaceStorage<Processing::FileInterface> rotatedFileInterface;
3236

3337
// ------------------------------------------------
38+
39+
private:
40+
constexpr static std::size_t DefaultUI = 1;
41+
constexpr static std::size_t SettingsOnTheSideUI = 2;
42+
43+
std::size_t m_UIType = 0;
44+
45+
std::function<void(void)> m_ResizedCallback;
46+
47+
// ------------------------------------------------
3448

3549
};
3650

include/Kaixo/SpectralRotator/Gui/SettingsView.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ namespace Kaixo::Gui {
6464

6565
struct Settings {
6666

67+
// ------------------------------------------------
68+
69+
std::function<void(int)> fftSizeChanged;
70+
std::function<void(int)> fftResolutionChanged;
71+
std::function<void(float)> fftDbDepthChanged;
72+
73+
// ------------------------------------------------
74+
6775
} settings;
6876

6977
// ------------------------------------------------

include/Kaixo/SpectralRotator/Gui/SpectralFileViewer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ namespace Kaixo::Gui {
5555

5656
// ------------------------------------------------
5757

58+
SpectralViewer& spectralViewer() { return *m_SpectralViewer; }
59+
60+
// ------------------------------------------------
61+
5862
private:
5963
SpectralViewer* m_SpectralViewer;
6064

include/Kaixo/SpectralRotator/Gui/SpectralViewer.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,34 @@ namespace Kaixo::Gui {
6969

7070
// ------------------------------------------------
7171

72+
void fftSize(std::size_t size);
73+
void fftResolution(std::size_t range);
74+
void fftRange(float range);
75+
76+
// ------------------------------------------------
77+
7278
private:
7379
mutable std::mutex m_AnalyzeResultMutex{};
7480
juce::Image m_Image = juce::Image(juce::Image::PixelFormat::ARGB, 512, 256, true);
7581
juce::Image m_Generated = juce::Image(juce::Image::PixelFormat::ARGB, 512, 256, true);
7682

83+
std::size_t m_FFTSize = 2048;
84+
std::size_t m_FFTResolution = 2048;
85+
float m_FFTRange = 48;
7786
std::size_t m_AnalyzingProgress = 0;
7887
std::size_t m_AnalyzingProgressTotal = 0;
7988
bool m_ShowingProgress = false;
8089
bool m_DidResize = false;
8190
std::atomic_bool m_NewImageReady = false;
8291
std::atomic_bool m_TryingToAssignNewImage = false;
92+
std::atomic_bool m_CausedByResize = false;
8393
std::atomic_bool m_GeneratingImage = false;
94+
std::atomic_bool m_ShouldAnalyze = false;
8495
std::atomic_bool m_FileWillProbablyChange = false;
8596
Processing::AudioBufferSpectralInformation m_AnalyzeResult;
8697
float m_PlayPosition = 0;
8798
Theme::Drawable m_Loading = T.loading;
88-
std::thread m_GeneratingThread{};
99+
cxxpool::thread_pool m_GeneratingThreadPool{ 1 };
89100

90101
std::chrono::steady_clock::time_point m_LastResize;
91102

include/Kaixo/SpectralRotator/Processing/AudioBufferSpectralInformation.hpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,28 @@ namespace Kaixo::Processing {
2121

2222
// ------------------------------------------------
2323

24-
struct FourierFrame {
24+
std::size_t frameSize = 1024;
25+
std::vector<float> intensity{};
2526

26-
// ------------------------------------------------
27-
28-
std::vector<float> intensity{};
29-
30-
// ------------------------------------------------
31-
32-
float intensityAt(float x, float dx);
33-
34-
// ------------------------------------------------
27+
// ------------------------------------------------
28+
29+
float get(std::size_t x, std::size_t y);
3530

36-
};
31+
std::size_t frames() { return intensity.size() / frameSize; }
3732

3833
// ------------------------------------------------
3934

40-
std::vector<FourierFrame> frames{};
35+
float intensityAtY(std::size_t x, float y, float dy);
4136

4237
// ------------------------------------------------
4338

44-
float intensityAt(float x, float y, float dy);
39+
float intensityAt(float x, float dx, float y, float dy);
4540

4641
// ------------------------------------------------
4742

48-
static AudioBufferSpectralInformation analyze(const Processing::AudioBuffer& buffer, std::size_t fftSize, std::size_t horizontalResolution);
43+
static AudioBufferSpectralInformation analyze(
44+
const Processing::AudioBuffer& buffer, std::size_t fftSize,
45+
std::size_t horizontalResolution, std::size_t* progress);
4946

5047
// ------------------------------------------------
5148

include/Kaixo/SpectralRotator/Processing/Interfaces.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace Kaixo::Processing {
4242
float position();
4343

4444
std::filesystem::path path();
45-
AudioBufferSpectralInformation analyzeBuffer(std::size_t fftSize, std::size_t horizontalResolution);
45+
AudioBufferSpectralInformation analyzeBuffer(std::size_t fftSize, std::size_t horizontalResolution, std::size_t* progress = nullptr);
4646

4747
// ------------------------------------------------
4848

source/Kaixo/SpectralRotator/Gui/MainView.cpp

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ namespace Kaixo::Gui {
3737

3838
// ------------------------------------------------
3939

40-
auto& rotated = add<SpectralFileViewer>({ 32, 8 + (Height - 12) / 2, Width - 36, (Height - 12) / 2 }, {
40+
auto& rotated = add<SpectralFileViewer>({
4141
.rotatable = true,
4242
.background = T.rotateBackground,
4343
.file = rotatedFileInterface,
4444
});
4545

46-
add<SpectralFileViewer>({ 32, 4, Width - 36, (Height - 12) / 2 }, {
46+
auto& input = add<SpectralFileViewer>({
4747
.rotatable = false,
4848
.background = T.sourceBackground,
4949
.file = inputFileInterface,
@@ -52,20 +52,73 @@ namespace Kaixo::Gui {
5252

5353
// ------------------------------------------------
5454

55-
auto& settings = add<SettingsView>({ 32, 4, Width - 36, Height - 8 }, {});
55+
auto& settings = add<SettingsView>({
56+
.fftSizeChanged = [&](int fftSize) {
57+
input.spectralViewer().fftSize(fftSize);
58+
rotated.spectralViewer().fftSize(fftSize);
59+
},
60+
.fftResolutionChanged = [&](float value) {
61+
input.spectralViewer().fftResolution(value);
62+
rotated.spectralViewer().fftResolution(value);
63+
},
64+
.fftDbDepthChanged = [&](float value) {
65+
input.spectralViewer().fftRange(value);
66+
rotated.spectralViewer().fftRange(value);
67+
}
68+
});
5669
settings.setVisible(false);
5770

5871
// ------------------------------------------------
5972

6073
add<Button>({ 4, 265, 20, 20 }, {
6174
.callback = [&](bool) {
6275
settings.setVisible(!settings.isVisible());
76+
m_ResizedCallback();
6377
},
6478
.graphics = T.settings.button
6579
});
6680

6781
// ------------------------------------------------
82+
83+
m_ResizedCallback = [&] {
84+
std::size_t newUIType = 0;
85+
if (settings.isVisible()) {
86+
if (width() < 600) newUIType = DefaultUI;
87+
else newUIType = SettingsOnTheSideUI;
88+
} else newUIType = DefaultUI;
89+
90+
if (newUIType == m_UIType) return;
91+
92+
m_UIType = newUIType;
93+
94+
switch (m_UIType) {
95+
case DefaultUI: {
96+
rotated.dimensions(UnevaluatedRect{ 32, 8 + (Height - 12) / 2, Width - 36, (Height - 12) / 2 });
97+
input.dimensions(UnevaluatedRect{ 32, 4, Width - 36, (Height - 12) / 2 });
98+
settings.dimensions(UnevaluatedRect{ 32, 4, Width - 36, Height - 8 });
99+
break;
100+
}
101+
case SettingsOnTheSideUI: {
102+
rotated.dimensions(UnevaluatedRect{ 32, 8 + (Height - 12) / 2, Width - 340, (Height - 12) / 2 });
103+
input.dimensions(UnevaluatedRect{ 32, 4, Width - 340, (Height - 12) / 2 });
104+
settings.dimensions(UnevaluatedRect{ Width - 304, 4, 300, Height - 8 });
105+
break;
106+
}
107+
}
108+
109+
rotated.updateDimensions();
110+
input.updateDimensions();
111+
settings.updateDimensions();
112+
};
68113

114+
// ------------------------------------------------
115+
116+
}
117+
118+
// ------------------------------------------------
119+
120+
void MainView::resized() {
121+
m_ResizedCallback();
69122
}
70123

71124
// ------------------------------------------------

source/Kaixo/SpectralRotator/Gui/SettingsView.cpp

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace Kaixo::Gui {
3434
.graphics = g,
3535
.bounds = localDimensions(),
3636
.text = {
37-
{ "$setting-name", settings.name },
38-
{ "$setting-value", settings.value }
37+
{ "$name", settings.name },
38+
{ "$value", settings.value }
3939
},
4040
.state = state()
4141
});
@@ -61,15 +61,65 @@ namespace Kaixo::Gui {
6161

6262
// ------------------------------------------------
6363

64+
std::size_t y = 0;
6465
//Entry& theme = add<Entry>({ 4, 32 + 0 * 24, Width - 8, 22 }, {
6566
// .name = "Theme",
6667
// .value = std::string(T.name()),
6768
// .click = [&theme] {
6869
// // Open theme
6970
// }
7071
//});
71-
72-
add<Entry>({ 4, 32 + 0 * 24, Width - 8, 22 }, {
72+
73+
add<Knob>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
74+
.onchange = [&](ParamValue val) {
75+
constexpr int fftSizes[]{ 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
76+
int index = Math::clamp(normalToIndex(val, 9), 0, 8);
77+
Storage::set<int>("fft-size", index);
78+
settings.fftSizeChanged(fftSizes[index]);
79+
},
80+
.graphics = T.settings.entry,
81+
.tooltipName = false,
82+
.tooltipValue = false,
83+
.name = "FFT Size",
84+
.steps = 9,
85+
.format = Formatters::Group<"32", "64", "128", "256", "512", "1024", "2048", "4096", "8192">,
86+
.transform = Transformers::Group<9>,
87+
.resetValue = 6.f / 8.f
88+
}).value(Math::clamp(Storage::getOrDefault<int>("fft-size", 6), 0, 8) / 8.f);
89+
90+
add<Knob>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
91+
.onchange = [&](ParamValue val) {
92+
constexpr int fftSizes[]{ 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
93+
int index = Math::clamp(normalToIndex(val, 9), 0, 8);
94+
Storage::set<int>("fft-resolution", index);
95+
settings.fftResolutionChanged(fftSizes[index]);
96+
},
97+
.graphics = T.settings.entry,
98+
.tooltipName = false,
99+
.tooltipValue = false,
100+
.name = "FFT Resolution",
101+
.steps = 9,
102+
.format = Formatters::Group<"32", "64", "128", "256", "512", "1024", "2048", "4096", "8192">,
103+
.transform = Transformers::Group<9>,
104+
.resetValue = 6.f / 8.f
105+
}).value(Math::clamp(Storage::getOrDefault<int>("fft-resolution", 6), 0, 8) / 8.f);
106+
107+
add<Knob>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
108+
.onchange = [&](ParamValue val) {
109+
auto range = Transformers::Range<48.f, 144.f>.transform(val);
110+
Storage::set<float>("fft-range", range);
111+
settings.fftDbDepthChanged(range);
112+
},
113+
.graphics = T.settings.entry,
114+
.tooltipName = false,
115+
.tooltipValue = false,
116+
.name = "FFT Range",
117+
.format = Formatters::Decibels,
118+
.transform = Transformers::Range<48.f, 144.f>,
119+
.resetValue = Transformers::Range<48.f, 144.f>.normalize(100.f)
120+
}).value(Transformers::Range<48.f, 144.f>.normalize(Storage::getOrDefault<float>("fft-range", 100.f)));
121+
122+
add<Entry>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
73123
.name = "Output Folder",
74124
.value = AudioFile::generationLocation().string(),
75125
.click = [] {
@@ -78,11 +128,11 @@ namespace Kaixo::Gui {
78128
}
79129
});
80130

81-
add<Entry>({ 4, 32 + 1 * 24, Width - 8, 22 }, {
131+
add<Entry>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
82132
.name = "Version",
83133
.value = SYNTH_FullVersion,
84134
});
85-
135+
86136
// ------------------------------------------------
87137

88138
}

0 commit comments

Comments
 (0)