Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

linux_pcsnd_audiodrv.cpp

Go to the documentation of this file.
00001 //
00002 // 1997/09/27 21:38:01
00003 //
00004 
00005 #include "audiodrv.h"
00006 
00007 audioDriver::audioDriver()
00008 {
00009         // Reset everything.
00010         errorString = "None";
00011         frequency = 0;
00012         channels = 0;
00013         encoding = 0;
00014         precision = 0;
00015         audioHd = (-1);
00016 }
00017 
00018 bool audioDriver::IsThere()
00019 {
00020         // Check device availability and write permissions.
00021         return (access(AUDIODEVICE,W_OK)==0);
00022 }
00023 
00024 bool audioDriver::Open(udword inFreq, int inPrecision, int inChannels,
00025                                            int inFragments, int inFragBase)
00026 {
00027         if ((audioHd=open(AUDIODEVICE,O_WRONLY,0)) == (-1))
00028     {
00029                 errorString = "AUDIO: Could not open audio device.";
00030                 return false;
00031     }
00032         
00033         // Transfer input parameters to this object.
00034         // May later be replaced with driver defaults.
00035         frequency = inFreq;
00036         precision = inPrecision;
00037         channels = inChannels;
00038         fragments = inFragments;
00039         fragSizeBase = inFragBase;
00040 
00041         // Set sample precision and type of encoding.
00042         int dsp_sampleSize;
00043         if (precision == SIDEMU_16BIT)
00044                 dsp_sampleSize = 16;
00045         else  // if (precision == SIDEMU_8BIT)
00046                 dsp_sampleSize = 8;
00047         if (ioctl(audioHd,SNDCTL_DSP_SAMPLESIZE,&dsp_sampleSize) == (-1))
00048     {
00049                 errorString = "AUDIO: Could not set sample size.";
00050                 return false;
00051     }
00052         // Verify and accept the sample precision the driver accepted.
00053         if (dsp_sampleSize == 16)
00054     {
00055                 precision = SIDEMU_16BIT;
00056                 encoding = SIDEMU_SIGNED_PCM;
00057     }
00058         else if (dsp_sampleSize == 8)
00059     {
00060                 precision = SIDEMU_8BIT;
00061                 encoding = SIDEMU_UNSIGNED_PCM;
00062     }
00063         else
00064         {
00065                 errorString = "AUDIO: Could not set sample size.";
00066                 return false;
00067         }
00068 
00069         // Set mono/stereo.
00070         int dsp_stereo;
00071         if (channels == SIDEMU_STEREO)
00072                 dsp_stereo = 1;
00073         else  // if (channels == SIDEMU_MONO)
00074                 dsp_stereo = 0;
00075         if (ioctl(audioHd,SNDCTL_DSP_STEREO,&dsp_stereo) == (-1))
00076     {
00077                 errorString = "AUDIO: Could not set mono/stereo.";
00078                 return false;
00079     }
00080         // Verify and accept the number of channels the driver accepted.
00081         if (dsp_stereo == 1)
00082                 channels = SIDEMU_STEREO;
00083         else if (dsp_stereo == 0)
00084                 channels = SIDEMU_MONO;
00085         else
00086     {
00087                 errorString = "AUDIO: Could not set mono/stereo.";
00088                 return false;
00089     }
00090         
00091         // Set frequency.
00092         int dsp_speed = frequency;
00093         if (ioctl(audioHd,SNDCTL_DSP_SPEED,&dsp_speed) == (-1))
00094     {
00095                 errorString = "AUDIO: Could not set frequency.";
00096                 return false;
00097     }
00098         // Accept the frequency the driver accepted.
00099         frequency = dsp_speed;
00100         
00101         // N fragments of size (2 ^ S) bytes
00102         //               NNNNSSSS
00103         // e.g. frag = 0x0004000e;
00104         // fragments should be out of [2,3,..,255]
00105         // fragSizeBase should be out of [7,8,...,17]
00106         // depending on the kernel audio driver buffer size
00107         int frag = (fragments << 16) | fragSizeBase;
00108         ioctl(audioHd,SNDCTL_DSP_SETFRAGMENT,&frag);
00109         ioctl(audioHd,SNDCTL_DSP_GETBLKSIZE,&blockSize);
00110         
00111     return true;
00112 }
00113 
00114 // Close an opened audio device, free any allocated buffers and
00115 // reset any variables that reflect the current state.
00116 void audioDriver::Close()
00117 {
00118         if (audioHd != (-1))
00119     {
00120                 close(audioHd);
00121                 audioHd = (-1);
00122     }
00123 }
00124 
00125 void audioDriver::Play(ubyte* pBuffer, int bufferSize)
00126 {
00127         if (audioHd != (-1))
00128         {
00129                 write(audioHd,pBuffer,bufferSize);
00130         }
00131 }

Generated on Tue Feb 8 04:13:58 2005 for Esidplay by doxygen 1.3.3