1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**********/
// "liveMedia"
// Copyright (c) 1996-2005 Live Networks, Inc. All rights reserved.
// A WAV audio file source
// Implementation
#include "WAVAudioFileSource.hh"
#include "InputFile.hh"
#include "GroupsockHelper.hh"
////////// WAVAudioFileSource //////////
WAVAudioFileSource*
WAVAudioFileSource::createNew(UsageEnvironment& env, char const* fileName) {
do {
FILE* fid = OpenInputFile(env, fileName);
if (fid == NULL) break;
WAVAudioFileSource* newSource = new WAVAudioFileSource(env, fid);
if (newSource != NULL && newSource->bitsPerSample() == 0) {
// The WAV file header was apparently invalid.
Medium::close(newSource);
break;
}
newSource->fFileSize = (unsigned)GetFileSize(fileName, fid);
return newSource;
} while (0);
return NULL;
}
unsigned WAVAudioFileSource::numPCMBytes() const {
if (fFileSize < fWAVHeaderSize) return 0;
return fFileSize - fWAVHeaderSize;
}
void WAVAudioFileSource::setScaleFactor(int scale) {
fScaleFactor = scale;
if (fScaleFactor < 0 && ftell(fFid) > 0) {
// Because we're reading backwards, seek back one sample, to ensure that
// (i) we start reading the last sample before the start point, and
// (ii) we don't hit end-of-file on the first read.
int const bytesPerSample = (fNumChannels*fBitsPerSample)/8;
fseek(fFid, -bytesPerSample, SEEK_CUR);
}
}
void WAVAudioFileSource::seekToPCMByte(unsigned byteNumber) {
byteNumber += fWAVHeaderSize;
if (byteNumber > fFileSize) byteNumber = fFileSize;
fseek(fFid, byteNumber, SEEK_SET);
}
#define nextc fgetc(fid)
#define ucEOF ((unsigned char)EOF)
static Boolean get4Bytes(FILE* fid, unsigned& result) { // little-endian
unsigned char c0, c1, c2, c3;
if ((c0 = nextc) == ucEOF || (c1 = nextc) == ucEOF ||
(c2 = nextc) == ucEOF || (c3 = nextc) == ucEOF) return False;
result = (c3<<24)|(c2<<16)|(c1<<8)|c0;
return True;
}
static Boolean get2Bytes(FILE* fid, unsigned short& result) {//little-endian
unsigned char c0, c1;
if ((c0 = nextc) == ucEOF || (c1 = nextc) == ucEOF) return False;
result = (c1<<8)|c0;
return True;
}
static Boolean skipBytes(FILE* fid, int num) {
while (num-- > 0) {
if (nextc == ucEOF) return False;
}
return True;
}
WAVAudioFileSource::WAVAudioFileSource(UsageEnvironment& env, FILE* fid)
: AudioInputDevice(env, 0, 0, 0, 0)/* set the real parameters later */,
fFid(fid), fLastPlayTime(0), fWAVHeaderSize(0), fFileSize(0), fScaleFactor(1) {
// Check the WAV file header for validity.
// Note: The following web pages contain info about the WAV format:
// http://www.technology.niagarac.on.ca/courses/comp630/WavFileFormat.html
// http://ccrma-www.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
// http://www.ringthis.com/dev/wave_format.htm
// http://www.lightlink.com/tjweber/StripWav/Canon.html
// http://www.borg.com/~jglatt/tech/wave.htm
// http://www.wotsit.org/download.asp?f=wavecomp
Boolean success = False; // until we learn otherwise
do {
// RIFF Chunk:
if (nextc != 'R' || nextc != 'I' || nextc != 'F' || nextc != 'F') break;
if (!skipBytes(fid, 4)) break;
if (nextc != 'W' || nextc != 'A' || nextc != 'V' || nextc != 'E') break;
// FORMAT Chunk:
if (nextc != 'f' || nextc != 'm' || nextc != 't' || nextc != ' ') break;
unsigned formatLength;
if (!get4Bytes(fid, formatLength)) break;
unsigned short audioFormat;
if (!get2Bytes(fid, audioFormat)) break;
if (audioFormat != 1) { // not PCM - we can't handle this
env.setResultMsg("Audio format is not PCM");
break;
}
unsigned short numChannels;
if (!get2Bytes(fid, numChannels)) break;
fNumChannels = (unsigned char)numChannels;
if (fNumChannels < 1 || fNumChannels > 2) { // invalid # channels
char errMsg[100];
sprintf(errMsg, "Bad # channels: %d", fNumChannels);
env.setResultMsg(errMsg);
break;
}
if (!get4Bytes(fid, fSamplingFrequency)) break;
if (fSamplingFrequency == 0) {
env.setResultMsg("Bad sampling frequency: 0");
break;
}
if (!skipBytes(fid, 6)) break;
unsigned short bitsPerSample;
if (!get2Bytes(fid, bitsPerSample)) break;
fBitsPerSample = (unsigned char)bitsPerSample;
if (fBitsPerSample == 0) {
env.setResultMsg("Bad bits-per-sample: 0");
break;
}
if (!skipBytes(fid, formatLength - 16)) break;
// FACT chunk (optional):
unsigned char c = nextc;
if (c == 'f') {
if (nextc != 'a' || nextc != 'c' || nextc != 't') break;
unsigned factLength;
if (!get4Bytes(fid, factLength)) break;
if (!skipBytes(fid, factLength)) break;
c = nextc;
}
// DATA Chunk:
if (c != 'd' || nextc != 'a' || nextc != 't' || nextc != 'a') break;
if (!skipBytes(fid, 4)) break;
// The header is good; the remaining data are the sample bytes.
fWAVHeaderSize = ftell(fid);
success = True;
} while (0);
if (!success) {
env.setResultMsg("Bad WAV file format");
// Set "fBitsPerSample" to zero, to indicate failure:
fBitsPerSample = 0;
return;
}
fPlayTimePerSample = 1e6/(double)fSamplingFrequency;
// Although PCM is a sample-based format, we group samples into
// 'frames' for efficient delivery to clients. Set up our preferred
// frame size to be close to 20 ms, if possible, but always no greater
// than 1400 bytes (to ensure that it will fit in a single RTP packet)
unsigned maxSamplesPerFrame = (1400*8)/(fNumChannels*fBitsPerSample);
unsigned desiredSamplesPerFrame = (unsigned)(0.02*fSamplingFrequency);
unsigned samplesPerFrame = desiredSamplesPerFrame < maxSamplesPerFrame
? desiredSamplesPerFrame : maxSamplesPerFrame;
fPreferredFrameSize = (samplesPerFrame*fNumChannels*fBitsPerSample)/8;
}
WAVAudioFileSource::~WAVAudioFileSource() {
CloseInputFile(fFid);
}
void WAVAudioFileSource::doGetNextFrame() {
if (feof(fFid) || ferror(fFid)) {
handleClosure(this);
return;
}
// Try to read as many bytes as will fit in the buffer provided
// (or "fPreferredFrameSize" if less)
if (fPreferredFrameSize < fMaxSize) {
fMaxSize = fPreferredFrameSize;
}
unsigned const bytesPerSample = (fNumChannels*fBitsPerSample)/8;
unsigned bytesToRead = fMaxSize - fMaxSize%bytesPerSample;
if (fScaleFactor == 1) {
// Common case - read samples in bulk:
fFrameSize = fread(fTo, 1, bytesToRead, fFid);
} else {
// We read every 'fScaleFactor'th sample:
fFrameSize = 0;
while (bytesToRead > 0) {
size_t bytesRead = fread(fTo, 1, bytesPerSample, fFid);
if (bytesRead <= 0) break;
fTo += bytesRead;
fFrameSize += bytesRead;
bytesToRead -= bytesRead;
// Seek to the appropriate place for the next sample:
fseek(fFid, (fScaleFactor-1)*bytesPerSample, SEEK_CUR);
}
}
// Set the 'presentation time' and 'duration' of this frame:
if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
// This is the first frame, so use the current time:
gettimeofday(&fPresentationTime, NULL);
} else {
// Increment by the play time of the previous data:
unsigned uSeconds = fPresentationTime.tv_usec + fLastPlayTime;
fPresentationTime.tv_sec += uSeconds/1000000;
fPresentationTime.tv_usec = uSeconds%1000000;
}
// Remember the play time of this data:
fDurationInMicroseconds = fLastPlayTime
= (unsigned)((fPlayTimePerSample*fFrameSize)/bytesPerSample);
// Switch to another task, and inform the reader that he has data:
#if defined(__WIN32__) || defined(_WIN32)
// HACK: One of our applications that uses this source uses an
// implementation of scheduleDelayedTask() that performs very badly
// (chewing up lots of CPU time, apparently polling) on Windows.
// Until this is fixed, we just call our "afterGetting()" function
// directly. This avoids infinite recursion, as long as our sink
// is discontinuous, which is the case for the RTP sink that
// this application uses. #####
afterGetting(this);
#else
nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
(TaskFunc*)FramedSource::afterGetting, this);
#endif
}
Boolean WAVAudioFileSource::setInputPort(int /*portIndex*/) {
return True;
}
double WAVAudioFileSource::getAverageLevel() const {
return 0.0;//##### fix this later
}
|