home *** CD-ROM | disk | FTP | other *** search
/ ftp.4front-tech.com / ftp.4front-tech.com.tar / ftp.4front-tech.com / ossfree / snd-util-3.8.tar.gz / snd-util-3.8.tar / sndkit / OSSlib / libmain.c < prev    next >
C/C++ Source or Header  |  1997-02-28  |  4KB  |  217 lines

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <sys/soundcard.h>
  7.  
  8. static int seqfd = -1;
  9. static int initialized = 0;
  10.  
  11. unsigned char *_seqbuf;
  12. int _seqbuflen = 0;
  13. int _seqbufptr = 0;
  14.  
  15. int synth_types[64] = {0};
  16. #define ST_NONE        0
  17. #define ST_GUS        1
  18. #define ST_OPL        2
  19.  
  20. int nrsynths = 0;
  21.  
  22. extern int gusload (int seqfd, int type, int dev, int pgm);
  23. extern int gusinit (int seqfd, int dev);
  24.  
  25. void
  26. seqbuf_dump ()
  27. {
  28.   if (_seqbufptr)
  29.     if (write (seqfd, _seqbuf, _seqbufptr) == -1)
  30.       {
  31.     perror ("write /dev/sequencer");
  32.     exit (-1);
  33.       }
  34.   _seqbufptr = 0;
  35. }
  36.  
  37. static int
  38. oss_init_lib(void)
  39. {
  40.     int i, synth;
  41.     struct synth_info info;
  42.  
  43.     if (_seqbuflen < 32 || _seqbuflen > 2048)
  44.        _seqbuflen = 2048;
  45.  
  46.     if ((_seqbuf=malloc(_seqbuflen))==NULL)
  47.         return 3;
  48.  
  49.     nrsynths = 0;
  50.     if (ioctl(seqfd, SNDCTL_SEQ_NRSYNTHS, &nrsynths)==-1)
  51.     {
  52.         perror("/dev/sequencer (NRSYNTHS");
  53.         exit(-1);
  54.     }
  55.  
  56.     for (synth=0;synth < nrsynths;synth++)
  57.     {
  58.         info.device = synth;
  59.  
  60.         if (ioctl(seqfd, SNDCTL_SYNTH_ID, &info)==-1)
  61.         {
  62.             perror("/dev/sequencer (SYNTH_ID");
  63.             fprintf(stderr, "OSS version 3.8 or later is required with OSSlib\n");
  64.             exit(-1);
  65.         }
  66.  
  67.         if (strcmp(info.name, "GUS")==0 ||
  68.             strcmp(info.name, "IWAVE")==0 ||
  69.             strcmp(info.name, "SoftOSS")==0)
  70.           {
  71.             synth_types[synth] = ST_GUS;
  72.             gusinit(seqfd, synth);
  73.           }
  74.         else
  75.         if (strcmp(info.name, "OPL")==0)
  76.           {
  77.             synth_types[synth] = ST_OPL;
  78.             opl3init(seqfd, synth);
  79.           }
  80.     }
  81.  
  82.     initialized = 1;
  83.     return 0; /* OK */
  84. }
  85.  
  86. int
  87. OSS_init(int userfd, int buflen)
  88. {
  89.     if (_seqbuflen || _seqbuflen || seqfd != -1 || initialized)
  90.     {
  91.         fprintf(stderr, "libOSS: OSS_init called too late\n");
  92.         return 1;
  93.     }
  94.  
  95.     seqfd = userfd;
  96.  
  97.     if (buflen < 32 || buflen > 2048)
  98.     {
  99.         fprintf(stderr, "libOSS: OSS_init called with invalid buflen\n");
  100.         return 2;
  101.     }
  102.  
  103.     _seqbuflen = buflen;
  104.  
  105.     return oss_init_lib();
  106. }
  107.  
  108. static void
  109. sanity_check(int fd, unsigned char *buf, int buflen)
  110. {
  111.     if (seqfd != fd)
  112.     if (seqfd == -1)
  113.        seqfd = fd;
  114.     else
  115.       {
  116.          fprintf(stderr, "OSSlib: seqfd is inconsistent\n");
  117.       }
  118.  
  119.     if (buf != _seqbuf)
  120.     {
  121.        fprintf(stderr, "OSSlib: _seqbuf is inconsistent\n");
  122.     }
  123.  
  124.     if (buflen != _seqbuflen)
  125.     {
  126.        fprintf(stderr, "OSSlib: _seqbuf is inconsistent\n");
  127.     }
  128.  
  129.     if (!initialized)
  130.        if (oss_init_lib() != 0)
  131.        {
  132.         fprintf(stderr, "OSSlib: Library initialization failed\n");
  133.         exit(-1);
  134.        }
  135. }
  136.  
  137. void
  138. OSS_seqbuf_dump(int fd, unsigned char *buf, int buflen)
  139. {
  140.     sanity_check(fd, buf, buflen);
  141.  
  142.     write(seqfd, _seqbuf, _seqbufptr);
  143.     _seqbufptr = 0;
  144. }
  145.  
  146. void
  147. OSS_seq_advbuf(int len, int fd, unsigned char *buf, int buflen)
  148. {
  149.     sanity_check(fd, buf, buflen);
  150.     _seqbufptr += len;
  151. }
  152.  
  153. void
  154. OSS_seq_needbuf(int len, int fd, unsigned char *buf, int buflen)
  155. {
  156.     sanity_check(fd, buf, buflen);
  157.  
  158.     if ((_seqbufptr+len) > _seqbuflen)
  159.     {
  160.         write(seqfd, _seqbuf, _seqbufptr);
  161.         _seqbufptr = 0;
  162.     }    
  163. }
  164.  
  165. void
  166. OSS_patch_caching(int dev, int chn, int patch, int fd, unsigned char *buf, int buflen)
  167. {
  168.     sanity_check(fd, buf, buflen);
  169.  
  170.     switch (synth_types[dev])
  171.     {
  172.     case ST_GUS:
  173.         gusload(seqfd, 0, dev, patch);
  174.         break;
  175.     case ST_OPL:
  176.         opl3load(seqfd, 0, dev, patch);
  177.         break;
  178.     }
  179. }
  180.  
  181. void
  182. OSS_drum_caching(int dev, int chn, int patch, int fd, unsigned char *buf, int buflen)
  183. {
  184.     sanity_check(fd, buf, buflen);
  185.  
  186.     switch (synth_types[dev])
  187.     {
  188.     case ST_GUS:
  189.         gusload(seqfd, 0, dev, patch+128);
  190.         break;
  191.     case ST_OPL:
  192.         opl3load(seqfd, 0, dev, patch+128);
  193.         break;
  194.     }
  195. }
  196.  
  197. void
  198. OSS_write_patch(int fd, unsigned char *buf, int len)
  199. {
  200.     sanity_check(fd, _seqbuf, _seqbuflen);
  201.  
  202.     if (write(fd, buf, len)==-1)
  203.     {
  204.        perror("OSS_write_patch");
  205.        exit(-1);
  206.     }
  207. }
  208.  
  209. int
  210. OSS_write_patch2(int fd, unsigned char *buf, int len)
  211. {
  212.     sanity_check(fd, _seqbuf, _seqbuflen);
  213.  
  214.     return write(fd, buf, len);
  215. }
  216.  
  217.