From 6e6cfc347d55891602bed0e27db291e2c01f0bfd Mon Sep 17 00:00:00 2001 From: Crizomb Date: Thu, 12 Jun 2025 16:21:45 +0200 Subject: [PATCH] change generateMusic signature + delete second_melody signature to get usefull infos second_melody too confusing for gameplay --- SimpleGame/src/Include/AudioEmitter.hpp | 3 ++- SimpleGame/src/Source/AudioEmitter.cpp | 29 ++++++++++++++----------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/SimpleGame/src/Include/AudioEmitter.hpp b/SimpleGame/src/Include/AudioEmitter.hpp index 587dcac..88cacdf 100644 --- a/SimpleGame/src/Include/AudioEmitter.hpp +++ b/SimpleGame/src/Include/AudioEmitter.hpp @@ -1,6 +1,7 @@ #include #include #include +#include #include class AudioEmitter { @@ -22,7 +23,7 @@ private: public: AudioEmitter(); - void generateMusic(); + std::vector> generateMusic(); void audioUpdate(); void audioEnd(); int firstChord(); diff --git a/SimpleGame/src/Source/AudioEmitter.cpp b/SimpleGame/src/Source/AudioEmitter.cpp index fcac468..f4ef50d 100644 --- a/SimpleGame/src/Source/AudioEmitter.cpp +++ b/SimpleGame/src/Source/AudioEmitter.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include void ERRCHECK(FMOD_RESULT result) { if (result != FMOD_OK) { @@ -275,7 +277,15 @@ int AudioEmitter::noteSecondaire(int note) { return randomWeightedChoice(notesPossibles, proba); } -void AudioEmitter::generateMusic() { +/** + * generate music + * + * @return vector of the notes generated. first is when the note will be played + * in seconds, second is the note + */ +std::vector> AudioEmitter::generateMusic() { + std::vector> result; + result.reserve(16 * AudioEmitter::nbr_melo_max); float beatDuration = tempo / 60.f; unsigned int sampleRate = 48000; int maxsize = 400; @@ -291,7 +301,6 @@ void AudioEmitter::generateMusic() { 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; @@ -319,25 +328,19 @@ void AudioEmitter::generateMusic() { FMOD::Channel *channelNote = nullptr; ERRCHECK(system->playSound(notes[index_note].get(), nullptr, true, &channelNote)); + float note_start = (i + time / 8.f) * beatDuration; + printf("note start %f \n", note_start); unsigned long long delayNote = - (unsigned long long)((i + time / 8.f) * beatDuration * sampleRate); + (unsigned long long)(note_start * 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); + result.push_back(std::pair(note_start, index_note)); activeChannels.push_back(channelNote); } } current_beat += nbr_melo_max; + return result; } void AudioEmitter::audioUpdate() { system->update(); }