From 7c95e8c6931cc89dc4220b9b97f36fc425bb0594 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Thu, 12 Jun 2025 15:49:07 +0200 Subject: [PATCH] clangd auto-format --- SimpleGame/src/Include/AudioEmitter.hpp | 57 +-- SimpleGame/src/Include/Game.hpp | 6 +- SimpleGame/src/Source/AudioEmitter.cpp | 559 +++++++++++++----------- SimpleGame/src/Source/Game.cpp | 33 +- 4 files changed, 350 insertions(+), 305 deletions(-) diff --git a/SimpleGame/src/Include/AudioEmitter.hpp b/SimpleGame/src/Include/AudioEmitter.hpp index f29fb07..587dcac 100644 --- a/SimpleGame/src/Include/AudioEmitter.hpp +++ b/SimpleGame/src/Include/AudioEmitter.hpp @@ -4,31 +4,32 @@ #include class AudioEmitter { - private: - std::unique_ptr system; - FMOD::Channel* timer{ nullptr }; - std::vector > chords; - std::vector> drums; - std::vector> notes; - int tempo{ 170 }; - std::vector> markov_matrix_chords; - std::vector> markov_matrix_melody; - int nbr_melo_max{ 4 }; - int current_beat{ 0 }; - std::vector> rythmes; - int index_note; - std::vector chordProgression; - std::vector activeChannels; - public: - AudioEmitter(); - void generateMusic(); - void audioUpdate(); - void audioEnd(); - int firstChord(); - int nextChord(int currentChord); - int firstNote(); - int nextNote(int currentNote); - int noteSecondaire(int note); - float getTime(); - float timeBeforeNewGeneration{ 0.2f }; -}; \ No newline at end of file +private: + std::unique_ptr system; + FMOD::Channel *timer{nullptr}; + std::vector> chords; + std::vector> drums; + std::vector> notes; + int tempo{170}; + std::vector> markov_matrix_chords; + std::vector> markov_matrix_melody; + int nbr_melo_max{4}; + int current_beat{0}; + std::vector> rythmes; + int index_note; + std::vector chordProgression; + std::vector activeChannels; + +public: + AudioEmitter(); + void generateMusic(); + void audioUpdate(); + void audioEnd(); + int firstChord(); + int nextChord(int currentChord); + int firstNote(); + int nextNote(int currentNote); + int noteSecondaire(int note); + float getTime(); + float timeBeforeNewGeneration{0.2f}; +}; diff --git a/SimpleGame/src/Include/Game.hpp b/SimpleGame/src/Include/Game.hpp index 23c7ad9..3a9b99d 100644 --- a/SimpleGame/src/Include/Game.hpp +++ b/SimpleGame/src/Include/Game.hpp @@ -1,9 +1,9 @@ #ifndef BOOK_GAME_HPP #define BOOK_GAME_HPP +#include "AudioEmitter.hpp" #include "RoundTarget.hpp" #include -#include "AudioEmitter.hpp" class Game { public: @@ -13,11 +13,11 @@ public: void run(); private: - void processEvents(AudioEmitter& audioEmitter); + void processEvents(AudioEmitter &audioEmitter); void update(sf::Time elapsedTime); void render(); - void updateStatistics(AudioEmitter& audioEmitter); + void updateStatistics(AudioEmitter &audioEmitter); static const sf::Time TimePerFrame; diff --git a/SimpleGame/src/Source/AudioEmitter.cpp b/SimpleGame/src/Source/AudioEmitter.cpp index 2d4b68b..fcac468 100644 --- a/SimpleGame/src/Source/AudioEmitter.cpp +++ b/SimpleGame/src/Source/AudioEmitter.cpp @@ -1,316 +1,359 @@ #include "AudioEmitter.hpp" #include +#include #include #include -#include void ERRCHECK(FMOD_RESULT result) { - if (result != FMOD_OK) { - std::cout << "FMOD error: " << FMOD_ErrorString(result) << std::endl; - exit(-1); - } + if (result != FMOD_OK) { + std::cout << "FMOD error: " << FMOD_ErrorString(result) << std::endl; + exit(-1); + } } -auto fmodSoundDeleter = [](FMOD::Sound* sound) { - if (sound) { - sound->release(); - } +auto fmodSoundDeleter = [](FMOD::Sound *sound) { + if (sound) { + sound->release(); + } }; AudioEmitter::AudioEmitter() { - FMOD::System* rawSystem = nullptr; - ERRCHECK(FMOD::System_Create(&rawSystem)); + FMOD::System *rawSystem = nullptr; + ERRCHECK(FMOD::System_Create(&rawSystem)); - system.reset(rawSystem); - ERRCHECK(system->init(512, FMOD_INIT_NORMAL, nullptr)); + system.reset(rawSystem); + ERRCHECK(system->init(512, FMOD_INIT_NORMAL, nullptr)); - - std::vector rawChords(7); - ERRCHECK(system->createSound("media/accords/do.wav", FMOD_LOOP_OFF, nullptr, &rawChords[0])); - ERRCHECK(system->createSound("media/accords/re.wav", FMOD_LOOP_OFF, nullptr, &rawChords[1])); - ERRCHECK(system->createSound("media/accords/mi.wav", FMOD_LOOP_OFF, nullptr, &rawChords[2])); - ERRCHECK(system->createSound("media/accords/fa.wav", FMOD_LOOP_OFF, nullptr, &rawChords[3])); - ERRCHECK(system->createSound("media/accords/sol.wav", FMOD_LOOP_OFF, nullptr, &rawChords[4])); - ERRCHECK(system->createSound("media/accords/la.wav", FMOD_LOOP_OFF, nullptr, &rawChords[5])); - ERRCHECK(system->createSound("media/accords/si.wav", FMOD_LOOP_OFF, nullptr, &rawChords[6])); - for (int i = 0; i < 7; i += 1) { - chords.push_back(std::unique_ptr(rawChords[i])); - } + std::vector rawChords(7); + ERRCHECK(system->createSound("media/accords/do.wav", FMOD_LOOP_OFF, nullptr, + &rawChords[0])); + ERRCHECK(system->createSound("media/accords/re.wav", FMOD_LOOP_OFF, nullptr, + &rawChords[1])); + ERRCHECK(system->createSound("media/accords/mi.wav", FMOD_LOOP_OFF, nullptr, + &rawChords[2])); + ERRCHECK(system->createSound("media/accords/fa.wav", FMOD_LOOP_OFF, nullptr, + &rawChords[3])); + ERRCHECK(system->createSound("media/accords/sol.wav", FMOD_LOOP_OFF, nullptr, + &rawChords[4])); + ERRCHECK(system->createSound("media/accords/la.wav", FMOD_LOOP_OFF, nullptr, + &rawChords[5])); + ERRCHECK(system->createSound("media/accords/si.wav", FMOD_LOOP_OFF, nullptr, + &rawChords[6])); + for (int i = 0; i < 7; i += 1) { + chords.push_back(std::unique_ptr(rawChords[i])); + } - int nbr_drums = 3; - std::vector rawDrums(nbr_drums); - ERRCHECK(system->createSound("media/percussions/drums1.wav", FMOD_LOOP_OFF, nullptr, &rawDrums[0])); - ERRCHECK(system->createSound("media/percussions/drums2.wav", FMOD_LOOP_OFF, nullptr, &rawDrums[1])); - ERRCHECK(system->createSound("media/percussions/drums3.wav", FMOD_LOOP_OFF, nullptr, &rawDrums[2])); - for (int i = 0; i < nbr_drums; i += 1) { - drums.push_back(std::unique_ptr(rawDrums[i])); - } + int nbr_drums = 3; + std::vector rawDrums(nbr_drums); + ERRCHECK(system->createSound("media/percussions/drums1.wav", FMOD_LOOP_OFF, + nullptr, &rawDrums[0])); + ERRCHECK(system->createSound("media/percussions/drums2.wav", FMOD_LOOP_OFF, + nullptr, &rawDrums[1])); + ERRCHECK(system->createSound("media/percussions/drums3.wav", FMOD_LOOP_OFF, + nullptr, &rawDrums[2])); + for (int i = 0; i < nbr_drums; i += 1) { + drums.push_back(std::unique_ptr(rawDrums[i])); + } - std::vector rawNotes(15); - ERRCHECK(system->createSound("media/notes/A1.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[0])); - ERRCHECK(system->createSound("media/notes/B1.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[1])); - ERRCHECK(system->createSound("media/notes/C1.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[2])); - ERRCHECK(system->createSound("media/notes/D1.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[3])); - ERRCHECK(system->createSound("media/notes/E1.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[4])); - ERRCHECK(system->createSound("media/notes/F1.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[5])); - ERRCHECK(system->createSound("media/notes/G1.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[6])); - ERRCHECK(system->createSound("media/notes/A2.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[7])); - ERRCHECK(system->createSound("media/notes/B2.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[8])); - ERRCHECK(system->createSound("media/notes/C2.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[9])); - ERRCHECK(system->createSound("media/notes/D2.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[10])); - ERRCHECK(system->createSound("media/notes/E2.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[11])); - ERRCHECK(system->createSound("media/notes/F2.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[12])); - ERRCHECK(system->createSound("media/notes/G2.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[13])); - ERRCHECK(system->createSound("media/notes/A3.wav", FMOD_LOOP_OFF, nullptr, &rawNotes[14])); - for (int i = 0; i < 15; i += 1) { - notes.push_back(std::unique_ptr(rawNotes[i])); - } + std::vector rawNotes(15); + ERRCHECK(system->createSound("media/notes/A1.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[0])); + ERRCHECK(system->createSound("media/notes/B1.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[1])); + ERRCHECK(system->createSound("media/notes/C1.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[2])); + ERRCHECK(system->createSound("media/notes/D1.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[3])); + ERRCHECK(system->createSound("media/notes/E1.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[4])); + ERRCHECK(system->createSound("media/notes/F1.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[5])); + ERRCHECK(system->createSound("media/notes/G1.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[6])); + ERRCHECK(system->createSound("media/notes/A2.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[7])); + ERRCHECK(system->createSound("media/notes/B2.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[8])); + ERRCHECK(system->createSound("media/notes/C2.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[9])); + ERRCHECK(system->createSound("media/notes/D2.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[10])); + ERRCHECK(system->createSound("media/notes/E2.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[11])); + ERRCHECK(system->createSound("media/notes/F2.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[12])); + ERRCHECK(system->createSound("media/notes/G2.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[13])); + ERRCHECK(system->createSound("media/notes/A3.wav", FMOD_LOOP_OFF, nullptr, + &rawNotes[14])); + for (int i = 0; i < 15; i += 1) { + notes.push_back(std::unique_ptr(rawN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,5.5,6.5,7}, - {0.5,1,1.5,2,3.5,4,4.5,5,5.5,6,6.5,7}, - {0,0.5,1,1.5,3,4,4.5,5,5.5,7}, - }; + markov_matrix_chords = {{0, 0.10, 0.00, 0.40, 0.35, 0.15, 0.00}, + // Depuis ii (1) + {0.10, 0, 0.00, 0.25, 0.65, 0.00, 0.00}, + // Depuis iii (2) + {0.00, 0.00, 0, 0.25, 0.15, 0.50, 0.10}, + // Depuis IV (3) + {0.45, 0.15, 0.00, 0, 0.25, 0.15, 0.00}, + // Depuis V (4) + {0.70, 0.00, 0.00, 0.10, 0, 0.20, 0.00}, + // Depuis vi (5) + {0.15, 0.15, 0.00, 0.45, 0.25, 0, 0.00}, + // Depuis vii° (6) + {0.80, 0.00, 0.00, 0.00, 0.20, 0.00, 0.00}}; - markov_matrix_chords = { - {0, 0.10, 0.00, 0.40, 0.35, 0.15, 0.00}, - // Depuis ii (1) - {0.10, 0, 0.00, 0.25, 0.65, 0.00, 0.00}, - // Depuis iii (2) - {0.00, 0.00, 0, 0.25, 0.15, 0.50, 0.10}, - // Depuis IV (3) - {0.45, 0.15, 0.00, 0, 0.25, 0.15, 0.00}, - // Depuis V (4) - {0.70, 0.00, 0.00, 0.10, 0, 0.20, 0.00}, - // Depuis vi (5) - {0.15, 0.15, 0.00, 0.45, 0.25, 0, 0.00}, - // Depuis vii° (6) - {0.80, 0.00, 0.00, 0.00, 0.20, 0.00, 0.00} - }; + markov_matrix_melody = {// A1 (0) + {0.05, 0.30, 0.25, 0.15, 0.10, 0.05, 0.03, 0.02, 0.02, + 0.01, 0.01, 0.00, 0.00, 0.00, 0.00}, + // B1 (1) + {0.20, 0.05, 0.30, 0.20, 0.10, 0.05, 0.03, 0.02, 0.02, + 0.01, 0.01, 0.00, 0.00, 0.00, 0.00}, + // C2 (2) + {0.10, 0.20, 0.05, 0.30, 0.15, 0.10, 0.05, 0.02, 0.02, + 0.01, 0.00, 0.00, 0.00, 0.00, 0.00}, + // D2 (3) + {0.05, 0.15, 0.20, 0.05, 0.30, 0.15, 0.05, 0.03, 0.02, + 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, + // E2 (4) + {0.03, 0.07, 0.15, 0.20, 0.05, 0.25, 0.15, 0.07, 0.03, + 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, + // F2 (5) + {0.02, 0.05, 0.10, 0.15, 0.20, 0.05, 0.25, 0.15, 0.05, + 0.02, 0.01, 0.00, 0.00, 0.00, 0.00}, + // G2 (6) + {0.01, 0.03, 0.07, 0.10, 0.15, 0.20, 0.05, 0.25, 0.10, + 0.03, 0.01, 0.00, 0.00, 0.00, 0.00}, + // A2 (7) - Centre tonal + {0.00, 0.02, 0.05, 0.10, 0.15, 0.20, 0.25, 0.05, 0.15, + 0.05, 0.02, 0.01, 0.00, 0.00, 0.00}, + // B2 (8) + {0.00, 0.00, 0.01, 0.03, 0.07, 0.15, 0.20, 0.25, 0.05, + 0.20, 0.10, 0.05, 0.03, 0.01, 0.00}, + // C3 (9) + {0.00, 0.00, 0.00, 0.02, 0.05, 0.10, 0.15, 0.20, 0.25, + 0.05, 0.20, 0.15, 0.07, 0.03, 0.01}, + // D3 (10) + {0.00, 0.00, 0.00, 0.00, 0.03, 0.07, 0.10, 0.15, 0.20, + 0.25, 0.05, 0.25, 0.15, 0.07, 0.03}, + // E3 (11) + {0.00, 0.00, 0.00, 0.00, 0.00, 0.02, 0.05, 0.10, 0.15, + 0.20, 0.25, 0.05, 0.20, 0.15, 0.08}, + // F3 (12) + {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.03, 0.07, 0.10, + 0.15, 0.20, 0.25, 0.05, 0.25, 0.15}, + // G3 (13) + {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.02, 0.05, + 0.10, 0.15, 0.20, 0.25, 0.05, 0.28}, + // A3 (14) + {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.03, + 0.07, 0.10, 0.15, 0.20, 0.25, 0.05}}; - markov_matrix_melody = { - // A1 (0) - {0.05, 0.30, 0.25, 0.15, 0.10, 0.05, 0.03, 0.02, 0.02, 0.01, 0.01, 0.00, 0.00, 0.00, 0.00}, - // B1 (1) - {0.20, 0.05, 0.30, 0.20, 0.10, 0.05, 0.03, 0.02, 0.02, 0.01, 0.01, 0.00, 0.00, 0.00, 0.00}, - // C2 (2) - {0.10, 0.20, 0.05, 0.30, 0.15, 0.10, 0.05, 0.02, 0.02, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00}, - // D2 (3) - {0.05, 0.15, 0.20, 0.05, 0.30, 0.15, 0.05, 0.03, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, - // E2 (4) - {0.03, 0.07, 0.15, 0.20, 0.05, 0.25, 0.15, 0.07, 0.03, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00}, - // F2 (5) - {0.02, 0.05, 0.10, 0.15, 0.20, 0.05, 0.25, 0.15, 0.05, 0.02, 0.01, 0.00, 0.00, 0.00, 0.00}, - // G2 (6) - {0.01, 0.03, 0.07, 0.10, 0.15, 0.20, 0.05, 0.25, 0.10, 0.03, 0.01, 0.00, 0.00, 0.00, 0.00}, - // A2 (7) - Centre tonal - {0.00, 0.02, 0.05, 0.10, 0.15, 0.20, 0.25, 0.05, 0.15, 0.05, 0.02, 0.01, 0.00, 0.00, 0.00}, - // B2 (8) - {0.00, 0.00, 0.01, 0.03, 0.07, 0.15, 0.20, 0.25, 0.05, 0.20, 0.10, 0.05, 0.03, 0.01, 0.00}, - // C3 (9) - {0.00, 0.00, 0.00, 0.02, 0.05, 0.10, 0.15, 0.20, 0.25, 0.05, 0.20, 0.15, 0.07, 0.03, 0.01}, - // D3 (10) - {0.00, 0.00, 0.00, 0.00, 0.03, 0.07, 0.10, 0.15, 0.20, 0.25, 0.05, 0.25, 0.15, 0.07, 0.03}, - // E3 (11) - {0.00, 0.00, 0.00, 0.00, 0.00, 0.02, 0.05, 0.10, 0.15, 0.20, 0.25, 0.05, 0.20, 0.15, 0.08}, - // F3 (12) - {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.03, 0.07, 0.10, 0.15, 0.20, 0.25, 0.05, 0.25, 0.15}, - // G3 (13) - {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.02, 0.05, 0.10, 0.15, 0.20, 0.25, 0.05, 0.28}, - // A3 (14) - {0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.03, 0.07, 0.10, 0.15, 0.20, 0.25, 0.05} - }; + chordProgression.resize(4); - chordProgression.resize(4); + int chord = firstChord(); + chordProgression[0] = chord; + for (int i = 1; i < 4; i += 1) { + chord = nextChord(chord); + chordProgression[i] = chord; + } - int chord = firstChord(); - chordProgression[0] = chord; - for (int i = 1; i < 4; i += 1) { - chord = nextChord(chord); - chordProgression[i] = chord; - } - - FMOD::Sound* metronome_Sound; - ERRCHECK(system->createSound("media/notes/A1.wav", FMOD_DEFAULT, nullptr, &metronome_Sound)); - ERRCHECK(system->playSound(metronome_Sound, nullptr, true, &timer)); - ERRCHECK(timer->setVolume(0)); - ERRCHECK(timer->setPaused(false)); + FMOD::Sound *metronome_Sound; + ERRCHECK(system->createSound("media/notes/A1.wav", FMOD_DEFAULT, nullptr, + &metronome_Sound)); + ERRCHECK(system->playSound(metronome_Sound, nullptr, true, &timer)); + ERRCHECK(timer->setVolume(0)); + ERRCHECK(timer->setPaused(false)); } int AudioEmitter::firstChord() { - std::vector possibleChords = { 1,5,6 }; - return possibleChords[rand() % possibleChords.size()] - 1; + std::vector possibleChords = {1, 5, 6}; + return possibleChords[rand() % possibleChords.size()] - 1; } int AudioEmitter::firstNote() { - std::vector possibleNotes = { 4,5,6,7,8,9,10,11 }; - return possibleNotes[rand() % possibleNotes.size()]; + std::vector possibleNotes = {4, 5, 6, 7, 8, 9, 10, 11}; + return possibleNotes[rand() % possibleNotes.size()]; } -int sampleIndex(const std::vector& probabilities) { - // Créer un générateur aléatoire - static std::random_device rd; - static std::mt19937 gen(rd()); +int sampleIndex(const std::vector &probabilities) { + // Créer un générateur aléatoire + static std::random_device rd; + static std::mt19937 gen(rd()); - // Créer une distribution discrète avec les probabilités données - std::discrete_distribution<> dist(probabilities.begin(), probabilities.end()); + // Créer une distribution discrète avec les probabilités données + std::discrete_distribution<> dist(probabilities.begin(), probabilities.end()); - // Tirer un échantillon - return dist(gen); + // Tirer un échantillon + return dist(gen); } -int randomWeightedChoice(const std::vector& values, const std::vector& weights) { - if (values.size() != weights.size() || values.empty()) { - throw std::invalid_argument("Les tableaux doivent être de même taille et non vides."); +int randomWeightedChoice(const std::vector &values, + const std::vector &weights) { + if (values.size() != weights.size() || values.empty()) { + throw std::invalid_argument( + "Les tableaux doivent être de même taille et non vides."); + } + + // Calcul de la somme totale des poids + double totalWeight = std::accumulate(weights.begin(), weights.end(), 0.0); + if (totalWeight <= 0) { + throw std::invalid_argument("La somme des poids doit être positive."); + } + + // Générateur aléatoire + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0.0, totalWeight); + + double r = dis(gen); + double cumulative = 0.0; + + for (size_t i = 0; i < values.size(); ++i) { + cumulative += weights[i]; + if (r < cumulative) { + return values[i]; } + } - // Calcul de la somme totale des poids - double totalWeight = std::accumulate(weights.begin(), weights.end(), 0.0); - if (totalWeight <= 0) { - throw std::invalid_argument("La somme des poids doit être positive."); - } - - // Générateur aléatoire - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_real_distribution<> dis(0.0, totalWeight); - - double r = dis(gen); - double cumulative = 0.0; - - for (size_t i = 0; i < values.size(); ++i) { - cumulative += weights[i]; - if (r < cumulative) { - return values[i]; - } - } - - // Fallback - devrait normalement ne jamais être atteint - return values.back(); + // Fallback - devrait normalement ne jamais être atteint + return values.back(); } int AudioEmitter::nextChord(int currentChord) { - return sampleIndex(markov_matrix_chords[currentChord]); + return sampleIndex(markov_matrix_chords[currentChord]); } int AudioEmitter::nextNote(int currentNote) { - return sampleIndex(markov_matrix_melody[currentNote]); + return sampleIndex(markov_matrix_melody[currentNote]); } int AudioEmitter::noteSecondaire(int note) { - std::vector notesPossibles = { note - 4,note - 3,note + 3,note + 4,note + 5 }; - std::vector proba = { 0.05,0.10,0.60,0.05,0.2 }; - for (int i = 0; i < proba.size(); i += 1) { - if ((notesPossibles[i] < 0) || (notesPossibles[i] >= notes.size())) { - proba[i] = 0; - } + std::vector notesPossibles = {note - 4, note - 3, note + 3, note + 4, + note + 5}; + std::vector proba = {0.05, 0.10, 0.60, 0.05, 0.2}; + for (int i = 0; i < proba.size(); i += 1) { + if ((notesPossibles[i] < 0) || (notesPossibles[i] >= notes.size())) { + proba[i] = 0; } - return randomWeightedChoice(notesPossibles, proba); + } + return randomWeightedChoice(notesPossibles, proba); } void AudioEmitter::generateMusic() { - float beatDuration = tempo / 60.f; - unsigned int sampleRate = 48000; - int maxsize = 400; - if (activeChannels.size() > maxsize) { - for (int i = 0; i < maxsize / 2; i += 1) { - if (activeChannels[i]) { - activeChannels[i]->stop(); - } - } + float beatDuration = tempo / 60.f; + unsigned int sampleRate = 48000; + int maxsize = 400; + if (activeChannels.size() > maxsize) { + for (int i = 0; i < maxsize / 2; i += 1) { + if (activeChannels[i]) { + activeChannels[i]->stop(); + } } - chordProgression[0] = nextChord(chordProgression[3]); - for (int i = 1; i < 4; i += 1) { - chordProgression[i] = nextChord(chordProgression[i-1]); + } + chordProgression[0] = nextChord(chordProgression[3]); + for (int i = 1; i < 4; i += 1) { + chordProgression[i] = nextChord(chordProgression[i - 1]); + } + int index_drums = rand() % 3; + int second_melody = 1; // Pas de seconde mélodie + for (int i = current_beat; i < current_beat + nbr_melo_max; i += 1) { + // Chords + FMOD::Channel *channelChords = nullptr; + int index_chord = chordProgression[i % 4]; + ERRCHECK(system->playSound(chords[index_chord].get(), nullptr, true, + &channelChords)); + + unsigned long long delay = + (unsigned long long)(i * beatDuration * sampleRate); + ERRCHECK(channelChords->setDelay(delay, 0, true)); + ERRCHECK(channelChords->setPaused(false)); + + activeChannels.push_back(channelChords); + // Drums + FMOD::Channel *channelDrums = nullptr; + ERRCHECK(system->playSound(drums[index_drums].get(), nullptr, true, + &channelDrums)); + ERRCHECK(channelDrums->setDelay(delay, 0, true)); + ERRCHECK(channelDrums->setPaused(false)); + + activeChannels.push_back(channelDrums); + // Mélodie + std::vector rythme_melodie = rythmes[rand() % rythmes.size()]; + for (float time : rythme_melodie) { + FMOD::Channel *channelNote = nullptr; + ERRCHECK(system->playSound(notes[index_note].get(), nullptr, true, + &channelNote)); + unsigned long long delayNote = + (unsigned long long)((i + time / 8.f) * beatDuration * sampleRate); + ERRCHECK(channelNote->setDelay(delayNote, 0, true)); + ERRCHECK(channelNote->setPaused(false)); + if (second_melody == 0) { + FMOD::Channel *channelNoteSec = nullptr; + ERRCHECK(system->playSound(notes[noteSecondaire(index_note)].get(), + nullptr, true, &channelNoteSec)); + unsigned long long delayNote = + (unsigned long long)((i + time / 8.f) * beatDuration * sampleRate); + ERRCHECK(channelNoteSec->setDelay(delayNote, 0, true)); + ERRCHECK(channelNoteSec->setPaused(false)); + activeChannels.push_back(channelNoteSec); + } + index_note = nextNote(index_note); + activeChannels.push_back(channelNote); } - int index_drums = rand() % 3; - int second_melody = 1; //Pas de seconde mélodie - for (int i = current_beat; i < current_beat + nbr_melo_max; i += 1) { - //Chords - FMOD::Channel* channelChords = nullptr; - int index_chord = chordProgression[i % 4]; - ERRCHECK(system->playSound(chords[index_chord].get(), nullptr, true, &channelChords)); - - unsigned long long delay = (unsigned long long)(i * beatDuration * sampleRate); - ERRCHECK(channelChords->setDelay(delay, 0, true)); - ERRCHECK(channelChords->setPaused(false)); - - activeChannels.push_back(channelChords); - //Drums - FMOD::Channel* channelDrums = nullptr; - ERRCHECK(system->playSound(drums[index_drums].get(), nullptr, true, &channelDrums)); - ERRCHECK(channelDrums->setDelay(delay, 0, true)); - ERRCHECK(channelDrums->setPaused(false)); - - activeChannels.push_back(channelDrums); - //Mélodie - std::vector rythme_melodie = rythmes[rand() % rythmes.size()]; - for (float time : rythme_melodie) { - FMOD::Channel* channelNote = nullptr; - ERRCHECK(system->playSound(notes[index_note].get(), nullptr, true, &channelNote)); - unsigned long long delayNote = (unsigned long long)((i + time / 8.f) * beatDuration * sampleRate); - ERRCHECK(channelNote->setDelay(delayNote, 0, true)); - ERRCHECK(channelNote->setPaused(false)); - if (second_melody == 0) { - FMOD::Channel* channelNoteSec = nullptr; - ERRCHECK(system->playSound(notes[noteSecondaire(index_note)].get(), nullptr, true, &channelNoteSec)); - unsigned long long delayNote = (unsigned long long)((i + time / 8.f) * beatDuration * sampleRate); - ERRCHECK(channelNoteSec->setDelay(delayNote, 0, true)); - ERRCHECK(channelNoteSec->setPaused(false)); - activeChannels.push_back(channelNoteSec); - } - index_note = nextNote(index_note); - activeChannels.push_back(channelNote); - } - } - current_beat += nbr_melo_max; + } + current_beat += nbr_melo_max; } -void AudioEmitter::audioUpdate() { - system->update(); -} +void AudioEmitter::audioUpdate() { system->update(); } void AudioEmitter::audioEnd() { - for (FMOD::Channel* c : activeChannels) { - delete c; - } - timer->stop(); - system->close(); - system->release(); + for (FMOD::Channel *c : activeChannels) { + delete c; + } + timer->stop(); + system->close(); + system->release(); } float AudioEmitter::getTime() { - float beatDuration = tempo / 60.f / 8.f; - unsigned long long dspClock = 0; - ERRCHECK(timer->getDSPClock(&dspClock, nullptr)); - return dspClock / 48000.f / beatDuration; -} \ No newline at end of file + float beatDuration = tempo / 60.f / 8.f; + unsigned long long dspClock = 0; + ERRCHECK(timer->getDSPClock(&dspClock, nullptr)); + return dspClock / 48000.f / beatDuration; +} diff --git a/SimpleGame/src/Source/Game.cpp b/SimpleGame/src/Source/Game.cpp index 2349952..b693450 100644 --- a/SimpleGame/src/Source/Game.cpp +++ b/SimpleGame/src/Source/Game.cpp @@ -1,7 +1,7 @@ #include "Game.hpp" -#include #include #include +#include const sf::Time Game::TimePerFrame = sf::seconds(1.f / 60.f); @@ -24,13 +24,14 @@ void Game::run() { sf::Time elapsedTime = clock.restart(); timeSinceLastUpdate += elapsedTime; while (timeSinceLastUpdate > TimePerFrame) { - if (32.f - fmod(audioEmitter.getTime(), 32.f) < audioEmitter.timeBeforeNewGeneration) { - if (!generated) { - audioEmitter.generateMusic(); - generated = true; - } + if (32.f - fmod(audioEmitter.getTime(), 32.f) < + audioEmitter.timeBeforeNewGeneration) { + if (!generated) { + audioEmitter.generateMusic(); + generated = true; + } } else { - generated = false; + generated = false; } timeSinceLastUpdate -= TimePerFrame; @@ -44,7 +45,7 @@ void Game::run() { } } -void Game::processEvents(AudioEmitter& audioEmitter) { +void Game::processEvents(AudioEmitter &audioEmitter) { while (const std::optional event = mWindow.pollEvent()) { if (const auto *keyPressed = event->getIf()) { mTarget.handlePlayerInput(keyPressed->code, true); @@ -52,7 +53,7 @@ void Game::processEvents(AudioEmitter& audioEmitter) { event->getIf()) { mTarget.handlePlayerInput(keyReleased->code, false); } else if (event->is()) { - audioEmitter.audioEnd(); + audioEmitter.audioEnd(); mWindow.close(); } } @@ -67,11 +68,11 @@ void Game::render() { mWindow.display(); } -void Game::updateStatistics(AudioEmitter& audioEmitter) { - mStatisticsText.setString(std::format( - "Mesure = {}\nBeat = {} us", ceil(audioEmitter.getTime()/8.f), - ceil(fmod(audioEmitter.getTime(),8.f)))); +void Game::updateStatistics(AudioEmitter &audioEmitter) { + mStatisticsText.setString(std::format( + "Mesure = {}\nBeat = {} us", ceil(audioEmitter.getTime() / 8.f), + ceil(fmod(audioEmitter.getTime(), 8.f)))); - mStatisticsUpdateTime -= sf::seconds(1.0f); - mStatisticsNumFrames = 0; -} \ No newline at end of file + mStatisticsUpdateTime -= sf::seconds(1.0f); + mStatisticsNumFrames = 0; +}