/* ============================================================================== 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é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 à séparé 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 à des choses à 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 à changé 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 à la fermeture Config::Save(metronome, options); }