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