home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / linux / mikmod-3.000 / mikmod-3 / mikmod-3.1.2 / drivers / drv_oss.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-07  |  4.5 KB  |  196 lines

  1. /*    MikMod sound library
  2.     (c) 1998 Miodrag Vallat and others - see file AUTHORS for complete list
  3.  
  4.     This library is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU Library General Public License as
  6.     published by the Free Software Foundation; either version 2 of
  7.     the License, or (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public
  15.     License along with this library; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /*==============================================================================
  20.  
  21.   $Id: drv_oss.c,v 1.16 1998/12/07 06:01:45 miod Exp $
  22.  
  23.   Mikmod driver for output on linux and FreeBSD Open Sound System (OSS)
  24.   (/dev/dsp) 
  25.  
  26. ==============================================================================*/
  27.  
  28. /*
  29.  
  30.     Written by Chris Conn <cconn@tohs.abacom.com>
  31.  
  32.     You can use the environment variables 'MM_FRAGSIZE' and 'MM_NUMFRAGS' to 
  33.     override the default size & number of audio buffer fragments. If you 
  34.     experience crackles & pops, try experimenting with these values.
  35.  
  36.     In general, the slower your system, the higher these values need to be. 
  37.  
  38.     MM_NUMFRAGS is within the range 2 to 255 (decimal)
  39.     MM_FRAGSIZE is is within the range 7 to 17 (dec).
  40.     The requested fragment size will be 2^MM_FRAGSIZE
  41.  
  42. */
  43.  
  44. #ifdef HAVE_CONFIG_H
  45. #include "config.h"
  46. #endif
  47.  
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #include <errno.h>
  52. #ifdef HAVE_FCNTL_H
  53. #include <fcntl.h>
  54. #endif
  55. #include <stdlib.h>
  56. #ifdef HAVE_SYS_IOCTL_H
  57. #include <sys/ioctl.h>
  58. #endif
  59. #ifdef __FreeBSD__
  60. #include <machine/soundcard.h>
  61. #else 
  62. #include <sys/soundcard.h>
  63. #endif
  64.  
  65. #include <mikmod_internals.h>
  66.  
  67. #define DEFAULT_FRAGSIZE 17
  68. #define DEFAULT_NUMFRAGS 4
  69.  
  70. static    int sndfd=-1;
  71. static    int fragmentsize;
  72. static    SBYTE *audiobuffer=NULL;
  73.  
  74. static BOOL OSS_IsThere(void)
  75. {
  76.     /* under Linux, and perhaps other Unixes, access()ing the device is not
  77.        enough since it won't fail if the machine doesn't have sound support
  78.        in the kernel or sound hardware                                      */
  79.     int fd;
  80.  
  81.     if((fd=open("/dev/dsp",O_WRONLY))>0) {
  82.         close(fd);
  83.         return 1;
  84.     }
  85.     return (errno==EACCES?1:0);
  86. }
  87.  
  88. static BOOL OSS_Init(void)
  89. {
  90.     char *env;
  91.     int play_precision,play_stereo,play_rate;
  92.     int fragsize,numfrags;
  93.     
  94.     if((sndfd=open("/dev/dsp",O_WRONLY))<0) {
  95.         _mm_errno=MMERR_OPENING_AUDIO;
  96.         return 1;
  97.     }
  98.  
  99.     fragsize=(env=getenv("MM_FRAGSIZE"))?atoi(env):DEFAULT_FRAGSIZE;
  100.     numfrags=(env=getenv("MM_NUMFRAGS"))?atoi(env):DEFAULT_NUMFRAGS;
  101.         
  102.     if(fragsize<7||fragsize>17) fragsize=DEFAULT_FRAGSIZE;
  103.     if(numfrags<2||numfrags>255) numfrags=DEFAULT_NUMFRAGS;
  104.  
  105.     fragmentsize=(numfrags<<16)|fragsize;
  106.     
  107.     if(ioctl(sndfd,SNDCTL_DSP_SETFRAGMENT,&fragmentsize)<0) {
  108.         _mm_errno=MMERR_OSS_SETFRAGMENT;
  109.         return 1;
  110.     }
  111.  
  112.     play_precision=(md_mode&DMODE_16BITS)?16:8;
  113.     play_stereo=(md_mode&DMODE_STEREO)?1:0;
  114.     play_rate=md_mixfreq;
  115.  
  116.     if(ioctl(sndfd,SNDCTL_DSP_SAMPLESIZE,&play_precision)<0) {
  117.         _mm_errno=MMERR_OSS_SETSAMPLESIZE;
  118.         return 1;
  119.     }
  120.     if(ioctl(sndfd,SNDCTL_DSP_STEREO,&play_stereo)<0) {
  121.         _mm_errno=MMERR_OSS_SETSTEREO;
  122.         return 1;
  123.     }
  124.     if(ioctl(sndfd,SNDCTL_DSP_SPEED,&play_rate)<0) {
  125.         _mm_errno=MMERR_OSS_SETSPEED;
  126.         return 1;
  127.     }
  128.  
  129.     ioctl(sndfd, SNDCTL_DSP_GETBLKSIZE, &fragmentsize);
  130.  
  131.     if(!(audiobuffer=(SBYTE*)_mm_malloc(fragmentsize*numfrags*sizeof(SBYTE)))) {
  132.         return 1;
  133.     }
  134.  
  135.     if(VC_Init()) {
  136.         return 1;
  137.     }
  138.     return 0;
  139. }
  140.  
  141. static void OSS_Exit(void)
  142. {
  143.     VC_Exit();
  144.     if (audiobuffer) {
  145.         free(audiobuffer);
  146.         audiobuffer=NULL;
  147.     }
  148.     if (sndfd>=0) {
  149.         close(sndfd);
  150.         sndfd=-1;
  151.     }
  152. }
  153.  
  154. static void OSS_Update(void)
  155. {
  156.     audio_buf_info buffinf;
  157.  
  158.     ioctl(sndfd, SNDCTL_DSP_GETOSPACE,&buffinf);
  159.     write(sndfd,audiobuffer,
  160.           VC_WriteBytes(audiobuffer,buffinf.fragments*buffinf.fragsize));
  161. }
  162.  
  163. static BOOL OSS_Reset(void)
  164. {
  165.     VC_Exit();
  166.     ioctl(sndfd,SNDCTL_DSP_RESET);
  167.     return VC_Init();
  168. }
  169.  
  170. MDRIVER drv_oss={
  171.     NULL,
  172.     "Open Sound System (OSS)",
  173.     "Open Sound System (OSS) driver v1.3",
  174.     0,255,
  175.     OSS_IsThere,
  176.     VC_SampleLoad,
  177.     VC_SampleUnload,
  178.     VC_SampleSpace,
  179.     VC_SampleLength,
  180.     OSS_Init,
  181.     OSS_Exit,
  182.     OSS_Reset,
  183.     VC_SetNumVoices,
  184.     VC_PlayStart,
  185.     VC_PlayStop,
  186.     OSS_Update,
  187.     VC_VoiceSetVolume,
  188.     VC_VoiceSetFrequency,
  189.     VC_VoiceSetPanning,
  190.     VC_VoicePlay,
  191.     VC_VoiceStop,
  192.     VC_VoiceStopped,
  193.     VC_VoiceGetPosition,
  194.     VC_VoiceRealVolume
  195. };
  196.