-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemePackerDlg.cpp
735 lines (624 loc) · 17.4 KB
/
ThemePackerDlg.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
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
731
732
733
734
// ThemePackerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ThemePacker.h"
#include "ThemePackerDlg.h"
#include "afxdialogex.h"
#include "SoundDlg.h"
#include "MusicDlg.h"
#include "STBwriter.h"
#include "STBreader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#pragma comment(lib, "Winmm.lib")
// CThemePackerDlg dialog
CThemePackerDlg::CThemePackerDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_THEMEPACKER_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
waveout = 0;
}
void CThemePackerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROJECTBUTTON, m_projectbtn);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Control(pDX, IDC_ADD, m_btnAdd);
DDX_Control(pDX, IDC_EDIT, m_btnEdit);
DDX_Control(pDX, IDC_DELETE, m_btnDelete);
DDX_Control(pDX, IDC_PLAY, m_btnPlay);
DDX_Control(pDX, IDC_ADDAUTO, m_autoaddBtn);
DDX_Control(pDX, IDC_SAVE, m_btnSave);
}
BEGIN_MESSAGE_MAP(CThemePackerDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO2, &CThemePackerDlg::OnBnClickedRadio2)
ON_BN_CLICKED(IDC_RADIO1, &CThemePackerDlg::OnBnClickedRadio1)
ON_COMMAND(ID_FILE_NEW32771, &CThemePackerDlg::OnFileNew)
ON_COMMAND(ID_FILE_OPEN32772, &CThemePackerDlg::OnFileOpen)
ON_COMMAND(ID_FILE_SAVE32773, &CThemePackerDlg::OnFileSave)
ON_COMMAND(ID_FILE_EXIT, &CThemePackerDlg::OnFileExit)
ON_BN_CLICKED(IDC_ADD, &CThemePackerDlg::OnBnClickedAdd)
ON_BN_CLICKED(IDC_EDIT, &CThemePackerDlg::OnBnClickedEdit)
ON_BN_CLICKED(IDC_DELETE, &CThemePackerDlg::OnBnClickedDelete)
ON_BN_CLICKED(IDC_PLAY, &CThemePackerDlg::OnBnClickedPlay)
ON_WM_SIZE()
// ON_NOTIFY(BCN_DROPDOWN, IDC_PROJECTBUTTON, &CThemePackerDlg::OnDropdownProjectbutton)
ON_BN_CLICKED(IDC_PROJECTBUTTON, &CThemePackerDlg::OnClickedProjectbutton)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CThemePackerDlg::OnDblclkList1)
ON_MESSAGE(WOM_DONE, &CThemePackerDlg::OnWomDone)
ON_COMMAND(ID_FILE_SAVEAS, &CThemePackerDlg::OnFileSaveas)
ON_BN_CLICKED(IDC_ADDAUTO, &CThemePackerDlg::OnBnClickedAddauto)
ON_WM_DROPFILES()
ON_BN_CLICKED(IDC_SAVE, &CThemePackerDlg::OnClickedSave)
END_MESSAGE_MAP()
// CThemePackerDlg message handlers
BOOL CThemePackerDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
saved = true;
GetWindowText(windowTitle);
if (fileName.IsEmpty()) setWindowName();
else doOpen();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
menu.LoadMenu(IDR_MENU1);
m_projectbtn.m_hMenu = menu.GetSubMenu(0)->GetSafeHmenu();
m_projectbtn.m_bOSMenu = FALSE;
((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(1);
m_list.InsertColumn(0, _T("Name"), LVCFMT_LEFT, 300, 0);
m_list.InsertColumn(1, _T("ID"), LVCFMT_LEFT, 50, 1);
OnBnClickedRadio1();
RECT rb, rl, rw;
m_btnAdd.GetWindowRect(&rb);
ScreenToClient(&rb);
m_list.GetWindowRect(&rl);
ScreenToClient(&rl);
GetClientRect(&rw);
m_lbdiff = rb.top - rl.bottom;
m_bwdiff = rw.bottom - rb.bottom;
m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
DragAcceptFiles();
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CThemePackerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CThemePackerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CThemePackerDlg::OnBnClickedRadio1()
{
m_list.DeleteAllItems();
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.cx = 50;
lvc.pszText = _T("ID");
m_list.SetColumn(1, &lvc);
for (int i = 0; i < sounds.GetCount(); i++) {
int k = m_list.InsertItem(m_list.GetItemCount(), sounds[i].name);
m_list.SetItemText(k, 1, CString(sounds[i].id));
}
m_soundshown = true;
}
void CThemePackerDlg::OnBnClickedRadio2()
{
m_list.DeleteAllItems();
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.cx = 300;
lvc.pszText = _T("Song");
m_list.SetColumn(1, &lvc);
for (int i = 0; i < musics.GetCount(); i++) {
CString s;
s.Format("(%d) %s", musics[i].type, musics[i].name);
int k = m_list.InsertItem(m_list.GetItemCount(), s);
m_list.SetItemText(k, 1, musics[i].playlist);
}
m_soundshown = false;
}
void CThemePackerDlg::RemoveAll()
{
for (int i = 0; i < sounds.GetCount(); i++)
if (sounds[i].wv) free(sounds[i].wv);
sounds.RemoveAll();
musics.RemoveAll();
}
void CThemePackerDlg::OnFileNew()
{
if (!saved) {
int res = AfxMessageBox(_T("Would you like to save changes?"), MB_YESNOCANCEL);
if (res == IDCANCEL) return;
if (res == IDYES) OnFileSave();
}
m_list.DeleteAllItems();
RemoveAll();
saved = true;
}
void CThemePackerDlg::doOpen()
{
if (stb.open(fileName)) {
AfxMessageBox("Error opening file");
return;
}
m_list.DeleteAllItems();
RemoveAll();
int numsounds = stb.get_num_sounds();
int nummusics = stb.get_num_musics();
char namebuf[256];
for (int i = 0; i < numsounds; i++) {
SoundDesc sd;
int pos;
stb.get_sound_bynum(i, (unsigned char *)&sd.id, &pos, &sd.size);
sd.wv = malloc(sd.size);
if (!sd.wv) {
AfxMessageBox("Memory error, exiting");
EndDialog(1);
return;
}
stb.read(sd.wv, pos, sd.size);
stb.get_sound_name(namebuf, i);
sd.name = namebuf;
sounds.Add(sd);
if (m_soundshown) {
int k = m_list.InsertItem(m_list.GetItemCount(), sd.name);
m_list.SetItemText(k, 1, CString((TCHAR)sd.id));
m_list.SetItemData(k, (DWORD_PTR)sd.wv);
}
}
for (int i = 0; i < nummusics; i++) {
MusicDesc md;
stb.get_music_bynum(i, &md.type, namebuf);
md.playlist = namebuf;
stb.get_music_name(namebuf, i);
md.name = namebuf;
musics.Add(md);
if (!m_soundshown) {
CString s;
s.Format("(%d) %s", md.type, md.name);
int i = m_list.InsertItem(m_list.GetItemCount(), s);
m_list.SetItemText(i, 1, md.playlist);
}
}
saved = true;
setWindowName();
}
void CThemePackerDlg::OnFileOpen()
{
if (!saved) {
int res = AfxMessageBox(_T("Would you like to save changes?"), MB_YESNOCANCEL);
if (res == IDCANCEL) return;
if (res == IDYES) OnFileSave();
}
CFileDialog fd(TRUE, _T("stb"), _T("*.stb"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T("Smart Toybox Theme (*.stb)|*.stb|All Files (*.*)|*.*||"));
if (fd.DoModal() == IDOK) {
fileName = fd.GetPathName();
doOpen();
}
}
void CThemePackerDlg::OnFileSaveas()
{
CFileDialog fd(FALSE, _T("stb"), PathFindFileName(fileName), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Smart Toybox Theme (*.stb)|*.stb|All Files (*.*)|*.*||"));
if (fd.DoModal() == IDOK) {
fileName = fd.GetPathName();
OnFileSave();
}
}
void CThemePackerDlg::OnFileSave()
{
if (fileName.IsEmpty()) {
OnFileSaveas();
return;
}
STBwriter stbw;
if (!stbw.Open(fileName, sounds.GetCount(), musics.GetCount()))
AfxMessageBox("Save failed");
else {
for (int i = 0; i < sounds.GetCount(); i++)
stbw.WriteSound(sounds[i].name, sounds[i].id, sounds[i].wv, sounds[i].size);
for (int i = 0; i < musics.GetCount(); i++)
stbw.WriteMusic(musics[i].name, musics[i].type, musics[i].playlist);
stbw.Close();
}
saved = true;
}
void CThemePackerDlg::OnFileExit()
{
// if not saved...
if(!saved) {
int res = AfxMessageBox(_T("Would you like to save changes?"), MB_YESNOCANCEL);
if (res == IDCANCEL) return;
if (res == IDYES) OnFileSave();
}
EndDialog(0);
}
CString CThemePackerDlg::getUsedID()
{
CString ret;
for (int i = 0; i < sounds.GetCount(); i++)
ret.AppendChar((TCHAR)sounds[i].id);
return ret;
}
void CThemePackerDlg::OnBnClickedAdd()
{
if (m_soundshown) {
CSoundDlg dlg;
dlg.m_alreadyhave = getUsedID();
if (dlg.DoModal() == IDOK) {
SoundDesc sd;
sd.name = dlg.m_name;
sd.id = dlg.m_id;
sd.wv = dlg.m_wv;
sd.size = dlg.m_wvsize;
sounds.Add(sd);
int i = m_list.InsertItem(m_list.GetItemCount(), sd.name);
m_list.SetItemText(i, 1, CString((TCHAR)sd.id));
m_list.SetItemData(i, (DWORD_PTR)sd.wv);
m_list.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
saved = false;
}
}
else {
CMusicDlg dlg;
if (dlg.DoModal() == IDOK) {
MusicDesc md;
md.name = dlg.m_name;
md.playlist = dlg.m_music;
md.type = dlg.m_type;
musics.Add(md);
CString s;
s.Format("(%d) %s", md.type, md.name);
int i=m_list.InsertItem(m_list.GetItemCount(), s);
m_list.SetItemText(i, 1, md.playlist);
m_list.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
saved = false;
}
}
}
void CThemePackerDlg::AddFiles(CStringArray &sa)
{
CSoundDlg sdlg;
sdlg.m_alreadyhave = getUsedID();
for (int i = 0; i < sa.GetCount(); i++) {
sdlg.m_path = sa[i];
if (!sdlg.TestFile()) continue;
sdlg.CreateNameFromFileName();
SoundDesc sd;
sd.name = sdlg.m_name;
sd.id = sdlg.GetUnusedID();
sd.wv = sdlg.m_wv;
sd.size = sdlg.m_wvsize;
sounds.Add(sd);
sdlg.m_alreadyhave += sd.id;
if (m_soundshown) {
int k = m_list.InsertItem(m_list.GetItemCount(), sd.name);
m_list.SetItemText(k, 1, CString((TCHAR)sd.id));
m_list.SetItemData(k, (DWORD_PTR)sd.wv);
if (!i) m_list.SetItemState(k, LVIS_SELECTED, LVIS_SELECTED);
} else {
CMusicDlg dlg;
dlg.m_name = sd.name;
dlg.m_music.AppendChar(sd.id);
if (dlg.DoModal() == IDOK) {
MusicDesc md;
md.name = dlg.m_name;
md.playlist = dlg.m_music;
md.type = dlg.m_type;
musics.Add(md);
CString s;
s.Format("(%d) %s", md.type, md.name);
int i = m_list.InsertItem(m_list.GetItemCount(), s);
m_list.SetItemText(i, 1, md.playlist);
m_list.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
}
}
saved = false;
}
}
void CThemePackerDlg::OnBnClickedAddauto()
{
CFileDialog fd(TRUE, _T("wav"), _T("*.wav"), OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T("Wave Audio File (*.wav)|*.wav|Wavpack Compressed Audio File (*.wv)|*.wv|All Files (*.*)|*.*||"));
if (fd.DoModal() == IDOK) {
char *fn = fd.GetOFN().lpstrFile;
CString path(fn);
CStringArray sa;
int slen = strlen(fn) + 1;
if (fd.GetOFN().nFileOffset < slen) {
// single file
sa.Add(path);
}
else {
fn += slen;
// multiple files
while (*fn) {
sa.Add(path + '\\' + fn);
fn += strlen(fn) + 1;
}
}
AddFiles(sa);
}
}
void CThemePackerDlg::OnDropFiles(HDROP hDropInfo)
{
CStringArray sa;
char buf[MAX_PATH + 1];
int numfiles = DragQueryFile(hDropInfo, -1, 0, 0);
for (int i = 0; i < numfiles; i++) {
DragQueryFile(hDropInfo, i, buf, MAX_PATH);
sa.Add(buf);
}
DragFinish(hDropInfo);
AddFiles(sa);
//CDialogEx::OnDropFiles(hDropInfo);
}
void CThemePackerDlg::OnBnClickedEdit()
{
if (!m_list.GetSelectedCount()) {
AfxMessageBox(_T("No selected items..."));
return;
}
POSITION p = m_list.GetFirstSelectedItemPosition();
int item = m_list.GetNextSelectedItem(p);
if (m_soundshown) {
CSoundDlg dlg;
dlg.m_alreadyhave = getUsedID();
dlg.m_alreadyhave.Remove((TCHAR)sounds[item].id);
dlg.m_id = sounds[item].id;
dlg.m_name = sounds[item].name;
if (dlg.DoModal() == IDOK) {
if (sounds[item].wv != dlg.m_wv) {
free(sounds[item].wv);
sounds[item].wv = dlg.m_wv;
sounds[item].size = dlg.m_wvsize;
m_list.SetItemData(item, (DWORD_PTR)dlg.m_wv);
}
sounds[item].name = dlg.m_name;
sounds[item].id = dlg.m_id;
m_list.SetItemText(item, 0, dlg.m_name);
m_list.SetItemText(item, 1, CString((TCHAR)dlg.m_id));
saved = false;
}
}
else {
CMusicDlg dlg;
dlg.m_name = musics[item].name;
dlg.m_music = musics[item].playlist;
dlg.m_type = musics[item].type;
if (dlg.DoModal() == IDOK) {
musics[item].name = dlg.m_name;
musics[item].playlist = dlg.m_music;
musics[item].type = dlg.m_type;
CString s;
s.Format("(%d) %s", dlg.m_type, dlg.m_name);
m_list.SetItemText(item, 0, s);
m_list.SetItemText(item, 1, dlg.m_music);
saved = false;
}
}
}
void CThemePackerDlg::OnBnClickedDelete()
{
if (!m_list.GetSelectedCount()) {
AfxMessageBox(_T("No selected items..."));
return;
}
POSITION p = m_list.GetFirstSelectedItemPosition();
int item = m_list.GetNextSelectedItem(p);
m_list.DeleteItem(item);
if (m_soundshown) {
if (sounds[item].wv) free(sounds[item].wv);
sounds.RemoveAt(item);
}
else musics.RemoveAt(item);
saved = false;
}
void CThemePackerDlg::OnBnClickedPlay()
{
if (!m_list.GetSelectedCount()) {
AfxMessageBox(_T("No selected items..."));
return;
}
POSITION p = m_list.GetFirstSelectedItemPosition();
int item = m_list.GetNextSelectedItem(p);
if (m_soundshown)
//playBuffer((char*)sounds[item].wv, sounds[item].size);
playSound(sounds[item].id);
else
playMelody(musics[item].playlist);
}
void CThemePackerDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
if (nType != SIZE_MAXIMIZED && nType != SIZE_RESTORED) return;
CRect rb;
if (!m_btnAdd.m_hWnd) return;
m_btnAdd.GetWindowRect(&rb);
ScreenToClient(&rb);
int diff = (cy - m_bwdiff) - rb.bottom;
rb.OffsetRect(0, diff);
m_btnAdd.MoveWindow(&rb);
m_autoaddBtn.GetWindowRect(&rb);
ScreenToClient(&rb);
rb.OffsetRect(0, diff);
m_autoaddBtn.MoveWindow(&rb);
m_btnEdit.GetWindowRect(&rb);
ScreenToClient(&rb);
rb.OffsetRect(0, diff);
m_btnEdit.MoveWindow(&rb);
m_btnDelete.GetWindowRect(&rb);
ScreenToClient(&rb);
rb.OffsetRect(0, diff);
m_btnDelete.MoveWindow(&rb);
m_btnPlay.GetWindowRect(&rb);
ScreenToClient(&rb);
rb.OffsetRect(0, diff);
m_btnPlay.MoveWindow(&rb);
m_btnSave.GetWindowRect(&rb);
ScreenToClient(&rb);
rb.OffsetRect(0, diff);
m_btnSave.MoveWindow(&rb);
m_list.GetWindowRect(&rb);
ScreenToClient(&rb);
rb.bottom += diff;
rb.right = cx;
m_list.MoveWindow(&rb);
}
void CThemePackerDlg::OnClickedProjectbutton()
{
switch (m_projectbtn.m_nMenuResult)
{
case ID_FILE_OPEN32772: OnFileOpen(); break;
case ID_FILE_NEW32771: OnFileNew(); break;
case ID_FILE_EXIT: OnFileExit(); break;
case ID_FILE_SAVE32773: OnFileSave(); break;
case ID_FILE_SAVEAS: OnFileSaveas(); break;
}
}
void CThemePackerDlg::OnDblclkList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
if (m_list.GetSelectedCount()) OnBnClickedEdit();
*pResult = 0;
}
void CThemePackerDlg::OnCancel()
{
OnFileExit();
}
void CThemePackerDlg::OnClickedSave()
{
if (!m_list.GetSelectedCount()) {
AfxMessageBox(_T("No selected items..."));
return;
}
if (!m_soundshown) {
AfxMessageBox(_T("Please select sound..."));
return;
}
POSITION p = m_list.GetFirstSelectedItemPosition();
int item = m_list.GetNextSelectedItem(p);
if (!sounds[item].wv || !sounds[item].size) {
AfxMessageBox(_T("No sound found..."));
return;
}
CFileDialog fd(FALSE, _T("wv"), _T("*.wv"), OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, _T("Wavpack Compressed Audio File (*.wv)|*.wv|All Files (*.*)|*.*||"));
if (fd.DoModal() == IDOK) {
FILE *f = fopen(fd.GetOFN().lpstrFile, "wb");
if (f) {
fwrite(sounds[item].wv, 1, sounds[item].size, f);
fclose(f);
}
else {
AfxMessageBox(_T("Failed to save file"));
return;
}
}
}
/////// PLAY
extern "C" {
#include "wp\float.c"
#include "wp\metadata.c"
#include "wp\bits.c"
#include "wp\unpack.c"
#include "wp\words.c"
#include "wp\wputils.c"
#include "wp\tinyunpack.c"
};
static bool justreset = false;
static char playmelody[256], *playpos;
void CThemePackerDlg::stopPlay()
{
if (!waveout) return;
waveOutReset(waveout);
waveOutUnprepareHeader(waveout, &wihdr, sizeof(wihdr));
waveOutClose(waveout);
waveout = 0;
}
void CThemePackerDlg::nextSong()
{
do {
char c = *playpos++;
if (!c) {
stopPlay();
return;
}
for (int i = 0; i < sounds.GetCount(); i++)
if (sounds[i].id == c) {
playBuffer((char*)sounds[i].wv, sounds[i].size);
return;
}
} while (1);
}
void CThemePackerDlg::playMelody(const char *melody)
{
strcpy(playmelody, melody);
playpos = playmelody;
if (waveout) {
waveOutReset(waveout);
waveOutUnprepareHeader(waveout, &wihdr, sizeof(wihdr)); // will call nextsong from callback
} else
nextSong();
}
bool CThemePackerDlg::playBuffer(char *buf, int insize)
{
if (!waveout) {
// justreset = true;
// waveOutReset(waveout);
// waveOutUnprepareHeader(waveout, &wihdr, sizeof(wihdr));
//}
//else {
WAVEFORMATEX wf = { 0 };
wf.cbSize = sizeof(wf);
wf.nChannels = 1;
wf.nSamplesPerSec = 16000;
wf.wFormatTag = WAVE_FORMAT_PCM;
wf.wBitsPerSample = 16;
wf.nBlockAlign = 2;
wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
if (waveOutOpen(&waveout, WAVE_MAPPER, &wf, (DWORD_PTR)GetSafeHwnd(), 0, CALLBACK_WINDOW | WAVE_ALLOWSYNC) != MMSYSERR_NOERROR)
return false;
}
int size = unpack_buffer(buf, (int*)rec_data_buf, insize) * 2; // *2 = 16bit
wihdr.dwFlags = 0;
wihdr.lpData = (LPSTR)rec_data_buf;
wihdr.dwBufferLength = size;
waveOutPrepareHeader(waveout, &wihdr, sizeof(wihdr));
waveOutWrite(waveout, &wihdr, sizeof(wihdr));
return true;
}
afx_msg LRESULT CThemePackerDlg::OnWomDone(WPARAM wParam, LPARAM lParam)
{
if(!justreset)
nextSong();
else
justreset = false;
return 0;
}