-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
730 lines (622 loc) · 27.9 KB
/
Copy pathmain.cpp
File metadata and controls
730 lines (622 loc) · 27.9 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/*
mah_tracker: AArt1256's custom SID chiptune tracker
Copyright (C) 2026 AArt1256
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, see
<https://www.gnu.org/licenses/>.
*/
#define GL_GLEXT_PROTOTYPES
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
#include "imgui_internal.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <SDL.h>
#include <sndfile.hh>
#include <filesystem>
#include <vector>
#include "portable-file-dialogs.h"
#include "defines.h"
#include "defines_ui.h"
namespace fs = std::filesystem;
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL_opengles2.h>
// TODO: add glext include here
#else
#include <SDL_opengl.h>
#endif
// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
#ifdef __EMSCRIPTEN__
#include "../libs/emscripten/emscripten_mainloop_stub.h"
#endif
#define rightClickable if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) ImGui::SetKeyboardFocusHere(-1);
void save_settings();
void load_settings();
struct window_bool {
float audio_volume;
bool settings;
bool imgui_debugger;
bool pattern;
bool controls;
bool orders;
bool instr;
bool reg_view;
bool filter_view;
};
struct window_bool visible_windows;
static bool opt_padding = false; // Is there padding (a blank space) between the window edge and the Dockspace?
extern void init_player_freq_table(uint16_t a_freq);
extern void load_file(char *filename, song *song);
extern void save_file(char *filename, song *song);
extern void reset_audio_buffer();
extern uint8_t *get_player_speeds();
cursor cur_cursor;
song c_song; // current song
std::vector<undo_chunk> undo_total;
extern bool audio_paused;
void init_default_song(song *song) {
for (int pat = 0; pat < 256; pat++) {
for (int row = 0; row < 256; row++) {
song->pattern[pat].rows[row].note = NOTE_EMPTY;
song->pattern[pat].rows[row].instr = 0;
song->pattern[pat].rows[row].eff_type = 0;
song->pattern[pat].rows[row].eff_arg = 0;
}
song->order_table[0][pat] = 0;
song->order_table[1][pat] = 0;
song->order_table[2][pat] = 0;
}
// Default instrument settings for freshly created instrument.
// 0xFF (128) instruments pre-populated in the editor list
// >chiptune writers making the best sounds you've ever heard and they're all named "new instrument"
for (int ins = 0; ins < 128; ins++) {
// ADSR
song->instr[ins].a = 0x0;
song->instr[ins].d = 0x8;
song->instr[ins].s = 0x0;
song->instr[ins].r = 0x0;
// Arp/Waveform Length
// INS_NO_LOOP is 0xFF (see defines.h)
song->instr[ins].wav_len = 0x1;
song->instr[ins].wav_loop = INS_NO_LOOP;
memset(song->instr[ins].wav,0x01,128);
memset(song->instr[ins].arp,48,128);
// 0x21 in SID parlance is sawtooth
song->instr[ins].wav[0] = 0x21;
// No filter by default
song->instr[ins].filter_enable = 0;
song->instr[ins].filter_res = 0;
song->instr[ins].filter_len = 0;
song->instr[ins].filter_sweep_mode = false; // absolute sweep
song->instr[ins].filter_loop = INS_NO_LOOP;
song->instr[ins].filter_init_cutoff = 0;
memset(song->instr[ins].filter,0,128);
memset(song->instr[ins].filter_mode,0,128);
// Duty settings
song->instr[ins].duty_start = 0x800;
song->instr[ins].duty_end = 0x800;
song->instr[ins].duty_speed = 0;
song->instr[ins].duty_reset = true;
}
// 00 01
// initial order table: 00 END
song->order_table[0][0] = 0x00;
song->order_table[1][0] = 0x01;
song->order_table[2][0] = 0x02;
song->order_len = 1;
song->order_loop = 0;
song->row_length = 64;
song->init_speed = 6;
song->pitch_bend_shift = 0;
song->a_frequency = 440;
}
float get_bpm(float hz, int speed_a, int speed_b) {
float avg_speed = (((float)speed_a)+((float)speed_b))/2.0f;
// NOTE: 4 is the number of rows between each highlight per beat
return 60.0f*hz/(4.0f*avg_speed);
}
void ShowExampleAppDockSpace(bool* p_open) {
ImGui::GetIO().ConfigWindowsMoveFromTitleBarOnly = true; // Don't know if there's a better way to do this right now
// Variables to configure the Dockspace example.
static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None; // Config flags for the Dockspace
// In this example, we're embedding the Dockspace into an invisible parent window to make it more configurable.
// We set ImGuiWindowFlags_NoDocking to make sure the parent isn't dockable into because this is handled by the Dockspace.
//
// ImGuiWindowFlags_MenuBar is to show a menu bar with config options. This isn't necessary to the functionality of a
// Dockspace, but it is here to provide a way to change the configuration flags interactively.
// You can remove the MenuBar flag if you don't want it in your app, but also remember to remove the code which actually
// renders the menu bar, found at the end of this function.
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
// Is the example in Fullscreen mode?
{
// If so, get the main viewport:
const ImGuiViewport* viewport = ImGui::GetMainViewport();
// Set the parent window's position, size, and viewport to match that of the main viewport. This is so the parent window
// completely covers the main viewport, giving it a "full-screen" feel.
ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize);
ImGui::SetNextWindowViewport(viewport->ID);
// Set the parent window's styles to match that of the main viewport:
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); // No corner rounding on the window
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); // No border around the window
// Manipulate the window flags to make it inaccessible to the user (no titlebar, resize/move, or navigation)
window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
}
// When using ImGuiDockNodeFlags_PassthruCentralNode, DockSpace() will render our background
// and handle the pass-thru hole, so the parent window should not have its own background:
if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode)
window_flags |= ImGuiWindowFlags_NoBackground;
// If the padding option is disabled, set the parent window's padding size to 0 to effectively hide said padding.
if (!opt_padding)
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
// Important: note that we proceed even if Begin() returns false (aka window is collapsed).
// This is because we want to keep our DockSpace() active. If a DockSpace() is inactive,
// all active windows docked into it will lose their parent and become undocked.
// We cannot preserve the docking relationship between an active window and an inactive docking, otherwise
// any change of dockspace/settings would lead to windows being stuck in limbo and never being visible.
ImGui::Begin("DockSpace Demo", p_open, window_flags);
// Remove the padding configuration - we pushed it, now we pop it:
if (!opt_padding)
ImGui::PopStyleVar();
// Pop the two style rules set in Fullscreen mode - the corner rounding and the border size.
ImGui::PopStyleVar(2);
// Check if Docking is enabled:
ImGuiIO& io = ImGui::GetIO();
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable)
{
// If it is, draw the Dockspace with the DockSpace() function.
// The GetID() function is to give a unique identifier to the Dockspace - here, it's "MyDockSpace".
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
}
else
{
// Docking is DISABLED - Show a warning message
}
// This is to show the menu bar that will change the config settings at runtime.
// If you copied this demo function into your own code and removed ImGuiWindowFlags_MenuBar at the top of the function,
// you should remove the below if-statement as well.
if (ImGui::BeginMenuBar())
{
if (ImGui::BeginMenu("file")) {
// Disabling fullscreen would allow the window to be moved to the front of other windows,
// which we can't undo at the moment without finer window depth/z control.
if (ImGui::MenuItem("new file")) {
cur_cursor.new_file_popup = true;
reset_audio_buffer();
}
if (ImGui::MenuItem("open file")) {
audio_paused = true;
auto f = pfd::open_file("Choose a file to read", pfd::path::home(),
{ "Mah-Tracker Files", "*.mah" },
pfd::opt::none).result();
if (!f.empty()) {
fs::path path_check = f[0];
if (path_check.extension() != ".mah")
path_check.replace_extension("mah");
load_file((char *)path_check.string().c_str(), &c_song);
}
audio_paused = false;
reset_audio_buffer();
}
if (ImGui::MenuItem("save file")) {
audio_paused = true;
auto f = pfd::save_file("Choose a file to write to", pfd::path::home(),
{ "Mah-Tracker Files", "*.mah" },
pfd::opt::none).result();
if (!f.empty()) {
fs::path path_check = f;
if (path_check.extension() != ".mah")
path_check.replace_extension("mah");
save_file((char *)path_check.string().c_str(), &c_song);
}
audio_paused = false;
reset_audio_buffer();
}
ImGui::EndMenu();
}
ImGui::MenuItem("settings", NULL, (bool *)&visible_windows.settings);
if (ImGui::BeginMenu("windows")) {
ImGui::MenuItem("ImGui Debugger", NULL, (bool *)&visible_windows.imgui_debugger);
ImGui::MenuItem("Pattern", NULL, (bool *)&visible_windows.pattern);
ImGui::MenuItem("Controls", NULL, (bool *)&visible_windows.controls);
ImGui::MenuItem("Orders", NULL, (bool *)&visible_windows.orders);
ImGui::MenuItem("Instrument Editor", NULL, (bool *)&visible_windows.instr);
ImGui::MenuItem("Register View", NULL, (bool *)&visible_windows.reg_view);
ImGui::MenuItem("Filter & Channel Info", NULL, (bool *)&visible_windows.filter_view);
ImGui::EndMenu();
}
if (cur_cursor.playing) {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0x80,0x80,0x80,0xff));
uint8_t *speeds = get_player_speeds();
if (speeds[0] != speeds[1]) ImGui::Text("| Speed %d:%d", speeds[0], speeds[1]);
else ImGui::Text("| Speed %d", speeds[0]);
ImGui::SameLine();
ImGui::Text("(%.2f BPM) | Order %02X/%02X | Row %02X/%02X", get_bpm(50.0f, speeds[0], speeds[1]), cur_cursor.order, c_song.order_len-1, cur_cursor.row, c_song.row_length);
ImGui::PopStyleColor();
}
ImGui::EndMenuBar();
}
// End the parent window that contains the Dockspace:
ImGui::End();
}
// save settings
void save_settings() {
ImGuiIO& io = ImGui::GetIO();
std::string filename = "mah-tracker.cfg";
std::ofstream f(filename);
if (!f) return;
std::string value;
f << io.FontGlobalScale << "\n";
for (int i = 0; i < sizeof(visible_windows); i++) {
f << (int)(((uint8_t*)&visible_windows)[i]) << "\n";
}
}
// load settings
void load_settings() {
ImGuiIO& io = ImGui::GetIO();
std::string filename = "mah-tracker.cfg";
std::ifstream f(filename);
if (!f) return;
std::string value;
getline(f,value); if (!value.empty()) io.FontGlobalScale = std::stof(value);
for (int i = 0; i < sizeof(visible_windows); i++) {
getline(f,value); if (!value.empty()) ((uint8_t*)&visible_windows)[i] = std::stoi(value);
}
}
extern void init_sid(); // player.cpp
extern void advance_audio(song *song, cursor *cur_cursor); // player.cpp
extern int advance_sample(song *song, cursor *cur_cursor, int16_t *buffer, int buffer_length); // player.cpp
extern int player_get_loop_cnt(); // player.cpp
extern void init_routine(song *song); // player.cpp
extern void register_view(bool *open);
extern void display_filter_info(cursor *cur_cursor, bool *open);
extern void SID_set_chip(bool mode); // player.cpp
float get_volume() { // for player.cpp
return visible_windows.audio_volume;
}
// Main code
int main(int argc, char *argv[]) {
visible_windows.imgui_debugger = false;
visible_windows.controls = true;
visible_windows.instr = true;
visible_windows.orders = true;
visible_windows.pattern = true;
visible_windows.settings = true;
visible_windows.audio_volume = 1.0f;
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
return -1;
}
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100 (WebGL 1.0)
const char* glsl_version = "#version 100";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(IMGUI_IMPL_OPENGL_ES3)
// GL ES 3.0 + GLSL 300 es (WebGL 2.0)
const char* glsl_version = "#version 300 es";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(__APPLE__)
// GL 3.2 Core + GLSL 150
const char* glsl_version = "#version 150";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
// From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
// Create window with graphics context
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI| SDL_WINDOW_MAXIMIZED);
SDL_Window* window = SDL_CreateWindow("Mah-Tracker -- Under Construction", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); // mah tracker baybeee!!11
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1;
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
if (gl_context == nullptr)
{
printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
return -1;
}
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("res/fontawesome.otf", 12.0f);
//io.Fonts->AddFontFromFileTTF("IBMPlexMono-Regular.ttf", 25.0f); // FOINISS FONT OH MAI GAHHHH
io.FontGlobalScale = 1.0f;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight(); What kind of soulless monster would use a LIGHT THEME?!
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
// Our state
ImVec4 clear_color = ImVec4(0.45f, 0.45f, 0.45f, 1.00f);
if (!std::filesystem::exists("imgui.ini")) reset_layout();
load_settings();
save_settings(); // for updating config files from older versions
// Define some variables for cursor operations
cur_cursor.ch = 0;
cur_cursor.row = 0;
cur_cursor.selection = note;
cur_cursor.octave = 3;
cur_cursor.latch = 0;
cur_cursor.order = 0;
cur_cursor.instr = 1;
cur_cursor.playing = 0;
cur_cursor.play_row = 0;
cur_cursor.loop = false;
cur_cursor.do_record = false; // jam
cur_cursor.do_follow = true; // enable follow-play
cur_cursor.new_file_popup = false;
cur_cursor.chip_mode = true; // 8580 SID
cur_cursor.dragging = false;
cur_cursor.already_dragged = false;
cur_cursor.is_muted[0] = false;
cur_cursor.is_muted[1] = false;
cur_cursor.is_muted[2] = false;
cur_cursor.pattern_copy_buffer.row_len = 0;
cur_cursor.pattern_copy_buffer.col_start = 0;
cur_cursor.pattern_copy_buffer.col_len = 0;
for (int ch = 0; ch < 3; ch++) {
for (int row = 0; row < 256; row++) {
cur_cursor.pattern_copy_buffer.ch_rows[ch].rows[row].note = NOTE_EMPTY;
cur_cursor.pattern_copy_buffer.ch_rows[ch].rows[row].instr = 0;
cur_cursor.pattern_copy_buffer.ch_rows[ch].rows[row].eff_arg = 0;
cur_cursor.pattern_copy_buffer.ch_rows[ch].rows[row].eff_type = 0;
}
}
cur_cursor.undo_pos = 0;
// Main loop
bool done = false;
int cur_frame = 0;
init_default_song(&c_song);
// process args in argv
int argc_pos = 1;
bool file_opened_args = false;
bool save_wav_args = false;
std::string save_wav_str = "";
while (argc_pos < argc) {
std::string arg_str = std::string(argv[argc_pos]);
if (arg_str == "-o") {
argc_pos++;
save_wav_str = std::string(argv[argc_pos]);
fs::path path_check = save_wav_str;
if (path_check.extension() == ".wav") {
printf("save .wav audio file %s\n",save_wav_str.c_str());
save_wav_args = true;
}
} else if (!file_opened_args) {
file_opened_args = true;
printf("loaded file %s\n",argv[argc_pos]);
load_file(argv[argc_pos], &c_song);
}
argc_pos++;
}
audio_paused = false;
init_sid();
init_routine(&c_song);
if (save_wav_args) {
SndfileHandle wav_file = SndfileHandle(save_wav_str, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 1, SAMPLE_RATE);
cur_cursor.playing = 1;
cur_cursor.play_row = 0;
cur_cursor.latch = 0;
int16_t sample_buf[1024];
while (player_get_loop_cnt() < 1) {
memset(sample_buf, 0, 1024*sizeof(int16_t));
int samp_len = advance_sample(&c_song, &cur_cursor, sample_buf, 1024);
wav_file.write(sample_buf, samp_len);
}
done = true;
}
#ifdef __EMSCRIPTEN__
// For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
// You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
io.IniFilename = nullptr;
EMSCRIPTEN_MAINLOOP_BEGIN
#else
// Main loop
while (!done) {
#endif
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
switch (event.type) {
case SDL_QUIT:
done = true;
break;
}
}
advance_audio(&c_song,&cur_cursor);
cur_frame++;
if (cur_frame == (50*60)) { // 3000?
cur_frame = 0; //I would guess this is out-of-bounds protection
save_settings();
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
ShowExampleAppDockSpace((bool*)false);
if (cur_cursor.order >= c_song.order_len)
cur_cursor.order = c_song.order_len-1;
if (cur_cursor.new_file_popup) {
ImGui::OpenPopup("Warning");
if (ImGui::BeginPopupModal("Warning")) {
ImGui::SetItemDefaultFocus();
ImGui::TextUnformatted("Are you sure you want to create a new song?");
if (ImGui::Button("Yes")) {
init_default_song(&c_song);
init_routine(&c_song);
cur_cursor.latch = 0;
cur_cursor.chip_mode = true;
SID_set_chip(cur_cursor.chip_mode);
cur_cursor.new_file_popup = false;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("No")) {
cur_cursor.new_file_popup = false;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
if (visible_windows.settings) {
// Settings
ImGui::Begin("Settings",(bool *)&visible_windows.settings);
ImGui::SliderFloat("UI Scaling Factor", &io.FontGlobalScale, 0.5f, 3.0f); rightClickable
ImGui::SliderFloat("Master Audio Volume", &visible_windows.audio_volume, 0.0f, 3.0f); rightClickable
ImGui::Separator();
ImGui::MenuItem("Padding", NULL, &opt_padding);
ImGui::Separator();
if (ImGui::MenuItem("Save Settings Manually")) save_settings();
if (ImGui::MenuItem("Load Settings Manually")) load_settings();
ImGui::Separator();
ImGui::End();
}
if (visible_windows.pattern) {
render_pat(&c_song,&cur_cursor,&undo_total,(bool *)&visible_windows.pattern);
}
if (visible_windows.controls) {
ImGui::Begin("Controls", (bool *)&visible_windows.controls);
//ImGui::SetNextItemWidth(ImGui::GetWindowSize().x * 0.2f);
uint8_t one = 1;
uint16_t one_16 = 1;
// Out-of-bounds protection for octave and speed.
if (ImGui::InputInt("Octave",&cur_cursor.octave)) {
if (cur_cursor.octave < 0) cur_cursor.octave = 0; // put it back to 0
else if (cur_cursor.octave > 7) cur_cursor.octave = 7; // put it back to 7
}
ImGui::Checkbox("Follow Pattern",&cur_cursor.do_follow);
ImGui::Checkbox("Loop Pattern",&cur_cursor.loop);
ImGui::Separator();
// Toggle cursor state between record and jam
if (ImGui::Button(cur_cursor.do_record?"Jam":"Record")) {
cur_cursor.do_record = !cur_cursor.do_record;
}
ImGui::SameLine();
// Play controls
if (ImGui::Button(cur_cursor.playing?"Stop":"Play")) {
cur_cursor.playing ^= 1; // Xor toggle
cur_cursor.play_row = 0;
cur_cursor.latch = 0;
init_routine(&c_song);
}
ImGui::SameLine();
if (cur_cursor.playing) {
uint8_t *speeds = get_player_speeds();
ImGui::Text("%.2f BPM", get_bpm(50.0f, speeds[0], speeds[1]));
} else {
ImGui::Text("%.2f BPM", get_bpm(50.0f, c_song.init_speed, c_song.init_speed));
}
ImGui::Separator();
if (ImGui::InputScalar("Speed",ImGuiDataType_U8,&c_song.init_speed,&one)) {
if (c_song.init_speed < 1) c_song.init_speed = 1; // No speedcore for you
else if (c_song.init_speed > 127) c_song.init_speed = 127; // If you need more than 127 speed there's something wrong
}
if (ImGui::InputScalar("Pitch Bend Shift",ImGuiDataType_U8,&c_song.pitch_bend_shift,&one)) {
if (c_song.pitch_bend_shift < 0) c_song.pitch_bend_shift = 0;
else if (c_song.pitch_bend_shift > 3) c_song.pitch_bend_shift = 3;
}
if (ImGui::InputScalar("Tuning",ImGuiDataType_U16,&c_song.a_frequency,&one_16)) {
if (c_song.a_frequency < 20) c_song.a_frequency = 20;
else if (c_song.a_frequency > 32767) c_song.a_frequency = 32767;
init_player_freq_table(c_song.a_frequency);
}
ImGui::Text("SID model select");
ImGui::SameLine();
if (ImGui::Button(cur_cursor.chip_mode?"8580 (new)":"6581 (old)")) {
cur_cursor.chip_mode = !cur_cursor.chip_mode;
SID_set_chip(cur_cursor.chip_mode);
}
if (ImGui::InputScalar("Order Loop",ImGuiDataType_U8,&c_song.order_loop,&one)) {
if (c_song.order_loop < 0) c_song.order_loop = 0;
else if (c_song.order_loop >= c_song.order_len) c_song.order_loop = c_song.order_len-1;
}
if (ImGui::InputScalar("Pattern Length",ImGuiDataType_U16,&c_song.row_length,&one_16)) {
if (c_song.row_length < 0) c_song.row_length = 0;
else if (c_song.row_length >= 256) c_song.row_length = 256;
}
//ImGui::Dummy(ImVec2(ImGui::GetContentRegionAvail().x/20.0,0.0f));
//ImGui::NewLine();
ImGui::SameLine();
ImGui::End();
}
// Checking if windows are visible, if not, then don't waste time rendering them
if (visible_windows.orders) {
render_orders(&c_song,&cur_cursor,(bool *)&visible_windows.pattern);
}
if (visible_windows.instr) {
render_instr(&c_song,&cur_cursor,(bool *)&visible_windows.instr);
}
if (visible_windows.reg_view) {
register_view(&visible_windows.reg_view);
}
if (visible_windows.filter_view) {
display_filter_info(&cur_cursor,&visible_windows.filter_view);
}
if (visible_windows.imgui_debugger) {
ImGui::ShowMetricsWindow((bool *)&visible_windows.imgui_debugger);
}
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_MAINLOOP_END;
#endif
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}