Skip to content

Commit df7b2b8

Browse files
committed
change generation folder
1 parent 1f467bf commit df7b2b8

13 files changed

Lines changed: 113 additions & 40 deletions

File tree

include/Kaixo/SpectralRotator/Gui/SettingsView.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Kaixo::Gui {
3434

3535
// ------------------------------------------------
3636

37-
std::function<void(void)> click;
37+
std::function<void(std::string&)> click;
3838

3939
// ------------------------------------------------
4040

@@ -57,6 +57,10 @@ namespace Kaixo::Gui {
5757
void paint(juce::Graphics& g) override;
5858

5959
// ------------------------------------------------
60+
61+
void changeValue(std::string_view val) { settings.value = val; repaint(); }
62+
63+
// ------------------------------------------------
6064

6165
};
6266

@@ -68,6 +72,7 @@ namespace Kaixo::Gui {
6872

6973
std::function<void(int)> fftSizeChanged;
7074
std::function<void(int)> fftResolutionChanged;
75+
std::function<void(int)> fftBlockSizeChanged;
7176
std::function<void(float)> fftDbDepthChanged;
7277

7378
// ------------------------------------------------
@@ -79,6 +84,10 @@ namespace Kaixo::Gui {
7984
SettingsView(Context c, Settings s);
8085

8186
// ------------------------------------------------
87+
88+
std::unique_ptr<FileChooser> generationDirectoryChooser{};
89+
90+
// ------------------------------------------------
8291

8392
};
8493

include/Kaixo/SpectralRotator/Gui/SpectralViewer.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace Kaixo::Gui {
7171

7272
void fftSize(std::size_t size);
7373
void fftResolution(std::size_t range);
74+
void fftBlockSize(std::size_t ms);
7475
void fftRange(float range);
7576

7677
// ------------------------------------------------
@@ -82,15 +83,16 @@ namespace Kaixo::Gui {
8283

8384
std::size_t m_FFTSize = 2048;
8485
std::size_t m_FFTResolution = 2048;
86+
std::size_t m_FFTBlockSize = 50;
8587
float m_FFTRange = 48;
8688
std::size_t m_AnalyzingProgress = 0;
8789
std::size_t m_AnalyzingProgressTotal = 0;
8890
bool m_ShowingProgress = false;
8991
bool m_DidResize = false;
9092
std::atomic_bool m_NewImageReady = false;
9193
std::atomic_bool m_TryingToAssignNewImage = false;
92-
std::atomic_bool m_CausedByResize = false;
9394
std::atomic_bool m_GeneratingImage = false;
95+
std::atomic_bool m_CausedByResize = false;
9496
std::atomic_bool m_ShouldAnalyze = false;
9597
std::atomic_bool m_FileWillProbablyChange = false;
9698
Processing::AudioBufferSpectralInformation m_AnalyzeResult;

include/Kaixo/SpectralRotator/Processing/AudioBufferSpectralInformation.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace Kaixo::Processing {
4242

4343
static AudioBufferSpectralInformation analyze(
4444
const Processing::AudioBuffer& buffer, std::size_t fftSize,
45-
std::size_t horizontalResolution, std::size_t* progress);
45+
std::size_t horizontalResolution, std::size_t blockSize, std::size_t* progress);
4646

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

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, std::size_t* progress = nullptr);
45+
AudioBufferSpectralInformation analyzeBuffer(std::size_t fftSize, std::size_t horizontalResolution, std::size_t blockSize, std::size_t* progress = nullptr);
4646

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

source/Kaixo/SpectralRotator/Gui/MainView.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ namespace Kaixo::Gui {
5757
input.spectralViewer().fftSize(fftSize);
5858
rotated.spectralViewer().fftSize(fftSize);
5959
},
60-
.fftResolutionChanged = [&](float value) {
60+
.fftResolutionChanged = [&](int value) {
6161
input.spectralViewer().fftResolution(value);
6262
rotated.spectralViewer().fftResolution(value);
63+
},
64+
.fftBlockSizeChanged = [&](int value) {
65+
input.spectralViewer().fftBlockSize(value);
66+
rotated.spectralViewer().fftBlockSize(value);
6367
},
6468
.fftDbDepthChanged = [&](float value) {
6569
input.spectralViewer().fftRange(value);

source/Kaixo/SpectralRotator/Gui/SettingsView.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "Kaixo/Core/Gui/Views/ImageView.hpp"
99
#include "Kaixo/Core/Gui/Knob.hpp"
10+
#include "Kaixo/Core/Gui/Button.hpp"
1011

1112
// ------------------------------------------------
1213

@@ -26,7 +27,7 @@ namespace Kaixo::Gui {
2627

2728
void SettingsView::Entry::mouseDown(const juce::MouseEvent& event) {
2829
View::mouseDown(event);
29-
if (settings.click) settings.click();
30+
if (settings.click) settings.click(settings.value);
3031
}
3132

3233
void SettingsView::Entry::paint(juce::Graphics& g) {
@@ -84,7 +85,7 @@ namespace Kaixo::Gui {
8485
.steps = 9,
8586
.format = Formatters::Group<"32", "64", "128", "256", "512", "1024", "2048", "4096", "8192">,
8687
.transform = Transformers::Group<9>,
87-
.resetValue = 6.f / 8.f
88+
.resetValue = 5.f / 8.f
8889
}).value(Math::clamp(Storage::getOrDefault<int>("fft-size", 6), 0, 8) / 8.f);
8990

9091
add<Knob>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
@@ -101,9 +102,26 @@ namespace Kaixo::Gui {
101102
.steps = 9,
102103
.format = Formatters::Group<"32", "64", "128", "256", "512", "1024", "2048", "4096", "8192">,
103104
.transform = Transformers::Group<9>,
104-
.resetValue = 6.f / 8.f
105+
.resetValue = 5.f / 8.f
105106
}).value(Math::clamp(Storage::getOrDefault<int>("fft-resolution", 6), 0, 8) / 8.f);
106107

108+
add<Knob>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
109+
.onchange = [&](ParamValue val) {
110+
constexpr int fftSizes[]{ 5, 10, 25, 50, 75, 100 };
111+
int index = Math::clamp(normalToIndex(val, 6), 0, 5);
112+
Storage::set<int>("fft-block-size", index);
113+
settings.fftBlockSizeChanged(fftSizes[index]);
114+
},
115+
.graphics = T.settings.entry,
116+
.tooltipName = false,
117+
.tooltipValue = false,
118+
.name = "FFT Block Size",
119+
.steps = 6,
120+
.format = Formatters::Group<"5 ms", "10 ms", "25 ms", "50 ms", "75 ms", "100 ms">,
121+
.transform = Transformers::Group<6>,
122+
.resetValue = 2.f / 5.f
123+
}).value(Math::clamp(Storage::getOrDefault<int>("fft-block-size", 6), 0, 8) / 8.f);
124+
107125
add<Knob>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
108126
.onchange = [&](ParamValue val) {
109127
auto range = Transformers::Range<48.f, 144.f>.transform(val);
@@ -118,16 +136,31 @@ namespace Kaixo::Gui {
118136
.transform = Transformers::Range<48.f, 144.f>,
119137
.resetValue = Transformers::Range<48.f, 144.f>.normalize(100.f)
120138
}).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 }, {
139+
140+
add<Button>({ Width - 24, 32 + y * 24, 20, 20 }, {
141+
.callback = [](bool) {
142+
File a{ AudioFile::generationLocation().string() };
143+
a.startAsProcess();
144+
},
145+
.graphics = T.settings.openFolder
146+
});
147+
148+
Entry& outputFolder = add<Entry>({ 4, 32 + (y++) * 24, Width - 32, 22 }, {
123149
.name = "Output Folder",
124150
.value = AudioFile::generationLocation().string(),
125-
.click = [] {
126-
File a{ AudioFile::generationLocation().string() };
127-
a.revealToUser();
151+
.click = [&](std::string& value) {
152+
auto folderChooserFlags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectDirectories;
153+
generationDirectoryChooser = std::make_unique<FileChooser>("Select directory", File{ AudioFile::generationLocation().string() });
154+
generationDirectoryChooser->launchAsync(folderChooserFlags, [&](const FileChooser&) {
155+
auto file = generationDirectoryChooser->getResult();
156+
if (!file.exists()) return; // not real file
157+
std::string fullPath = file.getFullPathName().toStdString();
158+
Storage::set<std::string>("generation-directory", fullPath);
159+
value = fullPath;
160+
});
128161
}
129162
});
130-
163+
131164
add<Entry>({ 4, 32 + (y++) * 24, Width - 8, 22 }, {
132165
.name = "Version",
133166
.value = SYNTH_FullVersion,

source/Kaixo/SpectralRotator/Gui/SpectralViewer.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ namespace Kaixo::Gui {
3030
constexpr int fftSizes[]{ 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
3131
m_FFTResolution = fftSizes[Math::clamp(value.value(), 0, 8)];
3232
}
33+
34+
if (auto value = Storage::get<int>("fft-block-size")) {
35+
constexpr int fftSizes[]{ 5, 10, 25, 50, 75, 100 };
36+
m_FFTBlockSize = fftSizes[Math::clamp(value.value(), 0, 5)];
37+
}
3338

3439
if (auto value = Storage::get<float>("fft-range")) {
3540
m_FFTRange = value.value();
@@ -88,27 +93,31 @@ namespace Kaixo::Gui {
8893
m_TryingToAssignNewImage = false;
8994
}
9095

96+
g.setImageResamplingQuality(juce::Graphics::highResamplingQuality);
97+
g.drawImage(m_Image, localDimensions().toFloat());
98+
99+
float x = m_PlayPosition * (width() - 3);
100+
g.setColour({ 210, 210, 210 });
101+
g.fillRect(Rect{ x, 0, 3, height() });
102+
91103
m_ShowingProgress = false;
92104
if (settings.file->modifyingFile() || (!m_CausedByResize && m_GeneratingImage) || m_FileWillProbablyChange) {
93105
m_ShowingProgress = true;
106+
bool rotating = settings.file->modifyingFile();
94107
float fileLoadingProgress = settings.file->loadingProgress();
95108
float analyzingProgress = static_cast<float>(m_AnalyzingProgress) / m_AnalyzingProgressTotal;
96-
float progress = m_FileWillProbablyChange
97-
? fileLoadingProgress * 0.5 + 0.5 * analyzingProgress // If file also changed, add to progress bar
98-
: analyzingProgress; // Otherwise just analyzing progress
109+
float progress = rotating
110+
? fileLoadingProgress // If file also changed, add to progress bar
111+
: analyzingProgress; // Otherwise just analyzing progress
99112

100113
m_Loading.draw({
101114
.graphics = g,
102115
.bounds = localDimensions(),
103-
.text = { { "$progress", std::format("%{:.0f}", progress * 100) }}
116+
.text = {
117+
{ "$progress", std::format("%{:.0f}", progress * 100) },
118+
{ "$load-type", rotating ? "Rotating" : "Analyzing" }
119+
}
104120
});
105-
} else {
106-
g.setImageResamplingQuality(juce::Graphics::highResamplingQuality);
107-
g.drawImage(m_Image, localDimensions().toFloat());
108-
109-
float x = m_PlayPosition * (width() - 3);
110-
g.setColour({ 210, 210, 210 });
111-
g.fillRect(Rect{ x, 0, 3, height() });
112121
}
113122
}
114123

@@ -153,10 +162,9 @@ namespace Kaixo::Gui {
153162

154163
if (m_ShouldAnalyze) {
155164
m_ShouldAnalyze = false;
156-
m_FileWillProbablyChange = false; // We're reanalyzing now
157165
m_AnalyzingProgress = 0;
158166
m_AnalyzingProgressTotal = width() * height() + Processing::Fft{}.estimateSteps(m_FFTSize, false) * m_FFTResolution;
159-
m_AnalyzeResult = settings.file->analyzeBuffer(m_FFTSize, m_FFTResolution, &m_AnalyzingProgress);
167+
m_AnalyzeResult = settings.file->analyzeBuffer(m_FFTSize, m_FFTResolution, m_FFTBlockSize , &m_AnalyzingProgress);
160168
} else {
161169
m_AnalyzingProgress = 0;
162170
m_AnalyzingProgressTotal = width() * height();
@@ -186,6 +194,7 @@ namespace Kaixo::Gui {
186194
}
187195
}
188196

197+
m_FileWillProbablyChange = false; // We're reanalyzing now
189198
m_GeneratingImage = false;
190199
m_NewImageReady = true;
191200
});
@@ -205,6 +214,12 @@ namespace Kaixo::Gui {
205214
reGenerateImage(true);
206215
}
207216

217+
void SpectralViewer::fftBlockSize(std::size_t ms) {
218+
if (ms == m_FFTBlockSize) return;
219+
m_FFTBlockSize = ms;
220+
reGenerateImage(true);
221+
}
222+
208223
void SpectralViewer::fftRange(float range) {
209224
if (range == m_FFTRange) return;
210225
m_FFTRange = range;

source/Kaixo/SpectralRotator/Processing/AudioBufferSpectralInformation.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ namespace Kaixo::Processing {
8383

8484
AudioBufferSpectralInformation AudioBufferSpectralInformation::analyze(
8585
const Processing::AudioBuffer& buffer, std::size_t fftSize,
86-
std::size_t horizontalResolution, std::size_t* progress)
86+
std::size_t horizontalResolution, std::size_t bSizeMs, std::size_t* progress)
8787
{
8888
if (buffer.empty()) return {};
8989

90-
std::size_t stepSize = Math::max(1, buffer.size() / horizontalResolution);
9190
std::size_t size = buffer.size();
92-
std::size_t blockSize = Math::min(fftSize, buffer.sampleRate * 0.05); // 50 millis
91+
std::size_t blockSize = Math::min(fftSize, buffer.sampleRate * (bSizeMs / 1000.f));
9392

9493
auto get = [&](std::size_t index) -> AudioFrame {
9594
if (index < buffer.size()) return buffer[index];
@@ -104,7 +103,7 @@ namespace Kaixo::Processing {
104103
Fft fft;
105104
fft.stepRef = progress;
106105
for (std::size_t x = 0; x < horizontalResolution; ++x) {
107-
std::size_t i = x * stepSize;
106+
std::size_t i = x * (static_cast<float>(buffer.size()) / horizontalResolution);
108107

109108
std::memset(fftBuffer.data(), 0, fftSize * sizeof(std::complex<float>));
110109
float scale = 0;
@@ -122,7 +121,7 @@ namespace Kaixo::Processing {
122121
float magnitude = std::abs(fftBuffer[y]);
123122
float normalizedMagnitude = (2 * magnitude) / scale; // Apply proper scaling
124123
result.intensity[index] = Math::Fast::magnitude_to_db(normalizedMagnitude);
125-
if (result.intensity[index] == -INFINITY) result.intensity[index] = -145;
124+
if (result.intensity[index] < -145) result.intensity[index] = -145;
126125
}
127126
}
128127

source/Kaixo/SpectralRotator/Processing/Interfaces.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,20 @@ namespace Kaixo::Processing {
118118
return {};
119119
}
120120

121-
AudioBufferSpectralInformation FileInterface::analyzeBuffer(std::size_t fftSize, std::size_t horizontalResolution, std::size_t* progress) {
121+
AudioBufferSpectralInformation FileInterface::analyzeBuffer(
122+
std::size_t fftSize, std::size_t horizontalResolution, std::size_t blockSize, std::size_t* progress)
123+
{
122124
auto& processor = self<SpectralRotatorProcessor>();
123125

124126
switch (settings.index) {
125127
case 0: {
126128
std::lock_guard lock{ processor.inputFile.fileMutex };
127-
return AudioBufferSpectralInformation::analyze(processor.inputFile.file.buffer, fftSize, horizontalResolution, progress);
129+
return AudioBufferSpectralInformation::analyze(processor.inputFile.file.buffer, fftSize, horizontalResolution, blockSize, progress);
128130
break;
129131
}
130132
case 1: {
131133
std::lock_guard lock{ processor.rotatedFile.fileMutex };
132-
return AudioBufferSpectralInformation::analyze(processor.rotatedFile.file.buffer, fftSize, horizontalResolution, progress);
134+
return AudioBufferSpectralInformation::analyze(processor.rotatedFile.file.buffer, fftSize, horizontalResolution, blockSize, progress);
133135
break;
134136
}
135137
}

source/Kaixo/Utils/AudioFile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
// ------------------------------------------------
77

8+
#include "Kaixo/Core/Storage.hpp"
89
#include "Kaixo/Utils/Decoders/Decoder.hpp"
910

1011
// ------------------------------------------------
@@ -54,7 +55,7 @@ namespace Kaixo {
5455
if (!std::filesystem::exists(path)) {
5556
std::filesystem::create_directories(path);
5657
}
57-
return path;
58+
return Storage::getOrDefault<std::string>("generation-directory", path.string());
5859
}
5960

6061
// ------------------------------------------------

0 commit comments

Comments
 (0)