home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.72_stereo.zip / src / gsound.c < prev    next >
C/C++ Source or Header  |  2010-10-21  |  14KB  |  557 lines

  1. //
  2. // GOATTRACKER sound routines
  3. //
  4.  
  5. #define GSOUND_C
  6.  
  7. #ifdef __WIN32__
  8. #include <windows.h>
  9. #endif
  10.  
  11. #include "goattrk2.h"
  12.  
  13. // General / reSID output
  14. int playspeed;
  15. int usehardsid = 0;
  16. int lefthardsid = 0;
  17. int righthardsid = 0;
  18. int usecatweasel = 0;
  19. int initted = 0;
  20. int firsttimeinit = 1;
  21. unsigned framerate = PALFRAMERATE;
  22. Sint16 *lbuffer = NULL;
  23. Sint16 *rbuffer = NULL;
  24.  
  25. FILE *writehandle = NULL;
  26.  
  27. void sound_playrout(void);
  28. void sound_mixer(Sint32 *dest, unsigned samples);
  29. Uint32 sound_timer(Uint32 interval);
  30.  
  31. #ifdef __WIN32__
  32.  
  33. // Win32 HardSID output
  34. typedef void (CALLBACK* lpWriteToHardSID)(Uint8 DeviceID, Uint8 SID_reg, Uint8 Data);
  35. typedef Uint8 (CALLBACK* lpReadFromHardSID)(Uint8 DeviceID, Uint8 SID_reg);
  36. typedef void (CALLBACK* lpInitHardSID_Mapper)(void);
  37. typedef void (CALLBACK* lpMuteHardSID_Line)(int Mute);
  38. typedef void (CALLBACK* lpHardSID_Delay)(Uint8 DeviceID, Uint16 Cycles);
  39. typedef void (CALLBACK* lpHardSID_Write)(Uint8 DeviceID, Uint16 Cycles, Uint8 SID_reg, Uint8 Data);
  40. typedef void (CALLBACK* lpHardSID_Flush)(Uint8 DeviceID);
  41. typedef void (CALLBACK* lpHardSID_SoftFlush)(Uint8 DeviceID);
  42. lpWriteToHardSID WriteToHardSID = NULL;
  43. lpReadFromHardSID ReadFromHardSID = NULL;
  44. lpInitHardSID_Mapper InitHardSID_Mapper = NULL;
  45. lpMuteHardSID_Line MuteHardSID_Line = NULL;
  46. lpHardSID_Delay HardSID_Delay = NULL;
  47. lpHardSID_Write HardSID_Write = NULL;
  48. lpHardSID_Flush HardSID_Flush = NULL;
  49. lpHardSID_SoftFlush HardSID_SoftFlush = NULL;
  50. HINSTANCE hardsiddll = 0;
  51. int dll_initialized = FALSE;
  52. // Cycle-exact HardSID support
  53. int cycleexacthardsid = FALSE;
  54. SDL_Thread* playerthread = NULL;
  55. SDL_mutex* flushmutex = NULL;
  56. volatile int runplayerthread = FALSE;
  57. volatile int flushplayerthread = FALSE;
  58. volatile int suspendplayroutine = FALSE;
  59. int sound_thread(void *userdata);
  60.  
  61. void InitHardDLL(void);
  62.  
  63. // Win32 CatWeasel MK3 PCI output
  64. #define SID_SID_PEEK_POKE   CTL_CODE(FILE_DEVICE_SOUND,0x0800UL + 1,METHOD_BUFFERED,FILE_ANY_ACCESS)
  65. HANDLE catweaselhandle;
  66.  
  67. #else
  68.  
  69. // Unix HardSID & CatWeasel output
  70. int lefthardsidfd = -1;
  71. int righthardsidfd = -1;
  72. int catweaselfd = -1;
  73.  
  74. #endif
  75.  
  76. int sound_init(unsigned b, unsigned mr, unsigned writer, unsigned hardsid, unsigned m, unsigned ntsc, unsigned multiplier, unsigned catweasel, unsigned interpolate, unsigned customclockrate)
  77. {
  78.   int c;
  79.  
  80.   #ifdef __WIN32__
  81.   if (!flushmutex)
  82.       flushmutex = SDL_CreateMutex();
  83.   #endif
  84.  
  85.   sound_uninit();
  86.  
  87.   if (multiplier)
  88.   {
  89.     if (ntsc)
  90.     {
  91.       framerate = NTSCFRAMERATE * multiplier;
  92.       snd_bpmtempo = 150 * multiplier;
  93.     }
  94.     else
  95.     {
  96.       framerate = PALFRAMERATE * multiplier;
  97.       snd_bpmtempo = 125 * multiplier;
  98.     }
  99.   }
  100.   else
  101.   {
  102.     if (ntsc)
  103.     {
  104.       framerate = NTSCFRAMERATE / 2;
  105.       snd_bpmtempo = 150 / 2;
  106.     }
  107.     else
  108.     {
  109.       framerate = PALFRAMERATE / 2;
  110.       snd_bpmtempo = 125 / 2;
  111.     }
  112.   }
  113.  
  114.   if (hardsid)
  115.   {
  116.     lefthardsid = (hardsid & 0xf) - 1;
  117.     righthardsid = ((hardsid >> 4) & 0xf) - 1;
  118.  
  119.     if ((righthardsid == lefthardsid) || (righthardsid < 0))
  120.       righthardsid = lefthardsid + 1;
  121.  
  122.     #ifdef __WIN32__
  123.     InitHardDLL();
  124.     if (dll_initialized)
  125.     {
  126.       usehardsid = hardsid;
  127.  
  128.       for (c = 0; c < NUMSIDREGS; c++)
  129.       {
  130.         sidreg[c] = 0;
  131.         WriteToHardSID(lefthardsid, c, 0x00);
  132.         WriteToHardSID(righthardsid, c, 0x00);
  133.       }
  134.       MuteHardSID_Line(FALSE);
  135.     }
  136.     else return 0;
  137.     if (!cycleexacthardsid)
  138.     {
  139.       SDL_SetTimer(1000 / framerate, sound_timer);
  140.     }
  141.     else
  142.     {
  143.       runplayerthread = TRUE;
  144.       playerthread = SDL_CreateThread(sound_thread, NULL);
  145.       if (!playerthread) return 0;
  146.     }
  147.     #else
  148.     char filename[80];
  149.     sprintf(filename, "/dev/sid%d", lefthardsid);
  150.     lefthardsidfd = open(filename, O_WRONLY, S_IREAD|S_IWRITE);
  151.     sprintf(filename, "/dev/sid%d", righthardsid);
  152.     righthardsidfd = open(filename, O_WRONLY, S_IREAD|S_IWRITE);
  153.     if ((lefthardsidfd >= 0) && (righthardsidfd >= 0))
  154.     {
  155.       usehardsid = hardsid;
  156.       for (c = 0; c < NUMSIDREGS; c++)
  157.       {
  158.         Uint32 dataword = c << 8;
  159.         write(lefthardsidfd, &dataword, 4);
  160.         write(righthardsidfd, &dataword, 4);
  161.       }
  162.     }
  163.     else return 0;
  164.     SDL_SetTimer(1000 / framerate, sound_timer);
  165.     #endif
  166.     goto SOUNDOK;
  167.   }
  168.  
  169.   if (catweasel)
  170.   {
  171.     #ifdef __WIN32__
  172.     catweaselhandle = CreateFile("\\\\.\\SID6581_1", GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ, 0L,
  173.       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
  174.     if (catweaselhandle == INVALID_HANDLE_VALUE)
  175.       return 0;
  176.     #else
  177.     catweaselfd = open("/dev/sid", O_WRONLY);
  178.     if (catweaselfd < 0)
  179.       catweaselfd = open("/dev/misc/sid", O_WRONLY);
  180.     if (catweaselfd < 0)
  181.       return 0;
  182.     #ifndef __amigaos__
  183.     if (ntsc)
  184.       ioctl(catweaselfd, CWSID_IOCTL_NTSC);
  185.     else
  186.       ioctl(catweaselfd, CWSID_IOCTL_PAL);
  187.     #endif
  188.     #endif
  189.  
  190.     usecatweasel = 1;
  191.     SDL_SetTimer(1000 / framerate, sound_timer);
  192.     goto SOUNDOK;
  193.   }
  194.  
  195.   if (!lbuffer) lbuffer = malloc(MIXBUFFERSIZE * sizeof(Sint16));
  196.   if (!rbuffer) rbuffer = malloc(MIXBUFFERSIZE * sizeof(Sint16));
  197.   if ((!lbuffer) || (!rbuffer)) return 0;
  198.  
  199.   if (writer)
  200.     writehandle = fopen("sidaudio.raw", "wb");
  201.  
  202.   playspeed = mr;
  203.   if (playspeed < MINMIXRATE) playspeed = MINMIXRATE;
  204.   if (playspeed > MAXMIXRATE) playspeed = MAXMIXRATE;
  205.   if (b < MINBUF) b = MINBUF;
  206.   if (b > MAXBUF) b = MAXBUF;
  207.  
  208.   if (firsttimeinit)
  209.   {
  210.     if (!snd_init(mr, SIXTEENBIT|STEREO, b, 1, 0)) return 0;
  211.     firsttimeinit = 0;
  212.   }
  213.   playspeed = snd_mixrate;
  214.   sid_init(playspeed, m, ntsc, interpolate & 1, customclockrate, interpolate >> 1);
  215.  
  216.   snd_player = &sound_playrout;
  217.   snd_setcustommixer(sound_mixer);
  218.  
  219.   SOUNDOK:
  220.   initted = 1;
  221.   return 1;
  222. }
  223.  
  224. void sound_uninit(void)
  225. {
  226.   int c;
  227.  
  228.   if (!initted) return;
  229.   initted = 0;
  230.  
  231.   // Apparently a delay is needed to make sure the sound timer thread is
  232.   // not mixing stuff anymore, and we can safely delete related structures
  233.   SDL_Delay(50);
  234.  
  235.   if (usehardsid || usecatweasel)
  236.   {
  237.     #ifdef __WIN32__
  238.     if (!playerthread)
  239.     {
  240.       SDL_SetTimer(0, NULL);
  241.     }
  242.     else
  243.     {
  244.       runplayerthread = FALSE;
  245.       SDL_WaitThread(playerthread, NULL);
  246.       playerthread = NULL;
  247.     }
  248.     #else
  249.     SDL_SetTimer(0, NULL);
  250.     #endif
  251.   }
  252.   else
  253.   {
  254.       snd_setcustommixer(NULL);
  255.     snd_player = NULL;
  256.   }
  257.  
  258.   if (writehandle)
  259.   {
  260.     fclose(writehandle);
  261.     writehandle = NULL;
  262.   }
  263.  
  264.   if (lbuffer)
  265.   {
  266.       free(lbuffer);
  267.       lbuffer = NULL;
  268.   }
  269.   if (rbuffer)
  270.   {
  271.       free(rbuffer);
  272.       rbuffer = NULL;
  273.   }
  274.  
  275.   if (usehardsid)
  276.   {
  277.     #ifdef __WIN32__
  278.     for (c = 0; c < NUMSIDREGS; c++)
  279.     {
  280.       WriteToHardSID(lefthardsid, c, 0x00);
  281.       WriteToHardSID(righthardsid, c, 0x00);
  282.     }
  283.     MuteHardSID_Line(TRUE);
  284.     #else
  285.     if ((lefthardsidfd >= 0) && (righthardsidfd >= 0))
  286.     {
  287.       for (c = 0; c < NUMSIDREGS; c++)
  288.       {
  289.         Uint32 dataword = c << 8;
  290.         write(lefthardsidfd, &dataword, 4);
  291.         write(righthardsidfd, &dataword, 4);
  292.       }
  293.       close(lefthardsidfd);
  294.       close(righthardsidfd);
  295.       lefthardsidfd = -1;
  296.       righthardsidfd = -1;
  297.     }
  298.     #endif
  299.   }
  300.  
  301.   if (usecatweasel)
  302.   {
  303.     #ifdef __WIN32__
  304.     DWORD w;
  305.     unsigned char buf[NUMSIDREGS * 2];
  306.     for (w = 0; w < NUMSIDREGS; w++)
  307.     {
  308.       buf[w*2] = 0x18 - w;
  309.       buf[w*2+1] = 0;
  310.     }
  311.     DeviceIoControl(catweaselhandle, SID_SID_PEEK_POKE, buf, sizeof(buf), 0L, 0UL, &w, 0L);
  312.     CloseHandle(catweaselhandle);
  313.     catweaselhandle = INVALID_HANDLE_VALUE;
  314.     #else
  315.     if (catweaselfd >= 0)
  316.     {
  317.       unsigned char buf[NUMSIDREGS];
  318.       memset(buf, 0, sizeof(buf));
  319.       lseek(catweaselfd, 0, SEEK_SET);
  320.       write(catweaselfd, buf, sizeof(buf));
  321.       close(catweaselfd);
  322.       catweaselfd = -1;
  323.     }
  324.     #endif
  325.   }
  326. }
  327.  
  328. void sound_suspend(void)
  329. {
  330.   #ifdef __WIN32__
  331.   SDL_LockMutex(flushmutex);
  332.   suspendplayroutine = TRUE;
  333.   SDL_UnlockMutex(flushmutex);
  334.   #endif
  335. }
  336.  
  337. void sound_flush(void)
  338. {
  339.   #ifdef __WIN32__
  340.   SDL_LockMutex(flushmutex);
  341.   flushplayerthread = TRUE;
  342.   SDL_UnlockMutex(flushmutex);
  343.   #endif
  344. }
  345.  
  346. Uint32 sound_timer(Uint32 interval)
  347. {
  348.   if (!initted) return interval;
  349.   sound_playrout();
  350.   return interval;
  351. }
  352.  
  353. #ifdef __WIN32__
  354. int sound_thread(void *userdata)
  355. {
  356.   unsigned long flush_cycles_interactive = hardsidbufinteractive * 1000; /* 0 = flush off for interactive mode*/
  357.   unsigned long flush_cycles_playback = hardsidbufplayback * 1000; /* 0 = flush off for playback mode*/
  358.   unsigned long cycles_after_flush = 0;
  359.   boolean interactive;
  360.  
  361.   while (runplayerthread)
  362.   {
  363.     unsigned cycles = 1000000 / framerate; // HardSID should be clocked at 1MHz
  364.     int c;
  365.  
  366.     if (flush_cycles_interactive > 0 || flush_cycles_playback > 0)
  367.     {
  368.       cycles_after_flush += cycles;
  369.     }
  370.  
  371.       // Do flush if starting playback, stopping playback, starting an interactive note etc.
  372.     if (flushplayerthread)
  373.     {
  374.         SDL_LockMutex(flushmutex);
  375.       if (HardSID_Flush)
  376.       {
  377.         HardSID_Flush(lefthardsid);
  378.       }
  379.       // Can clear player suspend now (if set)
  380.       suspendplayroutine = FALSE;
  381.       flushplayerthread = FALSE;
  382.       SDL_UnlockMutex(flushmutex);
  383.  
  384.       SDL_Delay(0);
  385.     }
  386.  
  387.     if (!suspendplayroutine) playroutine();
  388.  
  389.     interactive = !(boolean)recordmode /* jam mode */ || !(boolean)isplaying();
  390.  
  391.     // Left side
  392.     for (c = 0; c < NUMSIDREGS; c++)
  393.     {
  394.       unsigned o = sid_getorder(c);
  395.  
  396.       // Extra delay before loading the waveform (and mt_chngate,x)
  397.       if ((o == 4) || (o == 11) || (o == 18))
  398.       {
  399.         HardSID_Write(lefthardsid, SIDWRITEDELAY+SIDWAVEDELAY, o, sidreg[o]);
  400.         cycles -= SIDWRITEDELAY+SIDWAVEDELAY;
  401.       }
  402.       else 
  403.       {
  404.         HardSID_Write(lefthardsid, SIDWRITEDELAY, o, sidreg[o]);
  405.         cycles -= SIDWRITEDELAY;
  406.       }
  407.     }
  408.     
  409.     // Right side
  410.     for (c = 0; c < NUMSIDREGS; c++)
  411.     {
  412.       unsigned o = sid_getorder(c);
  413.  
  414.       // Extra delay before loading the waveform (and mt_chngate,x)
  415.       if ((o == 4) || (o == 11) || (o == 18))
  416.       {
  417.         HardSID_Write(righthardsid, SIDWRITEDELAY+SIDWAVEDELAY, o, sidreg2[o]);
  418.         cycles -= SIDWRITEDELAY+SIDWAVEDELAY;
  419.       }
  420.       else 
  421.       {
  422.         HardSID_Write(righthardsid, SIDWRITEDELAY, o, sidreg2[o]);
  423.         cycles -= SIDWRITEDELAY;
  424.       }
  425.     }
  426.  
  427.     // Now wait the rest of frame
  428.     while (cycles)
  429.     {
  430.       unsigned runnow = cycles;
  431.       if (runnow > 65535) runnow = 65535;
  432.       HardSID_Delay(lefthardsid, runnow);
  433.       cycles -= runnow;
  434.     }
  435.  
  436.     if ((flush_cycles_interactive>0 && interactive && cycles_after_flush>=flush_cycles_interactive) ||
  437.       (flush_cycles_playback>0 && !interactive && cycles_after_flush>=flush_cycles_playback)) 
  438.     {
  439.       if (HardSID_SoftFlush)
  440.         HardSID_SoftFlush(lefthardsid);
  441.       cycles_after_flush = 0;
  442.     }
  443.   }
  444.  
  445.   unsigned r;
  446.  
  447.   for (r = 0; r < NUMSIDREGS; r++)
  448.   {
  449.     HardSID_Write(lefthardsid, SIDWRITEDELAY, r, 0);
  450.     HardSID_Write(righthardsid, SIDWRITEDELAY, r, 0);
  451.   }
  452.   if (HardSID_SoftFlush)
  453.     HardSID_SoftFlush(lefthardsid);
  454.  
  455.   return 0;
  456. }
  457. #endif
  458.  
  459. void sound_playrout(void)
  460. {
  461.   int c;
  462.  
  463.   playroutine();
  464.   if (usehardsid)
  465.   {
  466.     #ifdef __WIN32__
  467.     for (c = 0; c < NUMSIDREGS; c++)
  468.     {
  469.       unsigned o = sid_getorder(c);
  470.       WriteToHardSID(lefthardsid, o, sidreg[o]);
  471.       WriteToHardSID(righthardsid, o, sidreg2[o]);
  472.     }
  473.     #else
  474.     for (c = 0; c < NUMSIDREGS; c++)
  475.     {
  476.       unsigned o = sid_getorder(c);
  477.       Uint32 dataword = (o << 8) | sidreg[o];
  478.       write(lefthardsidfd, &dataword, 4);
  479.       dataword = (o << 8) | sidreg2[o];
  480.       write(righthardsidfd, &dataword, 4);
  481.     }
  482.     #endif
  483.   }
  484.   else if (usecatweasel)
  485.   {
  486.     #ifdef __WIN32__
  487.     DWORD w;
  488.     unsigned char buf[NUMSIDREGS * 2];
  489.  
  490.     for(w = 0; w < NUMSIDREGS; w++)
  491.     {
  492.       unsigned o = sid_getorder(w);
  493.  
  494.       buf[w*2] = o;
  495.       buf[w*2+1] = sidreg[o];
  496.     }
  497.     DeviceIoControl(catweaselhandle, SID_SID_PEEK_POKE, buf, sizeof(buf), 0L, 0UL, &w, 0L);
  498.     #else
  499.     for (c = 0; c < NUMSIDREGS; c++)
  500.     {
  501.       unsigned o = sid_getorder(c);
  502.  
  503.       lseek(catweaselfd, o, SEEK_SET);
  504.       write(catweaselfd, &sidreg[o], 1);
  505.     }
  506.     #endif
  507.   }
  508. }
  509.  
  510. void sound_mixer(Sint32 *dest, unsigned samples)
  511. {
  512.   int c;
  513.  
  514.   if (!initted) return;
  515.   if (samples > MIXBUFFERSIZE) return;
  516.  
  517.   sid_fillbuffer(lbuffer, rbuffer, samples);
  518.   if (writehandle)
  519.   {
  520.     for (c = 0; c < samples; c++)
  521.     {
  522.       fwrite(&lbuffer[c], sizeof(Sint16), 1, writehandle);
  523.       fwrite(&rbuffer[c], sizeof(Sint16), 1, writehandle);
  524.     }
  525.   }
  526.   for (c = 0; c < samples; c++)
  527.   {
  528.     dest[c*2] = lbuffer[c];
  529.     dest[c*2+1] = rbuffer[c];
  530.   }
  531. }
  532.  
  533. #ifdef __WIN32__
  534. void InitHardDLL()
  535. {
  536.   if (!(hardsiddll=LoadLibrary("HARDSID.DLL"))) return;
  537.  
  538.   WriteToHardSID = (lpWriteToHardSID) GetProcAddress(hardsiddll, "WriteToHardSID");
  539.   ReadFromHardSID = (lpReadFromHardSID) GetProcAddress(hardsiddll, "ReadFromHardSID");
  540.   InitHardSID_Mapper = (lpInitHardSID_Mapper) GetProcAddress(hardsiddll, "InitHardSID_Mapper");
  541.   MuteHardSID_Line = (lpMuteHardSID_Line) GetProcAddress(hardsiddll, "MuteHardSID_Line");
  542.  
  543.   if (!WriteToHardSID) return;
  544.  
  545.   // Try to get cycle-exact interface
  546.   HardSID_Delay = (lpHardSID_Delay) GetProcAddress(hardsiddll, "HardSID_Delay");
  547.   HardSID_Write = (lpHardSID_Write) GetProcAddress(hardsiddll, "HardSID_Write");
  548.   HardSID_Flush = (lpHardSID_Flush) GetProcAddress(hardsiddll, "HardSID_Flush");
  549.   HardSID_SoftFlush = (lpHardSID_SoftFlush) GetProcAddress(hardsiddll, "HardSID_SoftFlush");
  550.   if ((HardSID_Delay) && (HardSID_Write) && (HardSID_Flush) && (HardSID_SoftFlush))
  551.     cycleexacthardsid = TRUE;
  552.   
  553.   InitHardSID_Mapper();
  554.   dll_initialized = TRUE;
  555. }
  556. #endif
  557.