-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi-gui.c
More file actions
395 lines (336 loc) · 14.2 KB
/
Copy pathmidi-gui.c
File metadata and controls
395 lines (336 loc) · 14.2 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
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <timidity.h>
#include <FLAC/stream_encoder.h>
#define ID_BTN_BROWSE 101
#define ID_BTN_RENDER 102
#define ID_CMB_FORMAT 103
#define ID_CMB_RATE 104
#define WM_USER_UPDATE_STATUS (WM_USER + 1)
#define WM_USER_RENDER_DONE (WM_USER + 2)
// Global UI Handles
HWND hMainWnd, hLblFile, hBtnBrowse, hCmbFormat, hCmbRate, hBtnRender, hLblStatus;
char selected_file[MAX_PATH] = "";
// WAV Header structure
#pragma pack(push, 1)
typedef struct {
char riff[4];
int overall_size;
char wave[4];
char fmt_chunk_marker[4];
int length_of_fmt;
short format_type;
short channels;
int sample_rate;
int byterate;
short block_align;
short bits_per_sample;
char data_chunk_header[4];
int data_size;
} WavHeader;
#pragma pack(pop)
void write_wav_header(char* buffer, int pcm_data_size, int sample_rate, int channels, int bits_per_sample) {
WavHeader* h = (WavHeader*)buffer;
memcpy(h->riff, "RIFF", 4);
h->overall_size = pcm_data_size + 36;
memcpy(h->wave, "WAVE", 4);
memcpy(h->fmt_chunk_marker, "fmt ", 4);
h->length_of_fmt = 16;
h->format_type = 1;
h->channels = (short)channels;
h->sample_rate = sample_rate;
h->byterate = sample_rate * channels * (bits_per_sample / 8);
h->block_align = (short)(channels * (bits_per_sample / 8));
h->bits_per_sample = (short)bits_per_sample;
memcpy(h->data_chunk_header, "data", 4);
h->data_size = pcm_data_size;
}
// Memory buffer for FLAC encoding
typedef struct {
char *buffer;
size_t size;
size_t capacity;
} MemoryBuffer;
static FLAC__StreamEncoderWriteStatus flac_write_callback(
const FLAC__StreamEncoder *encoder,
const FLAC__byte buffer[],
size_t bytes,
unsigned samples,
unsigned current_frame,
void *client_data
) {
(void)encoder; (void)samples; (void)current_frame;
MemoryBuffer *mem = (MemoryBuffer *)client_data;
if (mem->size + bytes > mem->capacity) {
mem->capacity = (mem->size + bytes) * 2;
mem->buffer = (char*)realloc(mem->buffer, mem->capacity);
}
memcpy(mem->buffer + mem->size, buffer, bytes);
mem->size += bytes;
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}
// Thread data structure
typedef struct {
char filepath[MAX_PATH];
char target_filepath[MAX_PATH];
int format_is_flac; // 0=WAV, 1=FLAC
int sample_rate;
} RenderData;
DWORD WINAPI RenderThread(LPVOID lpParam) {
RenderData* data = (RenderData*)lpParam;
// Set status
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)"Initializing libtimidity...");
if (mid_init("timidity.cfg") < 0) {
if (mid_init(NULL) < 0) {
if (mid_init_no_config() < 0) {
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)"Error: Failed to initialize libtimidity!");
free(data);
SendMessage(hMainWnd, WM_USER_RENDER_DONE, 0, 0);
return 1;
}
}
}
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)"Reading MIDI file...");
MidIStream* stream = mid_istream_open_file(data->filepath);
if (!stream) {
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)"Error: Failed to open MIDI file.");
free(data);
SendMessage(hMainWnd, WM_USER_RENDER_DONE, 0, 0);
return 1;
}
MidSongOptions options = {0};
options.rate = data->sample_rate;
options.format = MID_AUDIO_S16;
options.channels = 2;
options.buffer_size = data->sample_rate;
MidSong* song = mid_song_load(stream, &options);
if (!song) {
mid_istream_close(stream);
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)"Error: Failed to load song (missing config/patches?).");
free(data);
SendMessage(hMainWnd, WM_USER_RENDER_DONE, 0, 0);
return 1;
}
mid_song_start(song);
// Render loop
size_t pcm_capacity = data->sample_rate * 2 * 2 * 10;
size_t pcm_size = 0;
char* pcm_data = (char*)malloc(pcm_capacity);
char temp_buffer[16384];
int loops = 0;
char status_msg[512];
while (1) {
size_t bytes_read = mid_song_read_wave(song, (sint8*)temp_buffer, sizeof(temp_buffer));
if (bytes_read == 0) {
break;
}
if (pcm_size + bytes_read > pcm_capacity) {
pcm_capacity = pcm_capacity * 2;
pcm_data = (char*)realloc(pcm_data, pcm_capacity);
}
memcpy(pcm_data + pcm_size, temp_buffer, bytes_read);
pcm_size += bytes_read;
loops++;
if (loops % 200 == 0) {
snprintf(status_msg, sizeof(status_msg), "Rendering... %.2f MB", (double)pcm_size / (1024.0 * 1024.0));
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)status_msg);
}
}
mid_song_free(song);
mid_istream_close(stream);
if (data->format_is_flac) {
snprintf(status_msg, sizeof(status_msg), "Encoding to FLAC...");
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)status_msg);
MemoryBuffer flac_mem = { (char*)malloc(8192), 0, 8192 };
FLAC__StreamEncoder *encoder = FLAC__stream_encoder_new();
FLAC__stream_encoder_set_channels(encoder, 2);
FLAC__stream_encoder_set_bits_per_sample(encoder, 16);
FLAC__stream_encoder_set_sample_rate(encoder, data->sample_rate);
FLAC__stream_encoder_init_stream(
encoder,
flac_write_callback,
NULL, NULL, NULL,
&flac_mem
);
unsigned frames = pcm_size / 4;
FLAC__int32 *flac_samples = (FLAC__int32*)malloc(frames * 2 * sizeof(FLAC__int32));
sint16 *pcm_16 = (sint16*)pcm_data;
for(unsigned i = 0; i < frames * 2; i++) {
flac_samples[i] = pcm_16[i];
}
FLAC__stream_encoder_process_interleaved(encoder, flac_samples, frames);
FLAC__stream_encoder_finish(encoder);
FLAC__stream_encoder_delete(encoder);
free(flac_samples);
// Save FLAC to file
FILE* f = fopen(data->target_filepath, "wb");
if(f) {
fwrite(flac_mem.buffer, 1, flac_mem.size, f);
fclose(f);
}
free(flac_mem.buffer);
} else {
snprintf(status_msg, sizeof(status_msg), "Saving WAV file...");
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)status_msg);
int wav_total_size = sizeof(WavHeader) + pcm_size;
char* wav_buffer = (char*)malloc(wav_total_size);
write_wav_header(wav_buffer, pcm_size, data->sample_rate, 2, 16);
memcpy(wav_buffer + sizeof(WavHeader), pcm_data, pcm_size);
FILE* f = fopen(data->target_filepath, "wb");
if(f) {
fwrite(wav_buffer, 1, wav_total_size, f);
fclose(f);
}
free(wav_buffer);
}
free(pcm_data);
snprintf(status_msg, sizeof(status_msg), "Success! Saved as:\n%s", data->target_filepath);
SendMessage(hMainWnd, WM_USER_UPDATE_STATUS, 0, (LPARAM)status_msg);
SendMessage(hMainWnd, WM_USER_RENDER_DONE, 0, 0);
free(data);
mid_exit(); // Cleanup libtimidity for this render
return 0;
}
// Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
HFONT hFont = CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS, "Segoe UI");
CreateWindow("STATIC", "Select MIDI File:", WS_VISIBLE | WS_CHILD, 20, 20, 150, 20, hwnd, NULL, NULL, NULL);
hLblFile = CreateWindow("STATIC", "No file selected", WS_VISIBLE | WS_CHILD | SS_LEFT, 20, 45, 300, 20, hwnd, NULL, NULL, NULL);
hBtnBrowse = CreateWindow("BUTTON", "Browse...", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 330, 40, 100, 30, hwnd, (HMENU)ID_BTN_BROWSE, NULL, NULL);
CreateWindow("STATIC", "Output Format:", WS_VISIBLE | WS_CHILD, 20, 80, 150, 20, hwnd, NULL, NULL, NULL);
hCmbFormat = CreateWindow("COMBOBOX", "", WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST, 20, 100, 200, 100, hwnd, (HMENU)ID_CMB_FORMAT, NULL, NULL);
SendMessage(hCmbFormat, CB_ADDSTRING, 0, (LPARAM)"WAV (Lossless)");
SendMessage(hCmbFormat, CB_ADDSTRING, 0, (LPARAM)"FLAC (Compressed)");
SendMessage(hCmbFormat, CB_SETCURSEL, 0, 0);
CreateWindow("STATIC", "Sample Rate:", WS_VISIBLE | WS_CHILD, 230, 80, 150, 20, hwnd, NULL, NULL, NULL);
hCmbRate = CreateWindow("COMBOBOX", "", WS_VISIBLE | WS_CHILD | CBS_DROPDOWNLIST, 230, 100, 200, 100, hwnd, (HMENU)ID_CMB_RATE, NULL, NULL);
SendMessage(hCmbRate, CB_ADDSTRING, 0, (LPARAM)"44100 Hz");
SendMessage(hCmbRate, CB_ADDSTRING, 0, (LPARAM)"48000 Hz");
SendMessage(hCmbRate, CB_ADDSTRING, 0, (LPARAM)"96000 Hz");
SendMessage(hCmbRate, CB_SETCURSEL, 0, 0);
hBtnRender = CreateWindow("BUTTON", "Render Audio", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 20, 150, 410, 40, hwnd, (HMENU)ID_BTN_RENDER, NULL, NULL);
hLblStatus = CreateWindow("STATIC", "Status: Ready", WS_VISIBLE | WS_CHILD | SS_LEFT, 20, 205, 410, 40, hwnd, NULL, NULL, NULL);
// Set font manually for all controls
SendMessage(hLblFile, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hBtnBrowse, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hCmbFormat, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hCmbRate, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hBtnRender, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hLblStatus, WM_SETFONT, (WPARAM)hFont, TRUE);
break;
}
case WM_COMMAND: {
if (LOWORD(wParam) == ID_BTN_BROWSE) {
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = selected_file;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(selected_file);
ofn.lpstrFilter = "MIDI Files\0*.mid;*.midi\0All Files\0*.*\0";
ofn.nFilterIndex = 1;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
if (GetOpenFileName(&ofn) == TRUE) {
SetWindowText(hLblFile, selected_file);
}
}
else if (LOWORD(wParam) == ID_BTN_RENDER) {
if (strlen(selected_file) == 0) {
MessageBox(hwnd, "Please select a MIDI file first.", "Error", MB_ICONWARNING);
break;
}
int format_is_flac = SendMessage(hCmbFormat, CB_GETCURSEL, 0, 0);
char save_file[MAX_PATH] = "";
OPENFILENAME sfn;
ZeroMemory(&sfn, sizeof(sfn));
sfn.lStructSize = sizeof(sfn);
sfn.hwndOwner = hwnd;
sfn.lpstrFile = save_file;
sfn.nMaxFile = sizeof(save_file);
if (format_is_flac) {
sfn.lpstrFilter = "FLAC Audio\0*.flac\0All Files\0*.*\0";
sfn.lpstrDefExt = "flac";
} else {
sfn.lpstrFilter = "WAV Audio\0*.wav\0All Files\0*.*\0";
sfn.lpstrDefExt = "wav";
}
sfn.nFilterIndex = 1;
sfn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR;
if (GetSaveFileName(&sfn) == FALSE) {
break; // User cancelled
}
// Disable UI
EnableWindow(hBtnBrowse, FALSE);
EnableWindow(hCmbFormat, FALSE);
EnableWindow(hCmbRate, FALSE);
EnableWindow(hBtnRender, FALSE);
RenderData* data = (RenderData*)malloc(sizeof(RenderData));
strcpy(data->filepath, selected_file);
strcpy(data->target_filepath, save_file);
data->format_is_flac = format_is_flac;
int rate_idx = SendMessage(hCmbRate, CB_GETCURSEL, 0, 0);
if (rate_idx == 0) data->sample_rate = 44100;
else if (rate_idx == 1) data->sample_rate = 48000;
else if (rate_idx == 2) data->sample_rate = 96000;
CreateThread(NULL, 0, RenderThread, data, 0, NULL);
}
break;
}
case WM_USER_UPDATE_STATUS: {
SetWindowText(hLblStatus, (const char*)lParam);
break;
}
case WM_USER_RENDER_DONE: {
EnableWindow(hBtnBrowse, TRUE);
EnableWindow(hCmbFormat, TRUE);
EnableWindow(hCmbRate, TRUE);
EnableWindow(hBtnRender, TRUE);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
(void)hPrevInstance; (void)lpCmdLine;
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icex);
const char CLASS_NAME[] = "MidiStudioGUI";
WNDCLASS wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
hMainWnd = CreateWindowEx(
0, CLASS_NAME, "MIDI Studio (Native Renderer)",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 470, 300,
NULL, NULL, hInstance, NULL
);
if (hMainWnd == NULL) return 0;
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}