change generateMusic signature + delete second_melody

signature to get usefull infos
second_melody too confusing for gameplay
This commit is contained in:
Crizomb 2025-06-12 16:21:45 +02:00
parent 7c95e8c693
commit 6e6cfc347d
2 changed files with 18 additions and 14 deletions

View file

@ -1,6 +1,7 @@
#include <fmod.hpp>
#include <fmod_errors.h>
#include <memory>
#include <utility>
#include <vector>
class AudioEmitter {
@ -22,7 +23,7 @@ private:
public:
AudioEmitter();
void generateMusic();
std::vector<std::pair<float, int>> generateMusic();
void audioUpdate();
void audioEnd();
int firstChord();

View file

@ -3,6 +3,8 @@
#include <numeric>
#include <random>
#include <string>
#include <utility>
#include <vector>
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<std::pair<float, int>> AudioEmitter::generateMusic() {
std::vector<std::pair<float, int>> 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<float, int>(note_start, index_note));
activeChannels.push_back(channelNote);
}
}
current_beat += nbr_melo_max;
return result;
}
void AudioEmitter::audioUpdate() { system->update(); }