home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.67_stereo.zip / src / gsound.c < prev    next >
C/C++ Source or Header  |  2008-05-17  |  14KB  |  551 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.     if (ntsc)
  183.       ioctl(catweaselfd, CWSID_IOCTL_NTSC);
  184.     else
  185.       ioctl(catweaselfd, CWSID_IOCTL_PAL);
  186.     #endif
  187.  
  188.     usecatweasel = 1;
  189.     SDL_SetTimer(1000 / framerate, sound_timer);
  190.     goto SOUNDOK;
  191.   }
  192.  
  193.   lbuffer = malloc(MIXBUFFERSIZE * sizeof(Sint16));
  194.   rbuffer = malloc(MIXBUFFERSIZE * sizeof(Sint16));
  195.   if ((!lbuffer) || (!rbuffer)) return 0;
  196.  
  197.   if (writer)
  198.     writehandle = fopen("sidaudio.raw", "wb");
  199.  
  200.   playspeed = mr;
  201.   if (playspeed < MINMIXRATE) playspeed = MINMIXRATE;
  202.   if (playspeed > MAXMIXRATE) playspeed = MAXMIXRATE;
  203.   if (b < MINBUF) b = MINBUF;
  204.   if (b > MAXBUF) b = MAXBUF;
  205.  
  206.   if (firsttimeinit)
  207.   {
  208.     if (!snd_init(mr, SIXTEENBIT|STEREO, b, 1, 0)) return 0;
  209.     firsttimeinit = 0;
  210.   }
  211.   playspeed = snd_mixrate;
  212.   sid_init(playspeed, m, ntsc, interpolate, customclockrate);
  213.  
  214.   snd_player = &sound_playrout;
  215.   snd_setcustommixer(sound_mixer);
  216.  
  217.   SOUNDOK:
  218.   initted = 1;
  219.   atexit(sound_uninit);
  220.   return 1;
  221. }
  222.  
  223. void sound_uninit(void)
  224. {
  225.   int c;
  226.  
  227.   if (!initted) return;
  228.   initted = 0;
  229.  
  230.   if (usehardsid || usecatweasel)
  231.   {
  232.     #ifdef __WIN32__
  233.     if (!playerthread)
  234.     {
  235.       SDL_SetTimer(0, NULL);
  236.     }
  237.     else
  238.     {
  239.       runplayerthread = FALSE;
  240.       SDL_WaitThread(playerthread, NULL);
  241.       playerthread = NULL;
  242.     }
  243.     #else
  244.     SDL_SetTimer(0, NULL);
  245.     #endif
  246.   }
  247.   else
  248.   {
  249.       snd_setcustommixer(NULL);
  250.     snd_player = NULL;
  251.   }
  252.  
  253.   if (writehandle)
  254.   {
  255.     fclose(writehandle);
  256.     writehandle = NULL;
  257.   }
  258.  
  259.   if (lbuffer)
  260.   {
  261.       free(lbuffer);
  262.       lbuffer = NULL;
  263.   }
  264.   if (rbuffer)
  265.   {
  266.       free(rbuffer);
  267.       rbuffer = NULL;
  268.   }
  269.  
  270.   if (usehardsid)
  271.   {
  272.     #ifdef __WIN32__
  273.     for (c = 0; c < NUMSIDREGS; c++)
  274.     {
  275.       WriteToHardSID(lefthardsid, c, 0x00);
  276.       WriteToHardSID(righthardsid, c, 0x00);
  277.     }
  278.     MuteHardSID_Line(TRUE);
  279.     #else
  280.     if ((lefthardsidfd >= 0) && (righthardsidfd >= 0))
  281.     {
  282.       for (c = 0; c < NUMSIDREGS; c++)
  283.       {
  284.         Uint32 dataword = c << 8;
  285.         write(lefthardsidfd, &dataword, 4);
  286.         write(righthardsidfd, &dataword, 4);
  287.       }
  288.       close(lefthardsidfd);
  289.       close(righthardsidfd);
  290.       lefthardsidfd = -1;
  291.       righthardsidfd = -1;
  292.     }
  293.     #endif
  294.   }
  295.  
  296.   if (usecatweasel)
  297.   {
  298.     #ifdef __WIN32__
  299.     DWORD w;
  300.     unsigned char buf[NUMSIDREGS * 2];
  301.     for (w = 0; w < NUMSIDREGS; w++)
  302.     {
  303.       buf[w*2] = 0x18 - w;
  304.       buf[w*2+1] = 0;
  305.     }
  306.     DeviceIoControl(catweaselhandle, SID_SID_PEEK_POKE, buf, sizeof(buf), 0L, 0UL, &w, 0L);
  307.     CloseHandle(catweaselhandle);
  308.     catweaselhandle = INVALID_HANDLE_VALUE;
  309.     #else
  310.     if (catweaselfd >= 0)
  311.     {
  312.       unsigned char buf[NUMSIDREGS];
  313.       memset(buf, 0, sizeof(buf));
  314.       lseek(catweaselfd, 0, SEEK_SET);
  315.       write(catweaselfd, buf, sizeof(buf));
  316.       close(catweaselfd);
  317.       catweaselfd = -1;
  318.     }
  319.     #endif
  320.   }
  321. }
  322.  
  323. void sound_suspend(void)
  324. {
  325.   #ifdef __WIN32__
  326.   SDL_LockMutex(flushmutex);
  327.   suspendplayroutine = TRUE;
  328.   SDL_UnlockMutex(flushmutex);
  329.   #endif
  330. }
  331.  
  332. void sound_flush(void)
  333. {
  334.   #ifdef __WIN32__
  335.   SDL_LockMutex(flushmutex);
  336.   flushplayerthread = TRUE;
  337.   SDL_UnlockMutex(flushmutex);
  338.   #endif
  339. }
  340.  
  341. Uint32 sound_timer(Uint32 interval)
  342. {
  343.   if (!initted) return interval;
  344.   sound_playrout();
  345.   return interval;
  346. }
  347.  
  348. #ifdef __WIN32__
  349. int sound_thread(void *userdata)
  350. {
  351.   unsigned long flush_cycles_interactive = hardsidbufinteractive * 1000; /* 0 = flush off for interactive mode*/
  352.   unsigned long flush_cycles_playback = hardsidbufplayback * 1000; /* 0 = flush off for playback mode*/
  353.   unsigned long cycles_after_flush = 0;
  354.   boolean interactive;
  355.  
  356.   while (runplayerthread)
  357.   {
  358.     unsigned cycles = 1000000 / framerate; // HardSID should be clocked at 1MHz
  359.     int c;
  360.  
  361.     if (flush_cycles_interactive > 0 || flush_cycles_playback > 0)
  362.     {
  363.       cycles_after_flush += cycles;
  364.     }
  365.  
  366.       // Do flush if starting playback, stopping playback, starting an interactive note etc.
  367.     if (flushplayerthread)
  368.     {
  369.         SDL_LockMutex(flushmutex);
  370.       if (HardSID_Flush)
  371.       {
  372.         HardSID_Flush(lefthardsid);
  373.       }
  374.       // Can clear player suspend now (if set)
  375.       suspendplayroutine = FALSE;
  376.       flushplayerthread = FALSE;
  377.       SDL_UnlockMutex(flushmutex);
  378.  
  379.       SDL_Delay(0);
  380.     }
  381.  
  382.     if (!suspendplayroutine) playroutine();
  383.  
  384.     interactive = !(boolean)recordmode /* jam mode */ || !(boolean)isplaying();
  385.  
  386.     // Left side
  387.     for (c = 0; c < NUMSIDREGS; c++)
  388.     {
  389.       unsigned o = sid_getorder(c);
  390.  
  391.         // Extra delay before loading the waveform (and mt_chngate,x)
  392.         if ((o == 4) || (o == 11) || (o == 18))
  393.         {
  394.         HardSID_Write(lefthardsid, SIDWRITEDELAY+SIDWAVEDELAY, o, sidreg[o]);
  395.           cycles -= SIDWRITEDELAY+SIDWAVEDELAY;
  396.       }
  397.         else 
  398.       {
  399.             HardSID_Write(lefthardsid, SIDWRITEDELAY, o, sidreg[o]);
  400.             cycles -= SIDWRITEDELAY;
  401.         }
  402.     }
  403.     
  404.     // Right side
  405.     for (c = 0; c < NUMSIDREGS; c++)
  406.     {
  407.       unsigned o = sid_getorder(c);
  408.  
  409.         // Extra delay before loading the waveform (and mt_chngate,x)
  410.         if ((o == 4) || (o == 11) || (o == 18))
  411.         {
  412.         HardSID_Write(righthardsid, SIDWRITEDELAY+SIDWAVEDELAY, o, sidreg2[o]);
  413.           cycles -= SIDWRITEDELAY+SIDWAVEDELAY;
  414.       }
  415.         else 
  416.       {
  417.             HardSID_Write(righthardsid, SIDWRITEDELAY, o, sidreg2[o]);
  418.             cycles -= SIDWRITEDELAY;
  419.         }
  420.     }
  421.  
  422.     // Now wait the rest of frame
  423.     while (cycles)
  424.     {
  425.       unsigned runnow = cycles;
  426.       if (runnow > 65535) runnow = 65535;
  427.       HardSID_Delay(lefthardsid, runnow);
  428.       cycles -= runnow;
  429.     }
  430.  
  431.       if ((flush_cycles_interactive>0 && interactive && cycles_after_flush>=flush_cycles_interactive) ||
  432.           (flush_cycles_playback>0 && !interactive && cycles_after_flush>=flush_cycles_playback)) 
  433.     {
  434.       if (HardSID_SoftFlush)
  435.         HardSID_SoftFlush(lefthardsid);
  436.           cycles_after_flush = 0;
  437.     }
  438.   }
  439.  
  440.   unsigned r;
  441.  
  442.   for (r = 0; r < NUMSIDREGS; r++)
  443.   {
  444.     HardSID_Write(lefthardsid, SIDWRITEDELAY, r, 0);
  445.     HardSID_Write(righthardsid, SIDWRITEDELAY, r, 0);
  446.   }
  447.   if (HardSID_SoftFlush)
  448.     HardSID_SoftFlush(lefthardsid);
  449.  
  450.   return 0;
  451. }
  452. #endif
  453.  
  454. void sound_playrout(void)
  455. {
  456.   int c;
  457.  
  458.   playroutine();
  459.   if (usehardsid)
  460.   {
  461.     #ifdef __WIN32__
  462.     for (c = 0; c < NUMSIDREGS; c++)
  463.     {
  464.       unsigned o = sid_getorder(c);
  465.       WriteToHardSID(lefthardsid, o, sidreg[o]);
  466.       WriteToHardSID(righthardsid, o, sidreg2[o]);
  467.     }
  468.     #else
  469.     for (c = 0; c < NUMSIDREGS; c++)
  470.     {
  471.       unsigned o = sid_getorder(c);
  472.       Uint32 dataword = (o << 8) | sidreg[o];
  473.       write(lefthardsidfd, &dataword, 4);
  474.       dataword = (o << 8) | sidreg2[o];
  475.       write(righthardsidfd, &dataword, 4);
  476.     }
  477.     #endif
  478.   }
  479.   else if (usecatweasel)
  480.   {
  481.     #ifdef __WIN32__
  482.     DWORD w;
  483.     unsigned char buf[NUMSIDREGS * 2];
  484.  
  485.     for(w = 0; w < NUMSIDREGS; w++)
  486.     {
  487.       unsigned o = sid_getorder(w);
  488.  
  489.       buf[w*2] = o;
  490.       buf[w*2+1] = sidreg[o];
  491.     }
  492.     DeviceIoControl(catweaselhandle, SID_SID_PEEK_POKE, buf, sizeof(buf), 0L, 0UL, &w, 0L);
  493.     #else
  494.     for (c = 0; c < NUMSIDREGS; c++)
  495.     {
  496.       unsigned o = sid_getorder(c);
  497.  
  498.       lseek(catweaselfd, o, SEEK_SET);
  499.       write(catweaselfd, &sidreg[o], 1);
  500.     }
  501.     #endif
  502.   }
  503. }
  504.  
  505. void sound_mixer(Sint32 *dest, unsigned samples)
  506. {
  507.   int c;
  508.  
  509.   if (!initted) return;
  510.   if (samples > MIXBUFFERSIZE) return;
  511.  
  512.   sid_fillbuffer(lbuffer, rbuffer, samples);
  513.   if (writehandle)
  514.   {
  515.     for (c = 0; c < samples; c++)
  516.     {
  517.       fwrite(&lbuffer[c], sizeof(Sint16), 1, writehandle);
  518.       fwrite(&rbuffer[c], sizeof(Sint16), 1, writehandle);
  519.     }
  520.   }
  521.   for (c = 0; c < samples; c++)
  522.   {
  523.     dest[c*2] = lbuffer[c];
  524.     dest[c*2+1] = rbuffer[c];
  525.   }
  526. }
  527.  
  528. #ifdef __WIN32__
  529. void InitHardDLL()
  530. {
  531.   if (!(hardsiddll=LoadLibrary("HARDSID.DLL"))) return;
  532.  
  533.   WriteToHardSID = (lpWriteToHardSID) GetProcAddress(hardsiddll, "WriteToHardSID");
  534.   ReadFromHardSID = (lpReadFromHardSID) GetProcAddress(hardsiddll, "ReadFromHardSID");
  535.   InitHardSID_Mapper = (lpInitHardSID_Mapper) GetProcAddress(hardsiddll, "InitHardSID_Mapper");
  536.   MuteHardSID_Line = (lpMuteHardSID_Line) GetProcAddress(hardsiddll, "MuteHardSID_Line");
  537.  
  538.   if (!WriteToHardSID) return;
  539.  
  540.   // Try to get cycle-exact interface
  541.   HardSID_Delay = (lpHardSID_Delay) GetProcAddress(hardsiddll, "HardSID_Delay");
  542.   HardSID_Write = (lpHardSID_Write) GetProcAddress(hardsiddll, "HardSID_Write");
  543.   HardSID_Flush = (lpHardSID_Flush) GetProcAddress(hardsiddll, "HardSID_Flush");
  544.   HardSID_SoftFlush = (lpHardSID_SoftFlush) GetProcAddress(hardsiddll, "HardSID_SoftFlush");
  545.   if ((HardSID_Delay) && (HardSID_Write)) cycleexacthardsid = TRUE;
  546.  
  547.   InitHardSID_Mapper();
  548.   dll_initialized = TRUE;
  549. }
  550. #endif
  551.