This commit is contained in:
Crizomb 2025-06-14 23:32:36 +02:00
commit 40d3358c9d
3 changed files with 74 additions and 20 deletions

View file

@ -16,6 +16,7 @@ private:
std::vector<std::vector<float>> markov_matrix_chords; std::vector<std::vector<float>> markov_matrix_chords;
std::vector<std::vector<float>> markov_matrix_melody; std::vector<std::vector<float>> markov_matrix_melody;
int nbr_melo_max{4}; int nbr_melo_max{4};
int nbr_melo_total{ 64 };
int current_beat{0}; int current_beat{0};
std::vector<std::vector<float>> rythmes; std::vector<std::vector<float>> rythmes;
int index_note; int index_note;

View file

@ -58,6 +58,9 @@ AudioEmitter::AudioEmitter() {
for (int i = 0; i < 7; i += 1) { for (int i = 0; i < 7; i += 1) {
chords.push_back(std::unique_ptr<FMOD::Sound>(rawChords[i])); chords.push_back(std::unique_ptr<FMOD::Sound>(rawChords[i]));
} }
for (int i = 0; i < 7; i += 1) {
chords.push_back(std::unique_ptr<FMOD::Sound>(rawChords2[i]));
}
std::vector<FMOD::Sound *> rawNotes(15); std::vector<FMOD::Sound *> rawNotes(15);
ERRCHECK(system->createSound("media/notes/A1.mp3", FMOD_LOOP_OFF, nullptr, ERRCHECK(system->createSound("media/notes/A1.mp3", FMOD_LOOP_OFF, nullptr,
@ -96,10 +99,6 @@ AudioEmitter::AudioEmitter() {
index_note = firstNote(); index_note = firstNote();
std::vector<std::vector<float>> rythmes2 = {
};
rythmes = { rythmes = {
{0, 4}, {0, 4},
{0, 2, 4, 6}, {0, 2, 4, 6},
@ -285,6 +284,14 @@ std::vector<std::pair<float, int>> AudioEmitter::generateMusic() {
float beatDuration = tempo / 60.f; float beatDuration = tempo / 60.f;
unsigned int sampleRate = 48000; unsigned int sampleRate = 48000;
int maxsize = 400; int maxsize = 400;
int variation = 0;
if (((current_beat / nbr_melo_max) % 4) < 2) { //On change la manière dont sont joués les accords toutes les 2*nbr_melo_max mélodies générées
variation = 0;
}
else {
variation = 1;
}
int nbrChords = 7;
if (activeChannels.size() > maxsize) { if (activeChannels.size() > maxsize) {
for (int i = 0; i < maxsize / 2; i += 1) { for (int i = 0; i < maxsize / 2; i += 1) {
if (activeChannels[i]) { if (activeChannels[i]) {
@ -301,7 +308,7 @@ std::vector<std::pair<float, int>> AudioEmitter::generateMusic() {
// Chords // Chords
FMOD::Channel *channelChords = nullptr; FMOD::Channel *channelChords = nullptr;
int index_chord = chordProgression[i % 4]; int index_chord = chordProgression[i % 4];
ERRCHECK(system->playSound(chords[index_chord].get(), nullptr, true, ERRCHECK(system->playSound(chords[index_chord + variation*nbrChords].get(), nullptr, true,
&channelChords)); &channelChords));
unsigned long long delay = unsigned long long delay =
@ -311,19 +318,23 @@ std::vector<std::pair<float, int>> AudioEmitter::generateMusic() {
activeChannels.push_back(channelChords); activeChannels.push_back(channelChords);
// Mélodie // Mélodie
std::vector<float> rythme_melodie = rythmes[rand() % rythmes.size()]; if (i >= 4) {
for (float time : rythme_melodie) { int index_rythme = floor(((i - 4) * 1.f / nbr_melo_total) * (rythmes.size() - 1)) + ( rand() % nbr_melo_max ); //Les rythmes deviennent de plus en plus complexe, plus on avance dans le temps, plus le rythme est tiré de la fin du vecteur
FMOD::Channel *channelNote = nullptr; index_rythme = (int)fmin(index_rythme, rythmes.size() - 1);
ERRCHECK(system->playSound(notes[index_note].get(), nullptr, true, std::vector<float> rythme_melodie = rythmes[index_rythme];
&channelNote)); for (float time : rythme_melodie) {
float note_start = (i + time / 8.f) * beatDuration; FMOD::Channel* channelNote = nullptr;
unsigned long long delayNote = ERRCHECK(system->playSound(notes[index_note].get(), nullptr, true,
(unsigned long long)(note_start * sampleRate); &channelNote));
ERRCHECK(channelNote->setDelay(delayNote, 0, true)); float note_start = (i + time / 8.f) * beatDuration;
ERRCHECK(channelNote->setPaused(false)); unsigned long long delayNote =
result.push_back(std::pair<float, int>(note_start, index_note)); (unsigned long long)(note_start * sampleRate);
index_note = nextNote(index_note); ERRCHECK(channelNote->setDelay(delayNote, 0, true));
activeChannels.push_back(channelNote); ERRCHECK(channelNote->setPaused(false));
result.push_back(std::pair<float, int>(note_start, index_note));
index_note = nextNote(index_note);
activeChannels.push_back(channelNote);
}
} }
} }
current_beat += nbr_melo_max; current_beat += nbr_melo_max;

View file

@ -9,12 +9,54 @@ void generateTilePattern(std::vector<std::pair<float, int>> new_notes,
const AudioEmitter &audio_emitter) { const AudioEmitter &audio_emitter) {
int i = -1; int i = -1;
float beatDuration = 60.f / audio_emitter.tempo; float beatDuration = 60.f / audio_emitter.tempo;
int previous_note_pitch = 0;
NotePlaceEnum previous_note_placement = static_cast<NotePlaceEnum>(0);
for (auto note : new_notes) { for (auto note : new_notes) {
i++; i++;
// if (i % 4 != 0) // skip les 3/4 des notes (je suis trop nul sinon)
// continue;
float start_time = note.first; float start_time = note.first;
NotePlaceEnum notePlace = static_cast<NotePlaceEnum>(i % 3); NotePlaceEnum notePlace = static_cast<NotePlaceEnum>(i % 3);
if (i == 0) {
notePlace = static_cast<NotePlaceEnum>(rand()%3);
}
else {
if (note.second == previous_note_pitch) {
notePlace = previous_note_placement;
}
else if (note.second < previous_note_pitch) {
if (previous_note_placement == Left) {
notePlace = Left;
}
else if (previous_note_placement == Middle) {
notePlace = Left;
}
else {
if (rand() % 2 == 0) {
notePlace = Left;
}
else {
notePlace = Middle;
}
}
}
else {
if (previous_note_placement == Right) {
notePlace = Right;
}
else if (previous_note_placement == Middle) {
notePlace = Right;
}
else {
if (rand() % 2 == 0) {
notePlace = Right;
}
else {
notePlace = Middle;
}
}
}
}
previous_note_pitch = note.second;
previous_note_placement = notePlace;
NoteTile::create(start_time, beatDuration, notePlace, audio_emitter.getTime()); NoteTile::create(start_time, beatDuration, notePlace, audio_emitter.getTime());
} }
} }