Subversion Repositories pentevo

Rev

Rev 883 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "std.h"
  2.  
  3. #include "resource.h"
  4.  
  5. #include "emul.h"
  6. #include "vars.h"
  7. #include "dx.h"
  8. #include "memory.h"
  9. #include "savesnd.h"
  10.  
  11. #include "util.h"
  12.  
  13. static unsigned char wavhdr[]= {
  14.    0x52,0x49,0x46,0x46,0xcc,0xf6,0x3e,0x00,
  15.    0x57,0x41,0x56,0x45,0x66,0x6d,0x74,0x20,
  16.    0x10,0x00,0x00,0x00,0x01,0x00,0x02,0x00,
  17.    0x22,0x56,0x00,0x00,0x88,0x58,0x01,0x00,
  18.    0x04,0x00,0x10,0x00,0x64,0x61,0x74,0x61,
  19.    0xa8,0xf6,0x3e,0x00
  20. };
  21. #pragma pack(push, 1)
  22. static struct
  23. {
  24.    unsigned short sig;
  25.    unsigned char stereo;
  26.    unsigned short start;
  27.    unsigned ayfq;
  28.    unsigned char intfq;
  29.    unsigned short year;
  30.    unsigned rawsize;
  31. } vtxheader;
  32. #pragma pack(pop)
  33.  
  34. static bool silence(unsigned pos)
  35. {
  36.    return !(vtxbuf[pos+8] | vtxbuf[pos+9] | vtxbuf[pos+10]) ||
  37.           (vtxbuf[pos+7] & 0x3F) == 0x3F;
  38. }
  39.  
  40. INT_PTR CALLBACK VtxDlg(HWND dlg, UINT msg, WPARAM wp, LPARAM lp);
  41. static unsigned vtxyear, vtxchip;
  42. static char vtxname[200], vtxauthor[200], vtxsoft[200], vtxtracker[200], vtxcomm[200];
  43.  
  44. void savesnddialog()
  45. {
  46.    OnEnterGui(); //Alone Coder
  47.    unsigned end; //Alone Coder 0.36.7
  48.    if (savesndtype) {
  49.       if (savesndtype == 1) { // wave
  50.          unsigned fsize = unsigned(ftell(savesnd));
  51.          fseek(savesnd, 0, SEEK_SET);
  52.          fsize -= sizeof wavhdr;
  53.          *(unsigned*)(wavhdr+4) = fsize+0x2c-8;
  54.          *(unsigned*)(wavhdr+0x28) = fsize;
  55.          fwrite(wavhdr, 1, sizeof wavhdr, savesnd);
  56.          MessageBox(wnd, "WAV save done", "Save sound", MB_ICONINFORMATION);
  57.       } else { // vtx
  58.          savesndtype = 0;
  59.          unsigned char *newb = (unsigned char*)malloc(vtxbuffilled);
  60.          for (/*unsigned*/ end = 0; end < vtxbuffilled && silence(end); end += 14);
  61.          vtxbuffilled -= end; memcpy(vtxbuf, vtxbuf+end, vtxbuffilled);
  62.          for (end = vtxbuffilled; end && silence(end-14); end -= 14);
  63.          vtxbuffilled = end;
  64.          int nrec = vtxbuffilled/14;
  65.          for (int i = 0; i < nrec; i++)
  66.             for (int j = 0; j < 14; j++)
  67.                newb[j*nrec+i] = vtxbuf[i*14+j];
  68.          free(vtxbuf);
  69.          FILE *ff = fopen("vtx.tmp", "wb");
  70.          if (!ff) return;
  71.          fwrite(newb, 1, vtxbuffilled, ff);
  72.          fclose(ff);
  73.          STARTUPINFO si = { sizeof si };
  74.          si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE;
  75.          PROCESS_INFORMATION pi;
  76.          char Parh[] = "lha a vtx.lzh vtx.tmp";
  77.          if (CreateProcess(nullptr, Parh, nullptr, nullptr, 0, 0, nullptr, nullptr, &si, &pi))
  78.          {
  79.             WaitForSingleObject(pi.hProcess, 5000);
  80.             CloseHandle(pi.hProcess);
  81.             CloseHandle(pi.hThread);
  82.             DeleteFile("vtx.tmp");
  83.          }
  84.          else
  85.          {
  86.             DeleteFile("vtx.tmp");
  87.             MessageBox(wnd, "LHA.EXE not found in %PATH%", nullptr, MB_ICONERROR);
  88.             return;
  89.          }
  90.          ff = fopen("vtx.lzh", "rb"); if (!ff) return;
  91.          fseek(ff, 0x22, SEEK_SET);
  92.          size_t packed = fread(newb, 1, vtxbuffilled, ff)-1;
  93.          fclose(ff); DeleteFile("vtx.lzh");
  94.          DialogBox(hIn, MAKEINTRESOURCE(IDD_VTX), wnd, VtxDlg);
  95.          vtxheader.sig = (vtxchip & 1) ? WORD2('y','m') : WORD2('a','y');
  96.          static unsigned char ste[] = { 1, 2, 0 };
  97.          vtxheader.stereo = ste[vtxchip/2];
  98.          vtxheader.ayfq = conf.sound.ayfq;
  99.          vtxheader.intfq = 50;
  100.          vtxheader.year = u16(vtxyear);
  101.          vtxheader.rawsize = vtxbuffilled;
  102.          fwrite(&vtxheader, 1, 0x10, savesnd);
  103.          fwrite(vtxname, 1, strlen(vtxname)+1, savesnd);
  104.          fwrite(vtxauthor, 1, strlen(vtxauthor)+1, savesnd);
  105.          fwrite(vtxsoft, 1, strlen(vtxsoft)+1, savesnd);
  106.          fwrite(vtxtracker, 1, strlen(vtxtracker)+1, savesnd);
  107.          fwrite(vtxcomm, 1, strlen(vtxcomm)+1, savesnd);
  108.          fwrite(newb, 1, packed, savesnd);
  109.       }
  110.       fclose(savesnd);
  111.       savesndtype = 0;
  112.    } else {
  113.       OPENFILENAME ofn = { };
  114.       char sndsavename[0x200]; *sndsavename = 0;
  115.  
  116.       ofn.lStructSize = (WinVerMajor < 5) ? OPENFILENAME_SIZE_VERSION_400 : sizeof(OPENFILENAME);
  117.       ofn.lpstrFilter = "All sound (WAV)\0*.wav\0AY sound (VTX)\0*.vtx\0";
  118.       ofn.lpstrFile = sndsavename; ofn.nMaxFile = sizeof sndsavename;
  119.       ofn.lpstrTitle = "Save Sound";
  120.       ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
  121.       ofn.hwndOwner = wnd;
  122.       ofn.nFilterIndex = 1;
  123.       if (GetSaveFileName(&ofn)) {
  124.          char *name = sndsavename; for (char *x = name; *x; x++) if (*x == '\\') name = x+1;
  125.          if (!strchr(name, '.')) {
  126.             if (ofn.nFilterIndex == 1) strcat(sndsavename, ".wav");
  127.             else strcat(sndsavename, ".vtx");
  128.          }
  129.          savesnd = fopen(ofn.lpstrFile, "wb");
  130.          if (!savesnd) MessageBox(wnd, "Can't create file", nullptr, MB_ICONERROR);
  131.          else if (ofn.nFilterIndex == 2) { // vtx
  132.             savesndtype = 2;
  133.             vtxbuf = nullptr;
  134.          } else { // wave. all params, except fq are fixed: 16bit,stereo
  135.             *(unsigned*)(wavhdr+0x18) = conf.sound.fq; // fq
  136.             *(unsigned*)(wavhdr+0x1C) = conf.sound.fq*4; // bitrate
  137.             fwrite(wavhdr, 1, 44, savesnd); // header
  138.             savesndtype = 1;
  139.          }
  140.       }
  141.    }
  142.    eat();
  143.    OnExitGui(); //Alone Coder
  144. }
  145.  
  146. INT_PTR CALLBACK VtxDlg(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
  147. {
  148.     (void)lp;
  149.  
  150.    if (msg == WM_INITDIALOG) {
  151.       static char chips[] = "ABC AY\0ABC YM\0ACB AY\0ACB YM\0MONO AY\0MONO YM\0";
  152.       for (char *str = chips; *str; str += strlen(str)+1)
  153.          SendDlgItemMessage(dlg, IDC_VTXCHIP, CB_ADDSTRING, 0, (LPARAM)str);
  154.       unsigned c = ((conf.sound.ay_voltab[8] == conf.sound.ay_voltab[9])?0:1) /* + (conf.ay_preset>2?0:conf.ay_preset*2) */;
  155.       SendDlgItemMessage(dlg, IDC_VTXCHIP, CB_SETCURSEL, c, 0);
  156.       SetFocus(GetDlgItem(dlg, IDE_VTXNAME));
  157.       return 1;
  158.    }
  159.    if ((msg == WM_SYSCOMMAND && (wp & 0xFFF0) == SC_CLOSE) ||
  160.        (msg == WM_COMMAND && LOWORD(wp) == IDOK))
  161.    {
  162.       SendDlgItemMessage(dlg, IDE_VTXNAME, WM_GETTEXT, sizeof vtxname, (LPARAM)vtxname);
  163.       SendDlgItemMessage(dlg, IDE_VTXAUTH, WM_GETTEXT, sizeof vtxauthor, (LPARAM)vtxauthor);
  164.       SendDlgItemMessage(dlg, IDE_VTXSOFT, WM_GETTEXT, sizeof vtxsoft, (LPARAM)vtxsoft);
  165.       SendDlgItemMessage(dlg, IDE_VTXTRACK, WM_GETTEXT, sizeof vtxtracker, (LPARAM)vtxtracker);
  166.       SendDlgItemMessage(dlg, IDE_VTXCOMM, WM_GETTEXT, sizeof vtxcomm, (LPARAM)vtxcomm);
  167.       vtxchip = unsigned(SendDlgItemMessage(dlg, IDC_VTXCHIP, CB_GETCURSEL, 0, 0));
  168.       char xx[20]; SendDlgItemMessage(dlg, IDE_VTXYEAR, WM_GETTEXT, sizeof xx, (LPARAM)xx);
  169.       vtxyear = unsigned(atoi(xx));
  170.       EndDialog(dlg, 1);
  171.    }
  172.    return 0;
  173. }
  174.  
  175. static unsigned dopoke(int really)
  176. {
  177.    for (unsigned char *ptr = snbuf; *ptr; ) {
  178.       while (*ptr == ' ' || *ptr == ':' || *ptr == ';' || *ptr == ',') ptr++;
  179.       unsigned num = 0;
  180.       while (isdigit(*ptr)) num = num*10 + (*ptr++ - '0');
  181.       if (num < 0x4000 || num > 0xFFFF) return unsigned(ptr-snbuf+1);
  182.       while (*ptr == ' ' || *ptr == ':' || *ptr == ';' || *ptr == ',') ptr++;
  183.       unsigned val = 0;
  184.       while (isdigit(*ptr)) val = val*10 + (*ptr++ - '0');
  185.       if (val > 0xFF) return unsigned(ptr-snbuf+1);
  186.       while (*ptr == ' ' || *ptr == ':' || *ptr == ';' || *ptr == ',') ptr++;
  187.       if (really)
  188.           cpu.DirectWm(num, u8(val));
  189.    }
  190.    return 0;
  191. }
  192.  
  193. INT_PTR CALLBACK pokedlg(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
  194. {
  195.     (void)lp;
  196.  
  197.    if (msg == WM_INITDIALOG) {
  198.       SetFocus(GetDlgItem(dlg, IDE_POKE));
  199.       return 1;
  200.    }
  201.    if ((msg == WM_COMMAND && wp == IDCANCEL) ||
  202.        (msg == WM_SYSCOMMAND && (wp & 0xFFF0) == SC_CLOSE)) EndDialog(dlg, 0);
  203.    if (msg == WM_COMMAND && LOWORD(wp) == IDOK)
  204.    {
  205.       SendDlgItemMessage(dlg, IDE_POKE, WM_GETTEXT, /*sizeof snbuf*/640*480*4, (LPARAM)snbuf); //Alone Coder 0.36.5
  206.       unsigned r = dopoke(0);
  207.       if(r)
  208.       {
  209.           MessageBox(dlg, "Incorrect format", nullptr, MB_ICONERROR);
  210.           SendDlgItemMessage(dlg, IDE_POKE, EM_SETSEL, r, r);
  211.       }
  212.       else
  213.       {
  214.           dopoke(1);
  215.           EndDialog(dlg, 0);
  216.       }
  217.    }
  218.    return 0;
  219. }
  220.