Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

SolarisDspSource.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 
00003    Copyright (c) 2000 Tyrell Corporation. All rights reserved.
00004 
00005    Tyrell DarkIce
00006 
00007    File     : SolarisDspSource.cpp
00008    Version  : $Revision: 1.2 $
00009    Author   : $Author: darkeye $
00010    Location : $Source: /cvsroot/darkice/darkice/src/SolarisDspSource.cpp,v $
00011    
00012    Copyright notice:
00013 
00014     This program is free software; you can redistribute it and/or
00015     modify it under the terms of the GNU General Public License  
00016     as published by the Free Software Foundation; either version 2
00017     of the License, or (at your option) any later version.
00018    
00019     This program is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00022     GNU General Public License for more details.
00023    
00024     You should have received a copy of the GNU General Public License
00025     along with this program; if not, write to the Free Software
00026     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ------------------------------------------------------------------------------*/
00029 
00030 /* ============================================================ include files */
00031 
00032 #include "SolarisDspSource.h"
00033 
00034 #ifdef SUPPORT_SOLARIS_DSP
00035 // only compile this code if there is support for it
00036 
00037 #ifdef HAVE_CONFIG_H
00038 #include "config.h"
00039 #endif
00040 
00041 #ifdef HAVE_UNISTD_H
00042 #include <unistd.h>
00043 #else
00044 #error need unistd.h
00045 #endif
00046 
00047 #ifdef HAVE_STRING_H
00048 #include <string.h>
00049 #else
00050 #error need string.h
00051 #endif
00052 
00053 #ifdef HAVE_SYS_TYPES_H
00054 #include <sys/types.h>
00055 #else
00056 #error need sys/types.h
00057 #endif
00058 
00059 #ifdef HAVE_SYS_STAT_H
00060 #include <sys/stat.h>
00061 #else
00062 #error need sys/stat.h
00063 #endif
00064 
00065 #ifdef HAVE_FCNTL_H
00066 #include <fcntl.h>
00067 #else
00068 #error need fcntl.h
00069 #endif
00070 
00071 #ifdef HAVE_SYS_TIME_H
00072 #include <sys/time.h>
00073 #else
00074 #error need sys/time.h
00075 #endif
00076 
00077 #ifdef HAVE_SYS_IOCTL_H
00078 #include <sys/ioctl.h>
00079 #else
00080 #error need sys/ioctl.h
00081 #endif
00082 
00083 #if defined( HAVE_SYS_AUDIO_H )
00084 #include <sys/audio.h>
00085 #elif defined( HAVE_SYS_AUDIOIO_H )
00086 #include <sys/audioio.h>
00087 #else
00088 #error need sys/audio.h or sys/audioio.h
00089 #endif
00090 
00091 
00092 #include "Util.h"
00093 #include "Exception.h"
00094 #include "SolarisDspSource.h"
00095 
00096 
00097 /* ===================================================  local data structures */
00098 
00099 
00100 /* ================================================  local constants & macros */
00101 
00102 /*------------------------------------------------------------------------------
00103  *  File identity
00104  *----------------------------------------------------------------------------*/
00105 static const char fileid[] = "$Id: SolarisDspSource.cpp,v 1.2 2004/02/18 21:08:11 darkeye Exp $";
00106 
00107 
00108 /* ===============================================  local function prototypes */
00109 
00110 
00111 /* =============================================================  module code */
00112 
00113 /*------------------------------------------------------------------------------
00114  *  Initialize the object
00115  *----------------------------------------------------------------------------*/
00116 void
00117 SolarisDspSource :: init (  const char      * name )    throw ( Exception )
00118 {
00119     fileName       = Util::strDup( name);
00120     fileDescriptor = 0;
00121 }
00122 
00123 
00124 /*------------------------------------------------------------------------------
00125  *  De-initialize the object
00126  *----------------------------------------------------------------------------*/
00127 void
00128 SolarisDspSource :: strip ( void )                      throw ( Exception )
00129 {
00130     if ( isOpen() ) {
00131         close();
00132     }
00133     
00134     delete[] fileName;
00135 }
00136 
00137 #include <errno.h>
00138 
00139 /*------------------------------------------------------------------------------
00140  *  Open the audio source
00141  *----------------------------------------------------------------------------*/
00142 bool
00143 SolarisDspSource :: open ( void )                       throw ( Exception )
00144 {
00145     audio_info_t    audioInfo;
00146 
00147     if ( isOpen() ) {
00148         return false;
00149     }
00150 
00151     if ( (fileDescriptor = ::open( fileName, O_RDONLY)) == -1 ) {
00152         fileDescriptor = 0;
00153         return false;
00154     }
00155 
00156     AUDIO_INITINFO( &audioInfo);
00157     audioInfo.record.sample_rate = getSampleRate();
00158     audioInfo.record.channels    = getChannel();
00159     audioInfo.record.precision   = getBitsPerSample();
00160     audioInfo.record.encoding    = AUDIO_ENCODING_LINEAR;
00161     // for stupid OpenBSD we need to add the following, as it masks
00162     // read/write calls when using -pthread
00163     audioInfo.record.pause       = 0;
00164 
00165     if ( ioctl( fileDescriptor, AUDIO_SETINFO, &audioInfo) == -1 ) {
00166 
00167         close();
00168         throw Exception( __FILE__, __LINE__, "ioctl error");
00169     }
00170 
00171     if ( audioInfo.record.channels != getChannel() ) {
00172         close();
00173         throw Exception( __FILE__, __LINE__,
00174                          "can't set channels", audioInfo.record.channels);
00175     }
00176 
00177     if ( audioInfo.record.precision != getBitsPerSample() ) {
00178         close();
00179         throw Exception( __FILE__, __LINE__,
00180                          "can't set bits per sample",
00181                          audioInfo.record.precision);
00182     }
00183 
00184     if ( audioInfo.record.sample_rate != getSampleRate() ) {
00185         reportEvent( 2, "sound card recording sample rate set to ",
00186                         audioInfo.record.sample_rate,
00187                         " while trying to set it to ", getSampleRate());
00188         reportEvent( 2, "this is probably not a problem, but a slight "
00189                         "drift in the sound card driver");
00190     }
00191 
00192     
00193     return true;
00194 }
00195 
00196 
00197 /*------------------------------------------------------------------------------
00198  *  Check wether read() would return anything
00199  *----------------------------------------------------------------------------*/
00200 bool
00201 SolarisDspSource :: canRead ( unsigned int    sec,
00202                               unsigned int    usec )    throw ( Exception )
00203 {
00204     fd_set              fdset;
00205     struct timeval      tv;
00206     int                 ret;
00207 
00208     if ( !isOpen() ) {
00209         return false;
00210     }
00211 
00212     FD_ZERO( &fdset);
00213     FD_SET( fileDescriptor, &fdset);
00214     tv.tv_sec  = sec;
00215     tv.tv_usec = usec;
00216 
00217     ret = select( fileDescriptor + 1, &fdset, NULL, NULL, &tv);
00218     
00219     if ( ret == -1 ) {
00220         throw Exception( __FILE__, __LINE__, "select error");
00221     }
00222 
00223     return ret > 0;
00224 }
00225 
00226 
00227 /*------------------------------------------------------------------------------
00228  *  Read from the audio source
00229  *----------------------------------------------------------------------------*/
00230 unsigned int
00231 SolarisDspSource :: read (    void          * buf,
00232                               unsigned int    len )     throw ( Exception )
00233 {
00234     ssize_t     ret;
00235 
00236     if ( !isOpen() ) {
00237         return 0;
00238     }
00239 
00240     ret = ::read( fileDescriptor, buf, len);
00241 
00242     if ( ret == -1 ) {
00243         throw Exception( __FILE__, __LINE__, "read error");
00244     }
00245 
00246     return ret;
00247 }
00248 
00249 
00250 /*------------------------------------------------------------------------------
00251  *  Close the audio source
00252  *----------------------------------------------------------------------------*/
00253 void
00254 SolarisDspSource :: close ( void )                  throw ( Exception )
00255 {
00256     if ( !isOpen() ) {
00257         return;
00258     }
00259 
00260     ::close( fileDescriptor);
00261     fileDescriptor = 0;
00262 }
00263 
00264 #endif // SUPPORT_SOLARIS_DSP
00265 
00266 
00267 /*------------------------------------------------------------------------------
00268  
00269   $Source: /cvsroot/darkice/darkice/src/SolarisDspSource.cpp,v $
00270 
00271   $Log: SolarisDspSource.cpp,v $
00272   Revision 1.2  2004/02/18 21:08:11  darkeye
00273   ported to OpenBSD (real-time scheduling not yet supported)
00274 
00275   Revision 1.1  2001/09/11 15:05:21  darkeye
00276   added Solaris support
00277 
00278 
00279   
00280 ------------------------------------------------------------------------------*/
00281 

Generated on Thu Apr 14 13:59:13 2005 for DarkIce by  doxygen 1.4.1