00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "SolarisDspSource.h"
00033
00034 #ifdef SUPPORT_SOLARIS_DSP
00035
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
00098
00099
00100
00101
00102
00103
00104
00105 static const char fileid[] = "$Id: SolarisDspSource.cpp,v 1.2 2004/02/18 21:08:11 darkeye Exp $";
00106
00107
00108
00109
00110
00111
00112
00113
00114
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
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
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
00162
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
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
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
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
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281