Fixed 8bit file processing in integer version

This commit is contained in:
oparviai 2012-09-01 07:57:22 +00:00
parent 6a57544b2a
commit 85b12af596
2 changed files with 79 additions and 70 deletions

View File

@ -164,7 +164,7 @@ void *WavFileBase::getConvBuffer(int sizeBytes)
{ {
delete[] convBuff; delete[] convBuff;
convBuffSize = (sizeBytes + 15) & -8; convBuffSize = (sizeBytes + 15) & -8; // round up to following 8-byte bounday
convBuff = new char[convBuffSize]; convBuff = new char[convBuffSize];
} }
return convBuff; return convBuff;
@ -268,7 +268,7 @@ int WavInFile::checkCharTags() const
} }
int WavInFile::read(char *buffer, int maxElems) int WavInFile::read(unsigned char *buffer, int maxElems)
{ {
int numBytes; int numBytes;
uint afterDataRead; uint afterDataRead;
@ -304,30 +304,26 @@ int WavInFile::read(short *buffer, int maxElems)
int numElems; int numElems;
assert(buffer); assert(buffer);
if (header.format.bits_per_sample == 8) switch (header.format.bits_per_sample)
{
case 8:
{ {
// 8 bit format // 8 bit format
char *temp = (char*)getConvBuffer(maxElems); unsigned char *temp = (unsigned char*)getConvBuffer(maxElems);
int i; int i;
numElems = read(temp, maxElems); numElems = read(temp, maxElems);
// convert from 8 to 16 bit // convert from 8 to 16 bit
for (i = 0; i < numElems; i ++) for (i = 0; i < numElems; i ++)
{ {
buffer[i] = temp[i] << 8; buffer[i] = (short)(((short)temp[i] - 128) * 256);
} }
break;
} }
else
case 16:
{ {
// 16 bit format // 16 bit format
if (header.format.bits_per_sample != 16)
{
stringstream ss;
ss << "\nOnly 8/16 bit sample WAV files supported. Can't open WAV file with ";
ss << (int)header.format.bits_per_sample;
ss << " bit sample format. ";
ST_THROW_RT_ERROR(ss.str().c_str());
}
assert(sizeof(short) == 2); assert(sizeof(short) == 2);
@ -346,8 +342,19 @@ int WavInFile::read(short *buffer, int maxElems)
// 16bit samples, swap byte order if necessary // 16bit samples, swap byte order if necessary
_swap16Buffer((short *)buffer, numElems); _swap16Buffer((short *)buffer, numElems);
break;
} }
default:
{
stringstream ss;
ss << "\nOnly 8/16 bit sample WAV files supported. Can't open WAV file with ";
ss << (int)header.format.bits_per_sample;
ss << " bit sample format. ";
ST_THROW_RT_ERROR(ss.str().c_str());
}
};
return numElems; return numElems;
} }
@ -718,7 +725,6 @@ void WavOutFile::fillInHeader(uint sampleRate, uint bits, uint channels)
// copy string 'WAVE' to wave // copy string 'WAVE' to wave
memcpy(&(header.riff.wave), waveStr, 4); memcpy(&(header.riff.wave), waveStr, 4);
// fill in the 'format' part.. // fill in the 'format' part..
// copy string 'fmt ' to fmt // copy string 'fmt ' to fmt
@ -784,7 +790,7 @@ void WavOutFile::writeHeader()
void WavOutFile::write(const char *buffer, int numElems) void WavOutFile::write(const unsigned char *buffer, int numElems)
{ {
int res; int res;
@ -812,46 +818,49 @@ void WavOutFile::write(const short *buffer, int numElems)
// 16 bit samples // 16 bit samples
if (numElems < 1) return; // nothing to do if (numElems < 1) return; // nothing to do
if (header.format.bits_per_sample == 8) switch (header.format.bits_per_sample)
{
case 8:
{ {
int i; int i;
char *temp = new char[numElems]; unsigned char *temp = (unsigned char *)getConvBuffer(numElems);
// convert from 16bit format to 8bit format // convert from 16bit format to 8bit format
for (i = 0; i < numElems; i ++) for (i = 0; i < numElems; i ++)
{ {
temp[i] = buffer[i] >> 8; temp[i] = (unsigned char)(buffer[i] / 256 + 128);
} }
// write in 8bit format // write in 8bit format
write(temp, numElems); write(temp, numElems);
delete[] temp; break;
} }
else
case 16:
{ {
// 16bit format // 16bit format
short *pTemp = new short[numElems];
if (header.format.bits_per_sample != 16) // use temp buffer to swap byte order if necessary
{ short *pTemp = (short *)getConvBuffer(numElems * sizeof(short));
stringstream ss;
ss << "\nOnly 8/16 bit sample WAV files supported. Can't open WAV file with ";
ss << (int)header.format.bits_per_sample;
ss << " bit sample format. ";
ST_THROW_RT_ERROR(ss.str().c_str());
}
// allocate temp buffer to swap byte order if necessary
memcpy(pTemp, buffer, numElems * 2); memcpy(pTemp, buffer, numElems * 2);
_swap16Buffer(pTemp, numElems); _swap16Buffer(pTemp, numElems);
res = (int)fwrite(pTemp, 2, numElems, fptr); res = (int)fwrite(pTemp, 2, numElems, fptr);
delete[] pTemp;
if (res != numElems) if (res != numElems)
{ {
ST_THROW_RT_ERROR("Error while writing to a wav file."); ST_THROW_RT_ERROR("Error while writing to a wav file.");
} }
bytesWritten += 2 * numElems; bytesWritten += 2 * numElems;
break;
}
default:
{
stringstream ss;
ss << "\nOnly 8/16 bit sample WAV files supported in integer compilation. Can't open WAV file with ";
ss << (int)header.format.bits_per_sample;
ss << " bit sample format. ";
ST_THROW_RT_ERROR(ss.str().c_str());
}
} }
} }

View File

@ -188,7 +188,7 @@ public:
/// elements as are left in the file. /// elements as are left in the file.
/// ///
/// \return Number of 8-bit integers read from the file. /// \return Number of 8-bit integers read from the file.
int read(char *buffer, int maxElems); int read(unsigned char *buffer, int maxElems);
/// Reads audio samples from the WAV file to 16 bit integer format. Reads given number /// Reads audio samples from the WAV file to 16 bit integer format. Reads given number
/// of elements from the file or if end-of-file reached, as many elements as are /// of elements from the file or if end-of-file reached, as many elements as are
@ -256,7 +256,7 @@ public:
/// Write data to WAV file. This function works only with 8bit samples. /// Write data to WAV file. This function works only with 8bit samples.
/// Throws a 'runtime_error' exception if writing to file fails. /// Throws a 'runtime_error' exception if writing to file fails.
void write(const char *buffer, ///< Pointer to sample data buffer. void write(const unsigned char *buffer, ///< Pointer to sample data buffer.
int numElems ///< How many array items are to be written to file. int numElems ///< How many array items are to be written to file.
); );