Added support for 24-bit .wav files.

This commit is contained in:
Maximilian Wagenbach 2015-08-31 21:52:27 +02:00 committed by Lukas Dürrenberger
parent 221e0700f7
commit b7d7ac44f3
2 changed files with 31 additions and 7 deletions

View File

@ -62,7 +62,8 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Open a sound file from the disk for reading /// \brief Open a sound file from the disk for reading
/// ///
/// The supported audio formats are: WAV, OGG/Vorbis, FLAC. /// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC.
/// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
/// ///
/// \param filename Path of the sound file to load /// \param filename Path of the sound file to load
/// ///
@ -74,7 +75,8 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Open a sound file in memory for reading /// \brief Open a sound file in memory for reading
/// ///
/// The supported audio formats are: WAV, OGG/Vorbis, FLAC. /// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC.
/// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
/// ///
/// \param data Pointer to the file data in memory /// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes /// \param sizeInBytes Size of the data to load, in bytes
@ -87,7 +89,8 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Open a sound file from a custom stream for reading /// \brief Open a sound file from a custom stream for reading
/// ///
/// The supported audio formats are: WAV, OGG/Vorbis, FLAC. /// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC.
/// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
/// ///
/// \param stream Source stream to read from /// \param stream Source stream to read from
/// ///

View File

@ -65,13 +65,13 @@ namespace
return true; return true;
} }
bool decode(sf::InputStream& stream, sf::Int32& value) bool decode24bit(sf::InputStream& stream, sf::Uint32& value)
{ {
unsigned char bytes[sizeof(value)]; unsigned char bytes[3];
if (stream.read(bytes, sizeof(bytes)) != sizeof(bytes)) if (stream.read(bytes, sizeof(bytes)) != sizeof(bytes))
return false; return false;
value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16);
return true; return true;
} }
@ -169,15 +169,31 @@ Uint64 SoundFileReaderWav::read(Int16* samples, Uint64 maxCount)
break; break;
} }
case 3:
{
Uint32 sample = 0;
if (decode24bit(*m_stream, sample))
*samples++ = sample >> 8;
else
return count;
break;
}
case 4: case 4:
{ {
Int32 sample = 0; Uint32 sample = 0;
if (decode(*m_stream, sample)) if (decode(*m_stream, sample))
*samples++ = sample >> 16; *samples++ = sample >> 16;
else else
return count; return count;
break; break;
} }
default:
{
assert(false);
return 0;
}
} }
++count; ++count;
@ -248,6 +264,11 @@ bool SoundFileReaderWav::parseHeader(Info& info)
Uint16 bitsPerSample = 0; Uint16 bitsPerSample = 0;
if (!decode(*m_stream, bitsPerSample)) if (!decode(*m_stream, bitsPerSample))
return false; return false;
if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 24 && bitsPerSample != 32)
{
err() << "Unsupported sample size: " << bitsPerSample << " bit (Supported sample sizes are 8/16/24/32 bit)" << std::endl;
return false;
}
m_bytesPerSample = bitsPerSample / 8; m_bytesPerSample = bitsPerSample / 8;
// Skip potential extra information (should not exist for PCM) // Skip potential extra information (should not exist for PCM)