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_AF.c next >
Encoding:
C/C++ Source or Header  |  1998-12-07  |  4.4 KB  |  201 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_AF.c,v 1.17 1998/12/07 06:01:42 miod Exp $
  22.  
  23.   Mikmod driver for output on AF audio server.
  24.  
  25. ==============================================================================*/
  26.  
  27. /*
  28.  
  29.     Written by Roine Gustafsson <e93_rog@e.kth.se> Oct 25, 1995
  30.   
  31.     Portability:
  32.     Unixes running Digital AudioFile library, available from
  33.     ftp://crl.dec.com/pub/DEC/AF
  34.  
  35.     Usage:
  36.     Run the audio server (Aaxp&, Amsb&, whatever)
  37.     Set environment variable AUDIOFILE to something like 'mymachine:0'.
  38.     Remember, stereo is default! See commandline switches.
  39.  
  40.     I have a version which uses 2 computers for stereo.
  41.     Contact me if you want it.
  42.   
  43. */
  44.  
  45. #ifdef HAVE_CONFIG_H
  46. #include "config.h"
  47. #endif
  48.  
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h>
  51. #endif
  52. #ifdef HAVE_MALLOC_H
  53. #include <malloc.h>
  54. #endif
  55. #include <stdlib.h>
  56.  
  57. #include <AF/AFlib.h>
  58.  
  59. #include <mikmod_internals.h>
  60.  
  61. /* Global variables */
  62.  
  63. static    SBYTE *audiobuffer=NULL;
  64. static    int AFFragmentSize;
  65. static    AFAudioConn *AFaud=NULL;
  66. static    ATime AFtime;
  67. static    AC AFac;
  68. static    AFDeviceDescriptor *AFdesc;
  69.  
  70. static BOOL AF_IsThere(void)
  71. {
  72.     /* I'll think of a detection routine ... sometime */
  73.     return 1;
  74. }
  75.  
  76. static BOOL AF_Init(void)
  77. {
  78.     unsigned long mask;
  79.     AFSetACAttributes attributes;
  80.     int srate;
  81.     ADevice device;
  82.     int n;
  83.   
  84.     AFaud=AFOpenAudioConn("");
  85.     if (!AFaud) {
  86.         _mm_errno=MMERR_OPENING_AUDIO;
  87.         return 1;
  88.     }
  89.  
  90.     /* Search for a suitable device */
  91.     device=-1;
  92.     for (n=0;n<AFaud->ndevices;n++) {
  93.         AFdesc=AAudioDeviceDescriptor(AFaud,n);
  94.         if ((AFdesc->playNchannels==2)&&(md_mode&DMODE_STEREO)) {
  95.             device=n;
  96.             break;
  97.         }
  98.         if ((AFdesc->playNchannels==1)&&!(md_mode&DMODE_STEREO)) {
  99.             device=n;
  100.             break;
  101.         }
  102.     }
  103.     if (device==-1) {
  104.         _mm_errno=MMERR_AF_AUDIO_PORT;
  105.         return 1;
  106.     }
  107.  
  108.     attributes.preempt=Mix;
  109.     attributes.start_timeout=0;
  110.     attributes.end_silence=0;
  111.     /* in case of an 8bit device, the AF converts the 16 bit data to 8 bit */
  112.     attributes.type=LIN16;
  113.     attributes.channels=(md_mode&DMODE_STEREO)?Stereo:Mono;
  114.  
  115.     mask=ACPreemption|ACEncodingType|ACStartTimeout|ACEndSilence|ACChannels;
  116.     AFac=AFCreateAC(AFaud,device,mask,&attributes);
  117.     srate=AFac->device->playSampleFreq;
  118.  
  119.     md_mode|=DMODE_16BITS;            /* This driver only handles 16bits */
  120.     AFFragmentSize=(srate/40)*8;    /* Update 5 times/sec */
  121.     md_mixfreq=srate;                /* set mixing freq */
  122.  
  123.     if (md_mode&DMODE_STEREO) {
  124.         if (!(audiobuffer=(SBYTE*)_mm_malloc(2*2*AFFragmentSize))) 
  125.             return 1;
  126.     } else {
  127.         if (!(audiobuffer=(SBYTE*)_mm_malloc(2*AFFragmentSize))) 
  128.             return 1;
  129.     }
  130.   
  131.     return VC_Init();
  132. }
  133.  
  134. static BOOL AF_PlayStart(void)
  135. {
  136.     AFtime=AFGetTime(AFac);
  137.     return VC_PlayStart();
  138. }
  139.  
  140. static void AF_Exit(void)
  141. {
  142.     VC_Exit();
  143.     if (audiobuffer) {
  144.         free(audiobuffer);
  145.         audiobuffer=NULL;
  146.     }
  147.     if (AFaud) {
  148.         AFCloseAudioConn(AFaud);
  149.         AFaud=NULL;
  150.     }
  151. }
  152.  
  153. static BOOL AF_Reset(void)
  154. {
  155.     AF_Exit();
  156.     return AF_Init();
  157. }
  158.  
  159. static void AF_Update(void)
  160. {
  161.     ULONG done;
  162.   
  163.     done=VC_WriteBytes(audiobuffer,AFFragmentSize);
  164.     if (md_mode&DMODE_STEREO) {
  165.         AFPlaySamples(AFac,AFtime,done,(unsigned char*)audiobuffer);
  166.         AFtime+=done/4;
  167.         /* while (AFGetTime(AFac)<AFtime-1000); */
  168.     } else {
  169.         AFPlaySamples(AFac,AFtime,done,(unsigned char*)audiobuffer);
  170.         AFtime+=done/2;
  171.         /* while (AFGetTime(AFac)<AFtime-1000); */
  172.     }
  173. }
  174.  
  175. MDRIVER drv_AF={
  176.     NULL,
  177.     "AF driver",
  178.     "AudioFile driver v1.3",
  179.     0,255,
  180.     AF_IsThere,
  181.     VC_SampleLoad,
  182.     VC_SampleUnload,
  183.     VC_SampleSpace,
  184.     VC_SampleLength,
  185.     AF_Init,
  186.     AF_Exit,
  187.     AF_Reset,
  188.     VC_SetNumVoices,
  189.     AF_PlayStart,
  190.     VC_PlayStop,
  191.     AF_Update,
  192.     VC_VoiceSetVolume,
  193.     VC_VoiceSetFrequency,
  194.     VC_VoiceSetPanning,
  195.     VC_VoicePlay,
  196.     VC_VoiceStop,
  197.     VC_VoiceStopped,
  198.     VC_VoiceGetPosition,
  199.     VC_VoiceRealVolume
  200. };
  201.