-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (119 loc) · 4.47 KB
/
main.cpp
File metadata and controls
139 lines (119 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "raylib.h"
#include <array>
#include <string>
#include <vector>
#include <iostream>
#include <fluidsynth.h>
#ifndef FLUID_PLAYER_TEMPO_EXTERNAL_BPM
#define FLUID_PLAYER_TEMPO_EXTERNAL_BPM 1
#endif
#include <fstream>
#include <CoreFoundation/CoreFoundation.h>
#include <filesystem>
#include "utils/MidiUtils.h"
#include "ui/PianoKey.h"
#include "utils/SongInfo.h"
#include "utils/SoundFontUtils.h"
#include "ui/PianoPage.h"
#include "ui/MainMenuPage.h"
constexpr bool showKeyLabels = true;
// Add this at global scope in main.cpp (outside any function)
std::vector<std::vector<bool> > midiKeyStates(16, std::vector<bool>(NUM_TOTAL_KEYS, false));
enum class AppPage { MainMenu, Piano };
int main() {
// --- FluidSynth and MIDI setup ---
fluid_settings_t *settings = new_fluid_settings();
fluid_synth_t *synth = new_fluid_synth(settings);
fluid_audio_driver_t *adriver = new_fluid_audio_driver(settings, synth);
std::string soundFontDir = std::string(getenv("HOME")) + "/Documents/Sonique/soundFonts";
if (!EnsureSoundFontDir(soundFontDir)) {
return 0;
}
std::vector<std::string> loadedSoundFonts = ScanSoundFonts(soundFontDir);
int general = LoadSoundFont(synth, soundFontDir + "/general.sf2", 1);
for (int i = 1; i < 16; ++i) {
if (i == 9) continue;
fluid_synth_program_select(synth, i, general, 0, 0);
}
std::vector<std::string> loadedMidiFiles;
std::string midiDir = std::string(getenv("HOME")) + "/Documents/Sonique/midi";
namespace fs = std::filesystem;
if (!fs::exists(midiDir)) {
fs::create_directories(midiDir);
std::cerr << "Created MIDI directory at: " << midiDir << std::endl;
return 0;
}
for (const auto &entry: fs::directory_iterator(midiDir)) {
if (entry.is_regular_file()) {
std::string path = entry.path().string();
if (path.size() >= 4 &&
(path.substr(path.size() - 4) == ".mid" || path.substr(path.size() - 4) == ".MID")) {
loadedMidiFiles.push_back(path);
}
}
}
std::vector<SongInfo> songInfos = LoadSongInfos(std::string(getenv("HOME")) + "/Documents/Sonique/songinfo");
std::vector<SongInfo> loadedSongInfos;
for (const auto &midiPath: loadedMidiFiles) {
std::string midiFile = midiPath.substr(midiPath.find_last_of('/') + 1);
auto it = std::find_if(songInfos.begin(), songInfos.end(), [&](const SongInfo &s) {
return s.midiFile == midiFile;
});
if (it != songInfos.end()) {
loadedSongInfos.push_back(*it);
} else {
loadedSongInfos.push_back({midiFile, midiFile, "Unknown"});
}
}
if (loadedMidiFiles.empty()) {
std::cerr << "No MIDI files found in directory: " << midiDir << std::endl;
return 1;
}
std::vector<int> midiBpms;
for (const auto &path: loadedMidiFiles) {
int bpm = GetMidiInitialTempoBPM(path);
midiBpms.push_back(bpm > 0 ? bpm : 120);
}
// --- MIDI player setup ---
for (int ch = 0; ch < 16; ++ch) {
for (int k = 0; k < 88; ++k) {
midiKeyStates[ch][k] = false;
}
}
int currentSongIndex = 0;
fluid_player_t *player = new_fluid_player(synth);
fluid_player_add(player, loadedMidiFiles[currentSongIndex].c_str());
fluid_player_set_playback_callback(player, midi_event_handler, synth);
int tempo = midiBpms[currentSongIndex];
fluid_player_set_tempo(player, FLUID_PLAYER_TEMPO_EXTERNAL_BPM, tempo);
// --- Window and UI ---
const int initialWidth = 1220;
const int initialHeight = 800;
InitWindow(initialWidth, initialHeight, "Sonique");
SetWindowState(FLAG_WINDOW_RESIZABLE);
AppPage currentPage = AppPage::MainMenu;
PianoPage pianoPage(
synth, player, loadedMidiFiles, loadedSongInfos, midiBpms, midiKeyStates
);
MainMenuPage mainMenu([&]() { currentPage = AppPage::Piano; });
while (!WindowShouldClose()) {
switch (currentPage) {
case AppPage::MainMenu:
mainMenu.HandleInput();
mainMenu.Update();
mainMenu.Draw();
break;
case AppPage::Piano:
pianoPage.HandleInput();
pianoPage.Update();
pianoPage.Draw();
break;
}
}
// --- Cleanup ---
delete_fluid_audio_driver(adriver);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
CloseWindow();
return 0;
}