00001
00002
00003
00004
00005 #include "audiodrv.h"
00006
00007 audioDriver::audioDriver()
00008 {
00009
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
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
00034
00035 frequency = inFreq;
00036 precision = inPrecision;
00037 channels = inChannels;
00038 fragments = inFragments;
00039 fragSizeBase = inFragBase;
00040
00041
00042 int dsp_sampleSize;
00043 if (precision == SIDEMU_16BIT)
00044 dsp_sampleSize = 16;
00045 else
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
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
00070 int dsp_stereo;
00071 if (channels == SIDEMU_STEREO)
00072 dsp_stereo = 1;
00073 else
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
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
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
00099 frequency = dsp_speed;
00100
00101
00102
00103
00104
00105
00106
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
00115
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 }