clangd auto-format

This commit is contained in:
Crizomb 2025-06-12 15:49:07 +02:00
parent a9c8643318
commit 7c95e8c693
4 changed files with 350 additions and 305 deletions

View file

@ -19,6 +19,7 @@ class AudioEmitter {
int index_note;
std::vector<int> chordProgression;
std::vector<FMOD::Channel *> activeChannels;
public:
AudioEmitter();
void generateMusic();

View file

@ -1,9 +1,9 @@
#ifndef BOOK_GAME_HPP
#define BOOK_GAME_HPP
#include "AudioEmitter.hpp"
#include "RoundTarget.hpp"
#include <SFML/Graphics.hpp>
#include "AudioEmitter.hpp"
class Game {
public:

View file

@ -1,8 +1,8 @@
#include "AudioEmitter.hpp"
#include <iostream>
#include <numeric>
#include <random>
#include <string>
#include <numeric>
void ERRCHECK(FMOD_RESULT result) {
if (result != FMOD_OK) {
@ -24,49 +24,72 @@ AudioEmitter::AudioEmitter() {
system.reset(rawSystem);
ERRCHECK(system->init(512, FMOD_INIT_NORMAL, nullptr));
std::vector<FMOD::Sound *> 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]));
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<FMOD::Sound>(rawChords[i]));
}
int nbr_drums = 3;
std::vector<FMOD::Sound *> 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]));
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<FMOD::Sound>(rawDrums[i]));
}
std::vector<FMOD::Sound *> 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]));
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<FMOD::Sound>(rawNotes[i]));
}
index_note = firstNote();
rythmes = {
@ -101,8 +124,7 @@ AudioEmitter::AudioEmitter() {
{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},
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)
@ -114,41 +136,53 @@ AudioEmitter::AudioEmitter() {
// 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}
};
{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},
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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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},
{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}
};
{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);
@ -160,7 +194,8 @@ AudioEmitter::AudioEmitter() {
}
FMOD::Sound *metronome_Sound;
ERRCHECK(system->createSound("media/notes/A1.wav", FMOD_DEFAULT, nullptr, &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));
@ -188,9 +223,11 @@ int sampleIndex(const std::vector<float>& probabilities) {
return dist(gen);
}
int randomWeightedChoice(const std::vector<int>& values, const std::vector<double>& weights) {
int randomWeightedChoice(const std::vector<int> &values,
const std::vector<double> &weights) {
if (values.size() != weights.size() || values.empty()) {
throw std::invalid_argument("Les tableaux doivent être de même taille et non vides.");
throw std::invalid_argument(
"Les tableaux doivent être de même taille et non vides.");
}
// Calcul de la somme totale des poids
@ -227,7 +264,8 @@ int AudioEmitter::nextNote(int currentNote) {
}
int AudioEmitter::noteSecondaire(int note) {
std::vector<int> notesPossibles = { note - 4,note - 3,note + 3,note + 4,note + 5 };
std::vector<int> notesPossibles = {note - 4, note - 3, note + 3, note + 4,
note + 5};
std::vector<double> 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())) {
@ -258,16 +296,19 @@ void AudioEmitter::generateMusic() {
// Chords
FMOD::Channel *channelChords = nullptr;
int index_chord = chordProgression[i % 4];
ERRCHECK(system->playSound(chords[index_chord].get(), nullptr, true, &channelChords));
ERRCHECK(system->playSound(chords[index_chord].get(), nullptr, true,
&channelChords));
unsigned long long delay = (unsigned long long)(i * beatDuration * sampleRate);
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(system->playSound(drums[index_drums].get(), nullptr, true,
&channelDrums));
ERRCHECK(channelDrums->setDelay(delay, 0, true));
ERRCHECK(channelDrums->setPaused(false));
@ -276,14 +317,18 @@ void AudioEmitter::generateMusic() {
std::vector<float> 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(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(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);
@ -295,9 +340,7 @@ void AudioEmitter::generateMusic() {
current_beat += nbr_melo_max;
}
void AudioEmitter::audioUpdate() {
system->update();
}
void AudioEmitter::audioUpdate() { system->update(); }
void AudioEmitter::audioEnd() {
for (FMOD::Channel *c : activeChannels) {

View file

@ -1,7 +1,7 @@
#include "Game.hpp"
#include <string>
#include <iostream>
#include <math.h>
#include <string>
const sf::Time Game::TimePerFrame = sf::seconds(1.f / 60.f);
@ -24,7 +24,8 @@ void Game::run() {
sf::Time elapsedTime = clock.restart();
timeSinceLastUpdate += elapsedTime;
while (timeSinceLastUpdate > TimePerFrame) {
if (32.f - fmod(audioEmitter.getTime(), 32.f) < audioEmitter.timeBeforeNewGeneration) {
if (32.f - fmod(audioEmitter.getTime(), 32.f) <
audioEmitter.timeBeforeNewGeneration) {
if (!generated) {
audioEmitter.generateMusic();
generated = true;