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

sparc_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 inFragSizeBase)
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         channels = inChannels;
00037         precision = inPrecision;
00038 
00039 #ifdef CHECKFORSPEAKERBOX  
00040         int hwdevice;
00041         if ( ioctl(audioHd,AUDIO_GETDEV,&hwdevice) == -1)
00042     {
00043                 errorString = "AUDIO: No audio hardware device installed.";
00044                 return false;
00045     }
00046         if ( hwdevice != AUDIO_DEV_SPEAKERBOX )
00047     {
00048                 audioHd = -1;
00049                 errorString = "AUDIO: Speakerbox not installed/enabled.";
00050                 return false;
00051     }
00052 #endif
00053         
00054         // Choose the nearest possible frequency.
00055         int dbrifreqs[] = 
00056         {
00057                 8000, 9600, 11025, 16000, 18900, 22050, 32000, 37800, 44100, 48000, 0
00058         };
00059         int dbrifsel = 0;
00060         int dbrifreqdiff = 100000;
00061         int dbrifrequency = frequency;
00062         do
00063     {  
00064                 int dbrifreqdiff2 = frequency - dbrifreqs[dbrifsel];
00065                 dbrifreqdiff2 < 0 ? dbrifreqdiff2 = 0 - dbrifreqdiff2 : dbrifreqdiff2 += 0;
00066                 if ( dbrifreqdiff2 < dbrifreqdiff )  
00067                 {
00068                         dbrifreqdiff = dbrifreqdiff2;
00069                         dbrifrequency = dbrifreqs[dbrifsel];
00070                 }
00071                 dbrifsel++;
00072     }  while ( dbrifreqs[dbrifsel] != 0 );
00073         frequency = dbrifrequency;
00074         
00075         precision = SIDEMU_16BIT;  // Only poor audio capabilities for 8-bit.
00076         // Sparcstations 5 and 10 tend to be 16-bit only at rates above 8000 Hz.
00077         encoding = SIDEMU_SIGNED_PCM;
00078         blockSize = frequency;  // dumb mode
00079         audio_info myaudio_info;
00080         if ( ioctl(audioHd,AUDIO_GETINFO,&myaudio_info) == (-1))
00081     {
00082                 errorString = "AUDIO: Could not get audio info.";
00083                 return false;
00084     }
00085         AUDIO_INITINFO( &myaudio_info );
00086         myaudio_info.play.sample_rate = frequency;
00087         //myaudio_info.play.buffer_size = blockSize;  // does not work ?
00088         if (channels == SIDEMU_STEREO)
00089                 myaudio_info.play.channels = 2;
00090         else  // if (channels == SIDEMU_MONO)
00091                 myaudio_info.play.channels = 1;
00092         myaudio_info.play.precision = precision;
00093         myaudio_info.play.encoding = AUDIO_ENCODING_LINEAR;
00094         myaudio_info.output_muted = 0;
00095         if ( ioctl(audioHd,AUDIO_SETINFO,&myaudio_info) == (-1))
00096     {
00097                 errorString = "AUDIO: Could not set audio info.";
00098                 return false;
00099     }
00100 
00101         return true;
00102 }
00103 
00104 // Close an opened audio device, free any allocated buffers and
00105 // reset any variables that reflect the current state.
00106 void audioDriver::Close()
00107 {
00108         if (audioHd != (-1))
00109     {
00110                 close(audioHd);
00111                 audioHd = (-1);
00112     }
00113 }
00114 
00115 void audioDriver::Play(ubyte* pBuffer, int bufferSize)
00116 {
00117         if (audioHd != (-1))
00118         {
00119                 write(audioHd,pBuffer,bufferSize);
00120         }
00121 }

Generated on Tue Feb 8 04:14:16 2005 for Esidplay by doxygen 1.3.3