/
lsdpack.cpp
128 lines (112 loc) · 2.6 KB
/
lsdpack.cpp
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
#include <cstdio>
#include <cstdlib>
#include "gambatte.h"
#include "input.h"
#include "writer.h"
int written_songs;
gambatte::GB gameboy;
Input input;
std::string out_path;
void run_one_frame() {
size_t samples = 35112;
long unsigned int audioBuffer[35112 + 2064];
gameboy.runFor(0, 0, &audioBuffer[0], samples);
}
void wait(float seconds) {
for (float i = 0.f; i < 60.f * seconds; ++i) {
run_one_frame();
}
}
void press(unsigned key, float seconds = 0.1f) {
input.press(key);
wait(seconds);
}
void load_song(int position) {
press(SELECT);
press(SELECT | UP);
press(0);
press(DOWN, 3);
press(0);
press(A);
press(0);
press(A);
press(0);
press(UP, 5); // scroll to top
press(0);
for (int i = 0; i < position; ++i) {
press(DOWN);
press(0);
}
// press song name
press(A);
press(0);
// discard changes
press(LEFT);
press(0);
press(A);
// wait until song is loaded
press(0, 5);
if (gameboy.isSongEmpty()) {
write_music_to_disk();
puts("OK");
exit(0);
}
printf("Song %i...\n", ++written_songs);
}
bool sound_enabled;
void play_song() {
sound_enabled = false;
input.press(START);
record_song_start(out_path.c_str());
do {
wait(1);
} while(sound_enabled);
}
void on_ff_write(char p, char data) {
if (p < 0x10 || p >= 0x40) {
return; // not sound
}
switch (p) {
case 0x26:
if (sound_enabled && !data) {
record_song_stop();
sound_enabled = false;
return;
}
sound_enabled = data;
break;
}
if (sound_enabled) {
record_write(p, data);
}
}
void on_lcd_interrupt() {
if (sound_enabled) {
record_lcd();
}
}
void make_out_path(const char* in_path) {
out_path = in_path;
// .gb => .s
out_path.replace(out_path.end() - 2, out_path.end(), "s");
out_path.replace(out_path.begin(), out_path.begin() + out_path.rfind('/') + 1, "");
out_path.replace(out_path.begin(), out_path.begin() + out_path.rfind('\\') + 1, "");
printf("Recording to '%s'\n", out_path.c_str());
}
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: lsdpack <lsdj.gb>");
return 1;
}
gameboy.setInputGetter(&input);
gameboy.setWriteHandler(on_ff_write);
gameboy.setLcdHandler(on_lcd_interrupt);
gameboy.load(argv[1]);
make_out_path(argv[1]);
press(0, 3);
int i = 0;
while (true) {
load_song(i++);
play_song();
}
}