Krotos Modules 3
Loading...
Searching...
No Matches
KwidgetGUI_OfflineAnalyser.cpp
Go to the documentation of this file.
1namespace krotos
2{
4 : KwidgetGUI(owner), m_stft(2048, 2048, 0.5f, WindowType::Hann, ShortTimeFourierTransform::fftMode::freqMagOnly,
5 44100 /*TODO: Find way to pass sampleRate here!!*/)
6 {
7 setOpaque(true);
8 // hide the connection Button
9 // getTopBarComponent().m_selectButton.setVisible(false);
10
11 addAndMakeVisible(m_openButton);
12 m_openButton.setButtonText("Open");
13
14 addAndMakeVisible(m_clearButton);
15 m_clearButton.setButtonText("Clear");
16
17 addAndMakeVisible(m_erbButton);
18 m_erbButton.setButtonText("ERB Spectro");
19
20 addAndMakeVisible(m_stftButton);
21 m_stftButton.setButtonText("STFT Spectro");
22
23 m_openButton.onClick = [this] { openButtonClicked(); };
24 m_clearButton.onClick = [this] { clearButtonClicked(); };
25 m_erbButton.onClick = [this] { erbButtonClicked(); };
26 m_stftButton.onClick = [this] { stftButtonClicked(); };
27 }
28
30 {
31 // Set up the File browser window
32 m_chooser = std::make_unique<juce::FileChooser>("Select a Wav file to load..", juce::File{}, "*.wav");
33
34 auto chooserFlags = juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles;
35
36 // Launch the File browser Async
37 m_chooser->launchAsync(chooserFlags, [this](const juce::FileChooser& fc) {
38 auto file = fc.getResult();
39
40 if (file == juce::File{})
41 return;
42
43 AudioFormatManager formatManager;
44 formatManager.registerBasicFormats();
45
46 std::unique_ptr<juce::AudioFormatReader> reader(formatManager.createReaderFor(file));
47
48 if (reader.get() != nullptr)
49 {
50 m_fileBuffer.setSize((int)reader->numChannels, (int)reader->lengthInSamples);
51 reader->read(&m_fileBuffer, 0, (int)reader->lengthInSamples, 0, true, true);
52
53 m_sampleRate = static_cast<float>(reader->sampleRate);
54 }
55 });
56 }
57
59 {
60 // Clear the image and the Buffer
61 m_fileBuffer.clear();
62
63 m_spectrogramImage.clear(getLocalBounds(), Colour::fromRGB(0, 0, 0));
64
65 repaint();
66 }
67
69 {
70 // m_spectrogramImage.clear(getLocalBounds(), Colour::fromRGB(0, 0, 0));
71
73 // avergae buffer to mono
75 // extract data to vector
76 auto signalData = extractData(m_fileBuffer);
77
78 auto erbSpec = m_erbSTFT.filterSpectrum(signalData);
80
81 repaint();
82 }
83
85 {
86 auto stftMatrix = m_stft.stft(m_fileBuffer);
88
89 repaint();
90 }
91
93 {
95
96 g.drawImage(m_spectrogramImage, m_imageBounds.toFloat());
97 }
98
100 {
102
103 auto bounds = m_currentBounds;
104
105 auto topBar = bounds.removeFromTop(40);
106
107 Rectangle<int> openButtonBounds(m_currentBounds.getX() + k_padding, topBar.getY(), 60, 40);
108 Rectangle<int> clearButtonBounds(m_currentBounds.getX() + k_padding + 60, topBar.getY(), 60, 40);
109 Rectangle<int> stftButtonBounds(m_currentBounds.getX() + k_padding + 120, topBar.getY(), 60, 40);
110 Rectangle<int> erbButtonBounds(m_currentBounds.getX() + k_padding + 180, topBar.getY(), 60, 40);
111
112 m_openButton.setBounds(openButtonBounds);
113 m_clearButton.setBounds(clearButtonBounds);
114 m_erbButton.setBounds(erbButtonBounds);
115 m_stftButton.setBounds(stftButtonBounds);
116
117 m_imageBounds = bounds.reduced(30, 30);
118
119 m_spectrogramImage = Image(Image::RGB, m_imageBounds.getWidth(), m_imageBounds.getHeight(), true);
120 }
121} // namespace krotos
void drawSpectrogram(juce::Image &image, const std::vector< std::vector< float > > &erbSTFTPowMatrix)
Definition ERB_FFTSpectrogram.cpp:337
void setSampleRate(float fs)
Definition ERB_FFTSpectrogram.cpp:18
std::vector< std::vector< float > > filterSpectrum(std::vector< float > &inputSignal)
Definition ERB_FFTSpectrogram.cpp:38
TextButton m_erbButton
Definition KwidgetGUI_OfflineAnalyser.h:69
TextButton m_openButton
Definition KwidgetGUI_OfflineAnalyser.h:66
TextButton m_stftButton
Definition KwidgetGUI_OfflineAnalyser.h:68
ERB_FFTSpectrogram m_erbSTFT
Definition KwidgetGUI_OfflineAnalyser.h:60
void paint(Graphics &g) override
Definition KwidgetGUI_OfflineAnalyser.cpp:92
void stftButtonClicked()
Definition KwidgetGUI_OfflineAnalyser.cpp:84
void resized() override
Definition KwidgetGUI_OfflineAnalyser.cpp:99
void clearButtonClicked()
Definition KwidgetGUI_OfflineAnalyser.cpp:58
KwidgetGUI_OfflineAnalyser(Kwidget &owner)
Definition KwidgetGUI_OfflineAnalyser.cpp:3
float m_sampleRate
Definition KwidgetGUI_OfflineAnalyser.h:61
void erbButtonClicked()
Definition KwidgetGUI_OfflineAnalyser.cpp:68
void openButtonClicked()
Definition KwidgetGUI_OfflineAnalyser.cpp:29
void averageToMono(AudioSampleBuffer &buffer)
Definition KwidgetGUI_OfflineAnalyser.h:19
TextButton m_clearButton
Definition KwidgetGUI_OfflineAnalyser.h:67
Rectangle< int > m_imageBounds
Definition KwidgetGUI_OfflineAnalyser.h:64
std::unique_ptr< juce::FileChooser > m_chooser
Definition KwidgetGUI_OfflineAnalyser.h:71
AudioSampleBuffer m_fileBuffer
Definition KwidgetGUI_OfflineAnalyser.h:73
std::vector< float > extractData(const AudioSampleBuffer &buffer)
Definition KwidgetGUI_OfflineAnalyser.h:39
Image m_spectrogramImage
Definition KwidgetGUI_OfflineAnalyser.h:63
ShortTimeFourierTransform m_stft
Definition KwidgetGUI_OfflineAnalyser.h:59
Interface for a UI Component that controls a KwidgetProcessor.
Definition KwidgetGUI.h:24
void paint(Graphics &g) override
Definition KwidgetGUI.cpp:26
Rectangle< int > m_currentBounds
Definition KwidgetGUI.h:146
void resized() override
Definition KwidgetGUI.cpp:45
Definition Kwidget.h:8
Apply STFT analysis to a full audio signal offline.
Definition ShortTimeFourierTransform.h:15
void drawSpectrogram(juce::Image &image, const std::vector< std::vector< float > > &stftMatrix)
Definition ShortTimeFourierTransform.cpp:171
std::vector< std::vector< float > > stft(const AudioSampleBuffer &inputSignal)
Definition ShortTimeFourierTransform.cpp:26
Definition AirAbsorptionFilter.cpp:2
WindowType
Definition WindowFunctions.h:6
const int k_padding
Definition KwidgetGUI.h:3