00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "WAVAudioFileSource.hh"
00022 #include "InputFile.hh"
00023 #include "GroupsockHelper.hh"
00024
00026
00027 WAVAudioFileSource*
00028 WAVAudioFileSource::createNew(UsageEnvironment& env, char const* fileName) {
00029 do {
00030 FILE* fid = OpenInputFile(env, fileName);
00031 if (fid == NULL) break;
00032
00033 WAVAudioFileSource* newSource = new WAVAudioFileSource(env, fid);
00034 if (newSource != NULL && newSource->bitsPerSample() == 0) {
00035
00036 Medium::close(newSource);
00037 break;
00038 }
00039
00040 newSource->fFileSize = (unsigned)GetFileSize(fileName, fid);
00041
00042 return newSource;
00043 } while (0);
00044
00045 return NULL;
00046 }
00047
00048 unsigned WAVAudioFileSource::numPCMBytes() const {
00049 if (fFileSize < fWAVHeaderSize) return 0;
00050 return fFileSize - fWAVHeaderSize;
00051 }
00052
00053 void WAVAudioFileSource::setScaleFactor(int scale) {
00054 if (!fFidIsSeekable) return;
00055
00056 fScaleFactor = scale;
00057
00058 if (fScaleFactor < 0 && TellFile64(fFid) > 0) {
00059
00060
00061
00062 int bytesPerSample = (fNumChannels*fBitsPerSample)/8;
00063 if (bytesPerSample == 0) bytesPerSample = 1;
00064 SeekFile64(fFid, -bytesPerSample, SEEK_CUR);
00065 }
00066 }
00067
00068 void WAVAudioFileSource::seekToPCMByte(unsigned byteNumber, unsigned numBytesToStream) {
00069 byteNumber += fWAVHeaderSize;
00070 if (byteNumber > fFileSize) byteNumber = fFileSize;
00071
00072 SeekFile64(fFid, byteNumber, SEEK_SET);
00073
00074 fNumBytesToStream = numBytesToStream;
00075 fLimitNumBytesToStream = fNumBytesToStream > 0;
00076 }
00077
00078 unsigned char WAVAudioFileSource::getAudioFormat() {
00079 return fAudioFormat;
00080 }
00081
00082
00083 #define nextc fgetc(fid)
00084
00085 static Boolean get4Bytes(FILE* fid, unsigned& result) {
00086 int c0, c1, c2, c3;
00087 if ((c0 = nextc) == EOF || (c1 = nextc) == EOF ||
00088 (c2 = nextc) == EOF || (c3 = nextc) == EOF) return False;
00089 result = (c3<<24)|(c2<<16)|(c1<<8)|c0;
00090 return True;
00091 }
00092
00093 static Boolean get2Bytes(FILE* fid, unsigned short& result) {
00094 int c0, c1;
00095 if ((c0 = nextc) == EOF || (c1 = nextc) == EOF) return False;
00096 result = (c1<<8)|c0;
00097 return True;
00098 }
00099
00100 static Boolean skipBytes(FILE* fid, int num) {
00101 while (num-- > 0) {
00102 if (nextc == EOF) return False;
00103 }
00104 return True;
00105 }
00106
00107 WAVAudioFileSource::WAVAudioFileSource(UsageEnvironment& env, FILE* fid)
00108 : AudioInputDevice(env, 0, 0, 0, 0),
00109 fFid(fid), fFidIsSeekable(False), fLastPlayTime(0), fHaveStartedReading(False), fWAVHeaderSize(0), fFileSize(0),
00110 fScaleFactor(1), fLimitNumBytesToStream(False), fNumBytesToStream(0), fAudioFormat(WA_UNKNOWN) {
00111
00112
00113
00114
00115
00116
00117 Boolean success = False;
00118 do {
00119
00120 if (nextc != 'R' || nextc != 'I' || nextc != 'F' || nextc != 'F') break;
00121 if (!skipBytes(fid, 4)) break;
00122 if (nextc != 'W' || nextc != 'A' || nextc != 'V' || nextc != 'E') break;
00123
00124
00125 if (nextc != 'f' || nextc != 'm' || nextc != 't' || nextc != ' ') break;
00126 unsigned formatLength;
00127 if (!get4Bytes(fid, formatLength)) break;
00128 unsigned short audioFormat;
00129 if (!get2Bytes(fid, audioFormat)) break;
00130
00131 fAudioFormat = (unsigned char)audioFormat;
00132 if (fAudioFormat != WA_PCM && fAudioFormat != WA_PCMA && fAudioFormat != WA_PCMU && fAudioFormat != WA_IMA_ADPCM) {
00133
00134 env.setResultMsg("Audio format is not one that we handle (PCM/PCMU/PCMA or IMA ADPCM)");
00135 break;
00136 }
00137 unsigned short numChannels;
00138 if (!get2Bytes(fid, numChannels)) break;
00139 fNumChannels = (unsigned char)numChannels;
00140 if (fNumChannels < 1 || fNumChannels > 2) {
00141 char errMsg[100];
00142 sprintf(errMsg, "Bad # channels: %d", fNumChannels);
00143 env.setResultMsg(errMsg);
00144 break;
00145 }
00146 if (!get4Bytes(fid, fSamplingFrequency)) break;
00147 if (fSamplingFrequency == 0) {
00148 env.setResultMsg("Bad sampling frequency: 0");
00149 break;
00150 }
00151 if (!skipBytes(fid, 6)) break;
00152 unsigned short bitsPerSample;
00153 if (!get2Bytes(fid, bitsPerSample)) break;
00154 fBitsPerSample = (unsigned char)bitsPerSample;
00155 if (fBitsPerSample == 0) {
00156 env.setResultMsg("Bad bits-per-sample: 0");
00157 break;
00158 }
00159 if (!skipBytes(fid, formatLength - 16)) break;
00160
00161
00162 int c = nextc;
00163 if (c == 'f') {
00164 if (nextc != 'a' || nextc != 'c' || nextc != 't') break;
00165 unsigned factLength;
00166 if (!get4Bytes(fid, factLength)) break;
00167 if (!skipBytes(fid, factLength)) break;
00168 c = nextc;
00169 }
00170
00171
00172 if (c != 'd' || nextc != 'a' || nextc != 't' || nextc != 'a') break;
00173 if (!skipBytes(fid, 4)) break;
00174
00175
00176 fWAVHeaderSize = (unsigned)TellFile64(fid);
00177 success = True;
00178 } while (0);
00179
00180 if (!success) {
00181 env.setResultMsg("Bad WAV file format");
00182
00183 fBitsPerSample = 0;
00184 return;
00185 }
00186
00187 fPlayTimePerSample = 1e6/(double)fSamplingFrequency;
00188
00189
00190
00191
00192
00193 unsigned maxSamplesPerFrame = (1400*8)/(fNumChannels*fBitsPerSample);
00194 unsigned desiredSamplesPerFrame = (unsigned)(0.02*fSamplingFrequency);
00195 unsigned samplesPerFrame = desiredSamplesPerFrame < maxSamplesPerFrame ? desiredSamplesPerFrame : maxSamplesPerFrame;
00196 fPreferredFrameSize = (samplesPerFrame*fNumChannels*fBitsPerSample)/8;
00197
00198 fFidIsSeekable = FileIsSeekable(fFid);
00199 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00200
00201 makeSocketNonBlocking(fileno(fFid));
00202 #endif
00203 }
00204
00205 WAVAudioFileSource::~WAVAudioFileSource() {
00206 if (fFid == NULL) return;
00207
00208 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00209 envir().taskScheduler().turnOffBackgroundReadHandling(fileno(fFid));
00210 #endif
00211
00212 CloseInputFile(fFid);
00213 }
00214
00215 void WAVAudioFileSource::doGetNextFrame() {
00216 if (feof(fFid) || ferror(fFid) || (fLimitNumBytesToStream && fNumBytesToStream == 0)) {
00217 handleClosure(this);
00218 return;
00219 }
00220
00221 fFrameSize = 0;
00222 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00223 doReadFromFile();
00224 #else
00225 if (!fHaveStartedReading) {
00226
00227 envir().taskScheduler().turnOnBackgroundReadHandling(fileno(fFid),
00228 (TaskScheduler::BackgroundHandlerProc*)&fileReadableHandler, this);
00229 fHaveStartedReading = True;
00230 }
00231 #endif
00232 }
00233
00234 void WAVAudioFileSource::doStopGettingFrames() {
00235 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00236 envir().taskScheduler().turnOffBackgroundReadHandling(fileno(fFid));
00237 fHaveStartedReading = False;
00238 #endif
00239 }
00240
00241 void WAVAudioFileSource::fileReadableHandler(WAVAudioFileSource* source, int ) {
00242 if (!source->isCurrentlyAwaitingData()) {
00243 source->doStopGettingFrames();
00244 return;
00245 }
00246 source->doReadFromFile();
00247 }
00248
00249 static Boolean const readFromFilesSynchronously
00250 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00251 = True;
00252 #else
00253 = False;
00254 #endif
00255
00256 void WAVAudioFileSource::doReadFromFile() {
00257
00258 if (fLimitNumBytesToStream && fNumBytesToStream < fMaxSize) {
00259 fMaxSize = fNumBytesToStream;
00260 }
00261 if (fPreferredFrameSize < fMaxSize) {
00262 fMaxSize = fPreferredFrameSize;
00263 }
00264 unsigned bytesPerSample = (fNumChannels*fBitsPerSample)/8;
00265 if (bytesPerSample == 0) bytesPerSample = 1;
00266
00267
00268 unsigned bytesToRead = fScaleFactor == 1 ? fMaxSize - fMaxSize%bytesPerSample : bytesPerSample;
00269 unsigned numBytesRead;
00270 while (1) {
00271 if (readFromFilesSynchronously || fFidIsSeekable) {
00272 numBytesRead = fread(fTo, 1, bytesToRead, fFid);
00273 } else {
00274
00275 numBytesRead = read(fileno(fFid), fTo, bytesToRead);
00276 }
00277 if (numBytesRead == 0) {
00278 handleClosure(this);
00279 return;
00280 }
00281 fFrameSize += numBytesRead;
00282 fTo += numBytesRead;
00283 fMaxSize -= numBytesRead;
00284 fNumBytesToStream -= numBytesRead;
00285
00286
00287 if (!readFromFilesSynchronously && fFrameSize%bytesPerSample > 0) return;
00288
00289
00290
00291 if (fScaleFactor != 1) {
00292 SeekFile64(fFid, (fScaleFactor-1)*bytesPerSample, SEEK_CUR);
00293 if (fMaxSize < bytesPerSample) break;
00294 } else {
00295 break;
00296 }
00297 }
00298
00299
00300 if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
00301
00302 gettimeofday(&fPresentationTime, NULL);
00303 } else {
00304
00305 unsigned uSeconds = fPresentationTime.tv_usec + fLastPlayTime;
00306 fPresentationTime.tv_sec += uSeconds/1000000;
00307 fPresentationTime.tv_usec = uSeconds%1000000;
00308 }
00309
00310
00311 fDurationInMicroseconds = fLastPlayTime
00312 = (unsigned)((fPlayTimePerSample*fFrameSize)/bytesPerSample);
00313
00314
00315 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00316
00317 nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
00318 (TaskFunc*)FramedSource::afterGetting, this);
00319 #else
00320
00321
00322 FramedSource::afterGetting(this);
00323 #endif
00324 }
00325
00326 Boolean WAVAudioFileSource::setInputPort(int ) {
00327 return True;
00328 }
00329
00330 double WAVAudioFileSource::getAverageLevel() const {
00331 return 0.0;
00332 }