forked from jkotlinski/lsdpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsdpack.cpp
285 lines (242 loc) · 7.33 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* lsdpack - standalone LSDj (Little Sound Dj) recorder + player {{{
Copyright (C) 2018 Johan Kotlinski
https://www.littlesounddj.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. }}} */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "gambatte.h"
#include "input.h"
#include "writer.h"
#include "dumpwriter.h"
#include "getopt.h"
gambatte::GB gameboy;
Input input;
std::string out_path;
IWriter* writer;
void run_one_frame() {
size_t samples = 35112;
static gambatte::uint_least32_t 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);
if (!gameboy.isCgb() && seconds > 0.1f) {
seconds *= 2; // compensates for slow CPU
}
wait(seconds);
}
bool load_song(int position) {
press(SELECT);
press(SELECT | UP);
press(0);
for (int i = 0; i < 16; ++i) {
press(DOWN); // go to bottom
press(0);
}
press(A);
press(0);
press(A);
press(0);
for (int i = 0; i < 32; ++i) {
press(UP); // go 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()) {
return false;
}
return true;
}
bool sound_enabled;
void play_song() {
int seconds_elapsed = 0;
sound_enabled = false;
writer->record_song_start(out_path.c_str());
press(START);
if (!sound_enabled) {
fputs("Aborted: Song did not start.\n", stderr);
exit(1);
}
while (sound_enabled) {
wait(1);
if (++seconds_elapsed == 60 * 60) {
fputs("Aborted: Song still playing after one hour. Please add a HFF command to song end to stop recording.\n", stderr);
exit(1);
}
}
}
void on_ff_write(char p, char data, unsigned long cycle) {
if (p < 0x10 || p >= 0x40) {
return; // not sound
}
switch (p) {
case 0x26:
if (sound_enabled && !data) {
writer->record_song_stop();
sound_enabled = false;
return;
}
sound_enabled = data;
break;
}
if (sound_enabled) {
writer->record_write(p, data, cycle);
}
}
void on_lcd_interrupt() {
if (sound_enabled) {
writer->record_lcd();
}
}
void make_out_path(const char* in_path, std::string suffix) {
if (strlen(in_path) < strlen("a.gb")) {
return;
}
out_path = in_path;
out_path.replace(out_path.end() - 3, out_path.end(), ""); // strip .gb
out_path += suffix;
out_path.replace(out_path.begin(), out_path.begin() + 1 + out_path.rfind('/'), "");
out_path.replace(out_path.begin(), out_path.begin() + 1 + out_path.rfind('\\'), "");
printf("Recording to '%s'\n", out_path.c_str());
}
void load_gb(const char* path, bool dmg_mode) {
unsigned flags = dmg_mode ? gameboy.FORCE_DMG : 0;
if (gameboy.load(path, flags)) {
fprintf(stderr, "Loading %s failed\n", path);
exit(1);
}
printf("Loaded %s\n", path);
press(0, 3);
/* Press B to skip the first LittleFM screen, if the ROM is patched with it */
press(B);
press(0);
}
void record_gb(int argc, char* argv[], bool dmg_mode) {
make_out_path(argv[optind], ".s");
writer = new Writer(false);
unsigned flags = dmg_mode ? gameboy.FORCE_DMG : 0;
for (; optind < argc; ++optind) {
load_gb(argv[optind], flags);
for (int song_index = 0; song_index < 32; ++song_index) {
if (load_song(song_index)) {
printf("Playing song %i...\n", song_index + 1);
play_song();
}
}
}
writer->write_music_to_disk();
delete writer;
}
void record_gbs(int argc, char* argv[], bool dmg_mode) {
unsigned flags = dmg_mode ? gameboy.FORCE_DMG : 0;
for (; optind < argc; ++optind) {
load_gb(argv[optind], flags);
for (int song_index = 0; song_index < 32; ++song_index) {
writer = new Writer(true);
if (load_song(song_index)) {
printf("Playing song %i...\n", song_index + 1);
char suffix[20];
snprintf(suffix, sizeof(suffix), "-%i.s", song_index + 1);
make_out_path(argv[optind], suffix);
play_song();
writer->write_music_to_disk();
}
delete writer;
}
}
}
void record_dump(int argc, char* argv[], bool dmg_mode) {
writer = new DumpWriter();
unsigned flags = dmg_mode ? gameboy.FORCE_DMG : 0;
for (; optind < argc; ++optind) {
load_gb(argv[optind], flags);
for (int song_index = 0; song_index < 32; ++song_index) {
if (load_song(song_index)) {
printf("Playing song %i...\n", song_index + 1);
char suffix[20];
snprintf(suffix, sizeof(suffix), "-%i.txt", song_index + 1);
make_out_path(argv[optind], suffix);
play_song();
}
}
}
delete writer;
}
void print_help_and_exit() {
puts("usage: lsdpack [-g] [-r] [-d] [lsdj.gb lsdj2.gb ...]");
puts("");
puts("-g: .gbs conversion");
puts("-r: raw register dump; optimizations disabled");
puts("-d: record using emulated DMG (default: CGB)");
exit(1);
}
int main(int argc, char* argv[]) {
bool gbs_mode = false;
bool dump_mode = false;
bool dmg_mode = false;
unsigned flags = 0;
int c;
while ((c = getopt(argc, argv, "grd")) != -1) {
switch (c) {
case 'g':
puts(".gbs mode enabled");
gbs_mode = true;
break;
case 'r':
puts("register dump mode enabled");
dump_mode = true;
break;
case 'd':
puts("recording using emulated DMG");
dmg_mode = true;
break;
default:
print_help_and_exit();
}
}
if (argc <= optind) {
print_help_and_exit();
}
gameboy.setInputGetter(&input);
gameboy.setWriteHandler(on_ff_write);
gameboy.setLcdHandler(on_lcd_interrupt);
if (dmg_mode) {
flags |= gameboy.FORCE_DMG;
}
if (gbs_mode) {
record_gbs(argc, argv, flags);
} else if (dump_mode) {
record_dump(argc, argv, flags);
} else {
record_gb(argc, argv, flags);
}
puts("OK");
}