aboutsummaryrefslogtreecommitdiff
path: root/win32/configdialog.c
blob: 21c9c625bde20874bf74efcaff19591960c6bd95 (plain)
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
#include "configdialog.h"
#include <windowsx.h>
#include <commctrl.h>
#include <math.h>
#include <wchar.h>
#include <stdlib.h>

static double mix_to_db(uint32_t mix) {
  return 20.0 * log10((double)mix / 0x10000);
}

static uint32_t db_to_mix(double db) {
  return round(pow(10.0, db / 20.0) * 0x10000);
}

static struct {
  HINSTANCE hinst;
  HWND configwnd;
  ATOM config_class;
  HFONT font;
  void (*closecb)(void *);
  void *closecbptr;
  void (*changecb)(void *);
  void *changecbptr;
  HWND group_fm, group_ssg, group_ppz8;
  HWND check_fm_hires_sin;
  HWND check_fm_hires_env;
  HWND radio_ssg_opna;
  HWND radio_ssg_ymf288;
  HWND static_volume_offset, static_volume_info;
  HWND edit_ssg_mix;
  HWND radio_ppz8_none, radio_ppz8_linear, radio_ppz8_sinc;
  HWND updown_ssg_mix;
  WNDPROC groupbox_defproc;
  bool edit_ssg_mix_set;
  double ssg_mix_db;
} g;

enum {
  WIN_W = 500,
  WIN_H = 370,
  GROUP_X = 5,
  GROUP_FM_Y = 5,
  GROUP_W = 490,
  GROUP_FM_H = 75,
  BOX_X = 15,
  CHECK_FM_HIRES_SIN_Y = GROUP_FM_Y + 20,
  BOX_W = 450,
  CHECK_H = 25,
  CHECK_FM_HIRES_ENV_Y = CHECK_FM_HIRES_SIN_Y + CHECK_H,
  GROUP_SSG_Y = GROUP_FM_Y+GROUP_FM_H+5,
  GROUP_SSG_H = 170,
  RADIO_SSG_OPNA_Y = GROUP_SSG_Y + 20,
  STATIC_VOLUME_OFFSET_X = 25,
  STATIC_VOLUME_OFFSET_Y = RADIO_SSG_OPNA_Y + CHECK_H + 5,
  STATIC_VOLUME_OFFSET_W = 110,
  STATIC_VOLUME_INFO_X = 35,
  STATIC_VOLUME_INFO_Y = STATIC_VOLUME_OFFSET_Y + CHECK_H + 5,
  EDIT_SSG_MIX_X = 150,
  EDIT_SSG_MIX_Y = STATIC_VOLUME_OFFSET_Y - 5,
  EDIT_SSG_MIX_W = 100,
  EDIT_SSG_MIX_H = 25,
  RADIO_SSG_YMF288_Y = STATIC_VOLUME_INFO_Y + (EDIT_SSG_MIX_H+5)*2,
  GROUP_PPZ8_Y = GROUP_SSG_Y+GROUP_SSG_H+5,
  GROUP_PPZ8_H = 100,
  RADIO_PPZ8_NONE_Y = GROUP_PPZ8_Y + 20,
  RADIO_PPZ8_LINEAR_Y = RADIO_PPZ8_NONE_Y + CHECK_H,
  RADIO_PPZ8_SINC_Y = RADIO_PPZ8_LINEAR_Y + CHECK_H,
};

enum {
  ID_CHECK_FM_HIRES_SIN = 0x10,
  ID_CHECK_FM_HIRES_ENV,
  ID_RADIO_SSG_OPNA,
  ID_RADIO_SSG_YMF288,
  ID_EDIT_SSG_MIX,
  ID_RADIO_PPZ8_NONE,
  ID_RADIO_PPZ8_LINEAR,
  ID_RADIO_PPZ8_SINC,
  ID_UPDOWN_SSG_MIX,
};

struct fmplayer_config fmplayer_config = {
  .ssg_mix = 0x10000,
  .ppz8_interp = PPZ8_INTERP_SINC,
};

extern HWND g_currentdlg;

static bool groupbox_on_erasebkgnd(HWND hwnd, HDC hdc) {
  RECT cr;
  GetClientRect(hwnd, &cr);
  FillRect(hdc, &cr, (HBRUSH)(COLOR_BTNFACE+1));
  return true;
}

static LRESULT groupbox_wndproc(
  HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam
) {
  switch (msg) {
  HANDLE_MSG(hwnd, WM_ERASEBKGND, groupbox_on_erasebkgnd);
  }
  return CallWindowProc(g.groupbox_defproc, hwnd, msg, wParam, lParam);
}

static void on_destroy(HWND hwnd) {
  (void)hwnd;
  DestroyWindow(g.radio_ppz8_none);
  DestroyWindow(g.radio_ppz8_linear);
  DestroyWindow(g.radio_ppz8_sinc);
  DestroyWindow(g.radio_ssg_ymf288);
  DestroyWindow(g.updown_ssg_mix);
  DestroyWindow(g.edit_ssg_mix);
  DestroyWindow(g.static_volume_info);
  DestroyWindow(g.static_volume_offset);
  DestroyWindow(g.radio_ssg_opna);
  DestroyWindow(g.check_fm_hires_sin);
  DestroyWindow(g.check_fm_hires_env);
  DestroyWindow(g.group_fm);
  DestroyWindow(g.group_ssg);
  DestroyWindow(g.group_ppz8);
  g.configwnd = 0;
  if (g.closecb) g.closecb(g.closecbptr);
}

static void update_ssg_mix(void) {
  wchar_t buf[10];
  swprintf(buf, sizeof(buf)/sizeof(buf[0]),
      L"%2.2f", g.ssg_mix_db);
  Edit_SetText(g.edit_ssg_mix, buf);
}

static void on_command(HWND hwnd, int id, HWND hwnd_c, UINT code) {
  (void)hwnd;
  (void)hwnd_c;
  switch (id) {
  case ID_CHECK_FM_HIRES_SIN:
    fmplayer_config.fm_hires_sin = Button_GetCheck(g.check_fm_hires_sin);
    break;
  case ID_CHECK_FM_HIRES_ENV:
    fmplayer_config.fm_hires_env = Button_GetCheck(g.check_fm_hires_env);
    break;
  case ID_RADIO_SSG_OPNA:
    Button_SetCheck(g.radio_ssg_opna, true);
    Button_SetCheck(g.radio_ssg_ymf288, false);
    fmplayer_config.ssg_ymf288 = false;
    Edit_Enable(g.edit_ssg_mix, true);
    EnableWindow(g.updown_ssg_mix, true);
    break;
  case ID_RADIO_SSG_YMF288:
    Button_SetCheck(g.radio_ssg_opna, false);
    Button_SetCheck(g.radio_ssg_ymf288, true);
    fmplayer_config.ssg_ymf288 = true;
    Edit_Enable(g.edit_ssg_mix, false);
    EnableWindow(g.updown_ssg_mix, false);
    break;
  case ID_RADIO_PPZ8_NONE:
    Button_SetCheck(g.radio_ppz8_none, true);
    Button_SetCheck(g.radio_ppz8_linear, false);
    Button_SetCheck(g.radio_ppz8_sinc, false);
    fmplayer_config.ppz8_interp = PPZ8_INTERP_NONE;
    break;
  case ID_RADIO_PPZ8_LINEAR:
    Button_SetCheck(g.radio_ppz8_none, false);
    Button_SetCheck(g.radio_ppz8_linear, true);
    Button_SetCheck(g.radio_ppz8_sinc, false);
    fmplayer_config.ppz8_interp = PPZ8_INTERP_LINEAR;
    break;
  case ID_RADIO_PPZ8_SINC:
    Button_SetCheck(g.radio_ppz8_none, false);
    Button_SetCheck(g.radio_ppz8_linear, false);
    Button_SetCheck(g.radio_ppz8_sinc, true);
    fmplayer_config.ppz8_interp = PPZ8_INTERP_SINC;
    break;
  case ID_EDIT_SSG_MIX:
    if (code == EN_KILLFOCUS) {
      int len = Edit_GetTextLength(g.edit_ssg_mix) + 1;
      if (len) {
        wchar_t *buf = malloc(len * sizeof(buf[0]));
        if (buf) {
          Edit_GetText(g.edit_ssg_mix, buf, len);
          wchar_t *endp;
          g.ssg_mix_db = wcstod(buf, &endp);
          if (buf == endp || *endp) {
            g.ssg_mix_db = wcstol(buf, &endp, 0);
          }
          if (g.ssg_mix_db < -18.0) g.ssg_mix_db = -18.0;
          if (g.ssg_mix_db > 18.0) g.ssg_mix_db = 18.0;
          update_ssg_mix();
          fmplayer_config.ssg_mix = db_to_mix(g.ssg_mix_db);
          free(buf);
        }
      }
    }
    break;
  }
  g.changecb(g.changecbptr);
}

static bool on_create(HWND hwnd, const CREATESTRUCT *cs) {
  (void)cs;
  RECT wr;
  wr.left = 0;
  wr.right = WIN_W;
  wr.top = 0;
  wr.bottom = WIN_H;
  DWORD style = GetWindowLongPtr(hwnd, GWL_STYLE);
  DWORD exstyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  AdjustWindowRectEx(&wr, style, 0, exstyle);
  SetWindowPos(hwnd, HWND_TOP, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
                SWP_NOZORDER | SWP_NOMOVE);
  if (!g.font) {
    NONCLIENTMETRICS ncm;
    ncm.cbSize = sizeof(ncm);
    SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
    g.font = CreateFontIndirect(&ncm.lfMessageFont);
  }
  
  g.group_fm = CreateWindowEx(
    0, L"button", L"FM",
    WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
    GROUP_X, GROUP_FM_Y,
    GROUP_W, GROUP_FM_H,
    hwnd, 0, g.hinst, 0);
  g.group_ssg = CreateWindowEx(
    0, L"button", L"SSG (249600 Hz to 55467 Hz resampling + DC output)",
    WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
    GROUP_X, GROUP_SSG_Y,
    GROUP_W, GROUP_SSG_H,
    hwnd, 0, g.hinst, 0);
  g.group_ppz8 = CreateWindowEx(
    0, L"button", L"PPZ8 interpolation",
    WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
    GROUP_X, GROUP_PPZ8_Y,
    GROUP_W, GROUP_PPZ8_H,
    hwnd, 0, g.hinst, 0);
  g.check_fm_hires_sin = CreateWindowEx(
    0, L"button", L"Enable higher resolution sine table",
    WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP,
    BOX_X, CHECK_FM_HIRES_SIN_Y,
    BOX_W, CHECK_H,
    hwnd, (HMENU)ID_CHECK_FM_HIRES_SIN, g.hinst, 0);
  g.check_fm_hires_env = CreateWindowEx(
    0, L"button", L"Enable higher resolution envelope",
    WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP,
    BOX_X, CHECK_FM_HIRES_ENV_Y,
    BOX_W, CHECK_H,
    hwnd, (HMENU)ID_CHECK_FM_HIRES_ENV, g.hinst, 0);
  g.radio_ssg_opna = CreateWindowEx(
    0, L"button", L"OPNA analog circuit simulation (sinc + HPF)",
    WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | WS_TABSTOP | WS_GROUP,
    BOX_X, RADIO_SSG_OPNA_Y,
    BOX_W, CHECK_H,
    hwnd, (HMENU)ID_RADIO_SSG_OPNA, g.hinst, 0);
  g.radio_ssg_ymf288 = CreateWindowEx(
    0, L"button", L"Bit perfect with OPN3-L aka YMF288 (average of nearest 4.5 samples)",
    WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
    BOX_X, RADIO_SSG_YMF288_Y,
    BOX_W, CHECK_H,
    hwnd, (HMENU)ID_RADIO_SSG_YMF288, g.hinst, 0);
  g.static_volume_offset = CreateWindowEx(
    0, L"static", L"Volume offset (dB):",
    WS_CHILD | WS_VISIBLE,
    STATIC_VOLUME_OFFSET_X, STATIC_VOLUME_OFFSET_Y,
    STATIC_VOLUME_OFFSET_W, CHECK_H,
    hwnd, 0, g.hinst, 0);
  g.static_volume_info = CreateWindowEx(
    0, L"static", L"PC-9801-86 / YMF288: 0.0dB (reference)\nPC-9801-26 / Speakboard: 1.6dB (not verified)",
    WS_CHILD | WS_VISIBLE,
    STATIC_VOLUME_INFO_X, STATIC_VOLUME_INFO_Y,
    BOX_W, CHECK_H*2,
    hwnd, 0, g.hinst, 0);
  g.edit_ssg_mix = CreateWindowEx(
    WS_EX_CLIENTEDGE, L"edit", L"",
    WS_CHILD | WS_VISIBLE | WS_TABSTOP,
    EDIT_SSG_MIX_X, EDIT_SSG_MIX_Y,
    EDIT_SSG_MIX_W, EDIT_SSG_MIX_H,
    hwnd, (HMENU)ID_EDIT_SSG_MIX, g.hinst, 0);
  g.updown_ssg_mix = CreateWindowEx(
    0, UPDOWN_CLASS, L"",
    WS_CHILD | WS_VISIBLE | UDS_AUTOBUDDY | UDS_ALIGNRIGHT | UDS_ARROWKEYS | UDS_HOTTRACK,
    0, 0, 0, 0,
    hwnd, (HMENU)ID_UPDOWN_SSG_MIX, g.hinst, 0);
  g.radio_ppz8_none = CreateWindowEx(
    0, L"button", L"Nearest neighbor (ppz8.com equivalent)",
    WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | WS_TABSTOP | WS_GROUP,
    BOX_X, RADIO_PPZ8_NONE_Y,
    BOX_W, CHECK_H,
    hwnd, (HMENU)ID_RADIO_PPZ8_NONE, g.hinst, 0);
  g.radio_ppz8_linear = CreateWindowEx(
    0, L"button", L"Linear",
    WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
    BOX_X, RADIO_PPZ8_LINEAR_Y,
    BOX_W, CHECK_H,
    hwnd, (HMENU)ID_RADIO_PPZ8_LINEAR, g.hinst, 0);
  g.radio_ppz8_sinc = CreateWindowEx(
    0, L"button", L"Sinc (best quality)",
    WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
    BOX_X, RADIO_PPZ8_SINC_Y,
    BOX_W, CHECK_H,
    hwnd, (HMENU)ID_RADIO_PPZ8_SINC, g.hinst, 0);
  g.groupbox_defproc = (WNDPROC)GetWindowLongPtr(g.group_fm, GWLP_WNDPROC);
  SetWindowLongPtr(g.group_fm, GWLP_WNDPROC, (intptr_t)groupbox_wndproc);
  SetWindowLongPtr(g.group_ssg, GWLP_WNDPROC, (intptr_t)groupbox_wndproc);
  SetWindowLongPtr(g.group_ppz8, GWLP_WNDPROC, (intptr_t)groupbox_wndproc);
  SetWindowFont(g.group_fm, g.font, TRUE);
  SetWindowFont(g.group_ssg, g.font, TRUE);
  SetWindowFont(g.group_ppz8, g.font, TRUE);
  SetWindowFont(g.check_fm_hires_sin, g.font, TRUE);
  SetWindowFont(g.check_fm_hires_env, g.font, TRUE);
  SetWindowFont(g.radio_ssg_opna, g.font, TRUE);
  SetWindowFont(g.radio_ssg_ymf288, g.font, TRUE);
  SetWindowFont(g.static_volume_offset, g.font, TRUE);
  SetWindowFont(g.static_volume_info, g.font, TRUE);
  SetWindowFont(g.edit_ssg_mix, g.font, TRUE);
  SetWindowFont(g.radio_ppz8_none, g.font, TRUE);
  SetWindowFont(g.radio_ppz8_linear, g.font, TRUE);
  SetWindowFont(g.radio_ppz8_sinc, g.font, TRUE);

  if (fmplayer_config.fm_hires_sin) Button_SetCheck(g.check_fm_hires_sin, true);
  if (fmplayer_config.fm_hires_env) Button_SetCheck(g.check_fm_hires_env, true);

  Button_SetCheck(fmplayer_config.ssg_ymf288 ? g.radio_ssg_ymf288 : g.radio_ssg_opna, true);
  switch (fmplayer_config.ppz8_interp) {
  case PPZ8_INTERP_LINEAR:
    Button_SetCheck(g.radio_ppz8_linear, true);
    break;
  case PPZ8_INTERP_SINC:
    Button_SetCheck(g.radio_ppz8_sinc, true);
    break;
  default:
    Button_SetCheck(g.radio_ppz8_none, true);
    break;
  }
  g.ssg_mix_db = mix_to_db(fmplayer_config.ssg_mix);
  update_ssg_mix();
  Edit_Enable(g.edit_ssg_mix, !fmplayer_config.ssg_ymf288);
  EnableWindow(g.updown_ssg_mix, !fmplayer_config.ssg_ymf288);
  ShowWindow(hwnd, SW_SHOW);
  return true;
}

static void on_activate(HWND hwnd, bool activate, HWND targetwnd, WINBOOL state) {
  (void)targetwnd;
  (void)state;
  if (activate) g_currentdlg = hwnd;
  else g_currentdlg = 0;
}

static LRESULT CALLBACK wndproc(
  HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam
) {
  switch (msg) {
  HANDLE_MSG(hwnd, WM_DESTROY, on_destroy);
  HANDLE_MSG(hwnd, WM_CREATE, on_create);
  HANDLE_MSG(hwnd, WM_COMMAND, on_command);
  HANDLE_MSG(hwnd, WM_ACTIVATE, on_activate);
  case WM_NOTIFY:
    {
      const NMHDR *hdr = (NMHDR *)lParam;
      if (hdr->idFrom == ID_UPDOWN_SSG_MIX && hdr->code == UDN_DELTAPOS) {
        const NMUPDOWN *udnhdr = (NMUPDOWN *)hdr;
        g.ssg_mix_db -= udnhdr->iDelta * 0.1;
        g.ssg_mix_db = round(g.ssg_mix_db * 10.0) / 10.0;
        if (g.ssg_mix_db < -18.0) g.ssg_mix_db = -18.0;
        if (g.ssg_mix_db > 18.0) g.ssg_mix_db = 18.0;
        update_ssg_mix();
        fmplayer_config.ssg_mix = db_to_mix(g.ssg_mix_db);
      }
      break;
    }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

void configdialog_open(HINSTANCE hinst, HWND parent, void (*closecb)(void *), void *closecbptr, void (*changecb)(void *), void *changecbptr) {
  g.closecb = closecb;
  g.closecbptr = closecbptr;
  g.changecb = changecb;
  g.changecbptr = changecbptr;
  g.hinst = hinst;
  if (!g.configwnd) {
    if (!g.config_class) {
      WNDCLASS wc = {0};
      wc.style = CS_HREDRAW | CS_VREDRAW;
      wc.lpfnWndProc = wndproc;
      wc.hInstance = hinst;
      wc.hIcon = LoadIcon(g.hinst, MAKEINTRESOURCE(1));
      wc.hCursor = LoadCursor(NULL, IDC_ARROW);
      wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
      wc.lpszClassName = L"myon_fmplayer_ym2608_config";
      g.config_class = RegisterClass(&wc);
    }
    if (!g.config_class) {
      MessageBox(parent, L"Cannot register class", L"Error", MB_ICONSTOP);
      return;
    }
    g.configwnd = CreateWindowEx(0,
                                 MAKEINTATOM(g.config_class),
                                 L"FMPlayer config",
                                 WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN,
                                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                                 parent, 0, g.hinst, 0);
  } else {
    SetForegroundWindow(g.configwnd);
  }
}

void configdialog_close(void) {
  if (g.configwnd) {
    g.closecb = 0;
    DestroyWindow(g.configwnd);
  }
}