Skip to content

Commit

Permalink
Fast ogg scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Marisa-Chan committed Jan 22, 2013
1 parent 9fac5d7 commit a92b913
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 31 additions & 7 deletions src/SFML/Audio/SoundFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}


Expand Down
4 changes: 3 additions & 1 deletion src/SFML/Audio/SoundFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

4 comments on commit a92b913

@MarioLiebisch
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@Marisa-Chan
Copy link
Owner Author

@Marisa-Chan Marisa-Chan commented on a92b913 Jan 24, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarioLiebisch
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Marisa-Chan
Copy link
Owner Author

@Marisa-Chan Marisa-Chan commented on a92b913 Jan 24, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.