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, u_int32_t& 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, u_int16_t& 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 u_int32_t tmp;
00126 if (!get4Bytes(fid, tmp)) break;
00127 if (tmp != 0x20746d66) {
00128
00129 if (!get4Bytes(fid, tmp)) break;
00130 if (!skipBytes(fid, tmp)) break;
00131 }
00132
00133
00134 unsigned formatLength;
00135 if (!get4Bytes(fid, formatLength)) break;
00136 unsigned short audioFormat;
00137 if (!get2Bytes(fid, audioFormat)) break;
00138
00139 fAudioFormat = (unsigned char)audioFormat;
00140 if (fAudioFormat != WA_PCM && fAudioFormat != WA_PCMA && fAudioFormat != WA_PCMU && fAudioFormat != WA_IMA_ADPCM) {
00141
00142 env.setResultMsg("Audio format is not one that we handle (PCM/PCMU/PCMA or IMA ADPCM)");
00143 break;
00144 }
00145 unsigned short numChannels;
00146 if (!get2Bytes(fid, numChannels)) break;
00147 fNumChannels = (unsigned char)numChannels;
00148 if (fNumChannels < 1 || fNumChannels > 2) {
00149 char errMsg[100];
00150 sprintf(errMsg, "Bad # channels: %d", fNumChannels);
00151 env.setResultMsg(errMsg);
00152 break;
00153 }
00154 if (!get4Bytes(fid, fSamplingFrequency)) break;
00155 if (fSamplingFrequency == 0) {
00156 env.setResultMsg("Bad sampling frequency: 0");
00157 break;
00158 }
00159 if (!skipBytes(fid, 6)) break;
00160 unsigned short bitsPerSample;
00161 if (!get2Bytes(fid, bitsPerSample)) break;
00162 fBitsPerSample = (unsigned char)bitsPerSample;
00163 if (fBitsPerSample == 0) {
00164 env.setResultMsg("Bad bits-per-sample: 0");
00165 break;
00166 }
00167 if (!skipBytes(fid, formatLength - 16)) break;
00168
00169
00170 int c = nextc;
00171 if (c == 'f') {
00172 if (nextc != 'a' || nextc != 'c' || nextc != 't') break;
00173 unsigned factLength;
00174 if (!get4Bytes(fid, factLength)) break;
00175 if (!skipBytes(fid, factLength)) break;
00176 c = nextc;
00177 }
00178
00179
00180 if (c != 'd' || nextc != 'a' || nextc != 't' || nextc != 'a') break;
00181 if (!skipBytes(fid, 4)) break;
00182
00183
00184 fWAVHeaderSize = (unsigned)TellFile64(fid);
00185 success = True;
00186 } while (0);
00187
00188 if (!success) {
00189 env.setResultMsg("Bad WAV file format");
00190
00191 fBitsPerSample = 0;
00192 return;
00193 }
00194
00195 fPlayTimePerSample = 1e6/(double)fSamplingFrequency;
00196
00197
00198
00199
00200
00201 unsigned maxSamplesPerFrame = (1400*8)/(fNumChannels*fBitsPerSample);
00202 unsigned desiredSamplesPerFrame = (unsigned)(0.02*fSamplingFrequency);
00203 unsigned samplesPerFrame = desiredSamplesPerFrame < maxSamplesPerFrame ? desiredSamplesPerFrame : maxSamplesPerFrame;
00204 fPreferredFrameSize = (samplesPerFrame*fNumChannels*fBitsPerSample)/8;
00205
00206 fFidIsSeekable = FileIsSeekable(fFid);
00207 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00208
00209 makeSocketNonBlocking(fileno(fFid));
00210 #endif
00211 }
00212
00213 WAVAudioFileSource::~WAVAudioFileSource() {
00214 if (fFid == NULL) return;
00215
00216 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00217 envir().taskScheduler().turnOffBackgroundReadHandling(fileno(fFid));
00218 #endif
00219
00220 CloseInputFile(fFid);
00221 }
00222
00223 void WAVAudioFileSource::doGetNextFrame() {
00224 if (feof(fFid) || ferror(fFid) || (fLimitNumBytesToStream && fNumBytesToStream == 0)) {
00225 handleClosure(this);
00226 return;
00227 }
00228
00229 fFrameSize = 0;
00230 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00231 doReadFromFile();
00232 #else
00233 if (!fHaveStartedReading) {
00234
00235 envir().taskScheduler().turnOnBackgroundReadHandling(fileno(fFid),
00236 (TaskScheduler::BackgroundHandlerProc*)&fileReadableHandler, this);
00237 fHaveStartedReading = True;
00238 }
00239 #endif
00240 }
00241
00242 void WAVAudioFileSource::doStopGettingFrames() {
00243 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00244 envir().taskScheduler().turnOffBackgroundReadHandling(fileno(fFid));
00245 fHaveStartedReading = False;
00246 #endif
00247 }
00248
00249 void WAVAudioFileSource::fileReadableHandler(WAVAudioFileSource* source, int ) {
00250 if (!source->isCurrentlyAwaitingData()) {
00251 source->doStopGettingFrames();
00252 return;
00253 }
00254 source->doReadFromFile();
00255 }
00256
00257 void WAVAudioFileSource::doReadFromFile() {
00258
00259 if (fLimitNumBytesToStream && fNumBytesToStream < fMaxSize) {
00260 fMaxSize = fNumBytesToStream;
00261 }
00262 if (fPreferredFrameSize < fMaxSize) {
00263 fMaxSize = fPreferredFrameSize;
00264 }
00265 unsigned bytesPerSample = (fNumChannels*fBitsPerSample)/8;
00266 if (bytesPerSample == 0) bytesPerSample = 1;
00267
00268
00269 unsigned bytesToRead = fScaleFactor == 1 ? fMaxSize - fMaxSize%bytesPerSample : bytesPerSample;
00270 unsigned numBytesRead;
00271 while (1) {
00272 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00273 numBytesRead = fread(fTo, 1, bytesToRead, fFid);
00274 #else
00275 if (fFidIsSeekable) {
00276 numBytesRead = fread(fTo, 1, bytesToRead, fFid);
00277 } else {
00278
00279 numBytesRead = read(fileno(fFid), fTo, bytesToRead);
00280 }
00281 #endif
00282 if (numBytesRead == 0) {
00283 handleClosure(this);
00284 return;
00285 }
00286 fFrameSize += numBytesRead;
00287 fTo += numBytesRead;
00288 fMaxSize -= numBytesRead;
00289 fNumBytesToStream -= numBytesRead;
00290
00291
00292 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00293 if (fFrameSize%bytesPerSample > 0) return;
00294 #endif
00295
00296
00297
00298 if (fScaleFactor != 1) {
00299 SeekFile64(fFid, (fScaleFactor-1)*bytesPerSample, SEEK_CUR);
00300 if (fMaxSize < bytesPerSample) break;
00301 } else {
00302 break;
00303 }
00304 }
00305
00306
00307 if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
00308
00309 gettimeofday(&fPresentationTime, NULL);
00310 } else {
00311
00312 unsigned uSeconds = fPresentationTime.tv_usec + fLastPlayTime;
00313 fPresentationTime.tv_sec += uSeconds/1000000;
00314 fPresentationTime.tv_usec = uSeconds%1000000;
00315 }
00316
00317
00318 fDurationInMicroseconds = fLastPlayTime
00319 = (unsigned)((fPlayTimePerSample*fFrameSize)/bytesPerSample);
00320
00321
00322 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00323
00324 nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
00325 (TaskFunc*)FramedSource::afterGetting, this);
00326 #else
00327
00328
00329 FramedSource::afterGetting(this);
00330 #endif
00331 }
00332
00333 Boolean WAVAudioFileSource::setInputPort(int ) {
00334 return True;
00335 }
00336
00337 double WAVAudioFileSource::getAverageLevel() const {
00338 return 0.0;
00339 }