Tchick/Source/MainComponent.cpp
2026-02-04 11:51:22 +01:00

143 lines
4.5 KiB
C++
Raw Permalink Blame History

/*
==============================================================================
Copyright 2022 Nicolas Chambert
This program is free software : you can redistribute itand /or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see < http://www.gnu.org/licenses/>.
==============================================================================
*/
#include "MainComponent.h"
//==============================================================================
MainComponent::MainComponent(Config& conf) :
metronome(conf),
measures(juce::Colours::whitesmoke),
bpm(juce::Colours::whitesmoke),
tempo(juce::Colours::whitesmoke),
playButton(juce::Colours::whitesmoke),
options(juce::Colours::whitesmoke, conf)
{
addAndMakeVisible (measures);
addAndMakeVisible (bpm);
bpm.init(metronome);
addAndMakeVisible (tempo);
tempo.init(metronome);
addAndMakeVisible(playButton);
playButton.init(metronome);
addAndMakeVisible (options);
options.init(metronome);
setSize (400, 600);
// Some platforms require permissions to open input channels so request that here
if (juce::RuntimePermissions::isRequired (juce::RuntimePermissions::recordAudio)
&& ! juce::RuntimePermissions::isGranted (juce::RuntimePermissions::recordAudio))
{
juce::RuntimePermissions::request (juce::RuntimePermissions::recordAudio,
[&] (bool granted) { if (granted) setAudioChannels (2, 2); });
}
else
{
// Specify the number of input and output channels that we want to open
setAudioChannels (0, 2);
}
startTimer(20); // timer pour aller relire les eventuels signaux du m<>tronome
}
MainComponent::~MainComponent()
{
shutdownAudio();
}
//==============================================================================
void MainComponent::prepareToPlay (int , double sampleRate)
{
metronome.prepareToPlay(sampleRate); // passe le sampleRate au m<>tronome
}
void MainComponent::getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill)
{
// netoyage de l'entr<74>e
bufferToFill.clearActiveBufferRegion();
if (playButton.playState == PlayState::playing)
{
// boucle de lecture du m<>tronome
metronome.getNextAudioBlock (bufferToFill);
}
}
void MainComponent::releaseResources()
{
}
//==============================================================================
void MainComponent::paint (juce::Graphics& g)
{
g.fillAll (juce::Colours::lightgrey);
}
void MainComponent::resized()
{
// Comme on <20> s<>par<61> tout en composants, on utilise un simple grille en ligne
juce::Grid grid;
using Track = juce::Grid::TrackInfo;
using Fr = juce::Grid::Fr;
grid.templateRows = { Track (Fr (5)), Track (Fr (12)), Track (Fr (8)),Track (Fr (20)), Track (Fr (4)) };
grid.templateColumns = { Track (Fr (1)) };
float margin = 1.0f;
grid.items = {
juce::GridItem (measures).withMargin(juce::GridItem::Margin(0.0f, 0.0f, margin, 0.0f)),
juce::GridItem (bpm).withMargin(juce::GridItem::Margin(margin, 0.0f, margin, 0.0f)),
juce::GridItem (tempo).withMargin(juce::GridItem::Margin(margin, 0.0f, margin, 0.0f)),
juce::GridItem(playButton).withMargin(juce::GridItem::Margin(margin, 0.0f, margin, 0.0f)),
juce::GridItem (options).withMargin(juce::GridItem::Margin(margin, 0.0f, 0.0f, 0.0f)),
};
grid.performLayout (getLocalBounds());
}
void MainComponent::timerCallback()
{
// On regarde si le metronome <20> des choses <20> nous dire (changement de mesure)
if (metronome.signal.get() == true)
{
int cur = metronome.getCurrentMeasure();
metronome.signal.set(false); // on ferme le signal
int meas = metronome.getMeasures();
cur = cur > 0 ? cur - 1 : cur;
measures.update(meas, cur % meas);
if (cur != lastcur)
{
// On <20> chang<6E> de mesure, on indique au playButton de s'allumer pendant 6 frames
playButton.highlightFrames = 6;
lastcur = cur;
}
}
playButton.highlight(); // le bouton va se redessiner et decrementer son highlightFrames
}
void MainComponent::saveConfig()
{
// sauvegarde la configuration du metronome <20> la fermeture
Config::Save(metronome, options);
}