Skip to content

Commit

Permalink
Added support for 24-bit .wav files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Wagenbach authored and eXpl0it3r committed Sep 20, 2015
1 parent 221e070 commit b7d7ac4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
9 changes: 6 additions & 3 deletions include/SFML/Audio/InputSoundFile.hpp
Expand Up @@ -62,7 +62,8 @@ class SFML_AUDIO_API InputSoundFile : NonCopyable
////////////////////////////////////////////////////////////
/// \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
///
Expand All @@ -74,7 +75,8 @@ class SFML_AUDIO_API InputSoundFile : NonCopyable
////////////////////////////////////////////////////////////
/// \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 sizeInBytes Size of the data to load, in bytes
Expand All @@ -87,7 +89,8 @@ class SFML_AUDIO_API InputSoundFile : NonCopyable
////////////////////////////////////////////////////////////
/// \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
///
Expand Down
29 changes: 25 additions & 4 deletions src/SFML/Audio/SoundFileReaderWav.cpp
Expand Up @@ -65,13 +65,13 @@ namespace
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))
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;
}
Expand Down Expand Up @@ -169,15 +169,31 @@ Uint64 SoundFileReaderWav::read(Int16* samples, Uint64 maxCount)
break;
}

case 3:
{
Uint32 sample = 0;
if (decode24bit(*m_stream, sample))
*samples++ = sample >> 8;
else
return count;
break;
}

case 4:
{
Int32 sample = 0;
Uint32 sample = 0;
if (decode(*m_stream, sample))
*samples++ = sample >> 16;
else
return count;
break;
}

default:
{
assert(false);
return 0;
}
}

++count;
Expand Down Expand Up @@ -248,6 +264,11 @@ bool SoundFileReaderWav::parseHeader(Info& info)
Uint16 bitsPerSample = 0;
if (!decode(*m_stream, bitsPerSample))
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;

// Skip potential extra information (should not exist for PCM)
Expand Down

0 comments on commit b7d7ac4

Please sign in to comment.