Permalink
Browse files

Fast ogg scale

  • Loading branch information...
1 parent 9fac5d7 commit a92b9135067d3e9260998f79b1f4bef3e765cdf4 @Marisa-Chan committed Jan 22, 2013
Showing with 34 additions and 8 deletions.
  1. +31 −7 src/SFML/Audio/SoundFile.cpp
  2. +3 −1 src/SFML/Audio/SoundFile.hpp
@@ -53,7 +53,9 @@ SoundFile::SoundFile() :
m_file (NULL),
m_sampleCount (0),
m_channelCount(0),
-m_sampleRate (0)
+m_sampleRate (0),
+m_oggmax (1.0),
+m_floatformat (false)
{
}
@@ -218,13 +220,35 @@ bool SoundFile::openWrite(const std::string& filename, unsigned int channelCount
////////////////////////////////////////////////////////////
std::size_t SoundFile::read(Int16* data, std::size_t sampleCount)
{
- if (m_file && data && sampleCount)
- return static_cast<std::size_t>(sf_read_short(m_file, data, sampleCount));
+ if (m_file && data && sampleCount)
+ {
+ if (m_floatformat)
+ {
+ Int16* d = data;
+
+ for(std::size_t i=0; i<sampleCount; i+=m_channelCount)
+ {
+ float raw[16];
+ if (sf_read_float(m_file, raw, m_channelCount) != m_channelCount)
+ return i;
+
+ for (Int8 j=0; j< m_channelCount; j++)
+ {
+ if (raw[j] > m_oggmax)
+ m_oggmax = raw[j];
+
+ *d = (float)(raw[j] / m_oggmax) * 32767.0f;
+ d++;
+ }
+ }
+ }
+ else
+ return static_cast<std::size_t>(sf_read_short(m_file, data, sampleCount));
+ }
else
return 0;
}
-
-
+
////////////////////////////////////////////////////////////
void SoundFile::write(const Int16* data, std::size_t sampleCount)
{
@@ -264,8 +288,8 @@ void SoundFile::initialize(SF_INFO fileInfo)
// Enable scaling for Vorbis files (float samples)
// @todo enable when it's faster (it currently has to iterate over the *whole* music)
- //if (fileInfo.format & SF_FORMAT_VORBIS)
- // sf_command(m_file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE);
+ if (fileInfo.format & SF_FORMAT_VORBIS)
+ m_floatformat = true;
}
@@ -213,7 +213,9 @@ private :
Memory m_memory; ///< Memory reading info
std::size_t m_sampleCount; ///< Total number of samples in the file
unsigned int m_channelCount; ///< Number of channels used by the sound
- unsigned int m_sampleRate; ///< Number of samples per second
+ unsigned int m_sampleRate; ///< Number of samples per second
+ bool m_floatformat; ///< Float point sound format
+ float m_oggmax; ///< Dynamic ogg max level
};
} // namespace priv

4 comments on commit a92b913

Been a while since I've done some DSP, but wouldn't you determine the max scaling value over the whole track first rather then determining it "on the fly" while scaling already? E.g. the values in raw could be [5, 10, 15] you'd scale the first value using 5, the second one using 10, etc., resulting in [32767.0, 32767.0, 32767], because the maximum changes every iteration? Or am I missing something right now and this is working differently and/or intentionally that way?

Owner

Marisa-Chan replied Jan 24, 2013

Ah, okay, so it's essentially a "lesser evil" approach? Cause it should work fine once the maximum has been reached.

Owner

Marisa-Chan replied Jan 24, 2013

Please sign in to comment.