Opus: Replaced example file, coding style and rewrote writeInt in opuswriter

This commit is contained in:
Ferdinand Thiessen 2016-04-02 23:40:19 +02:00 committed by Lukas Dürrenberger
parent 96d4b28bf2
commit 90b6f3286e
4 changed files with 28 additions and 12 deletions

Binary file not shown.

View File

@ -54,6 +54,7 @@ namespace
case SEEK_END: case SEEK_END:
offset = stream->getSize() - offset; offset = stream->getSize() - offset;
} }
// Return value expected from libopusfile: 0 - Success and -1 - Failure // Return value expected from libopusfile: 0 - Success and -1 - Failure
return static_cast<int>(stream->seek(offset)) >= 0 ? 0 : -1; return static_cast<int>(stream->seek(offset)) >= 0 ? 0 : -1;
} }
@ -120,6 +121,7 @@ bool SoundFileReaderOpus::open(InputStream& stream, Info& info)
info.channelCount = opusHead->channel_count; info.channelCount = opusHead->channel_count;
info.sampleRate = opusHead->input_sample_rate; info.sampleRate = opusHead->input_sample_rate;
info.sampleCount = static_cast<std::size_t>(op_pcm_total(m_opus, -1) * opusHead->channel_count); info.sampleCount = static_cast<std::size_t>(op_pcm_total(m_opus, -1) * opusHead->channel_count);
// We must keep the channel count for the seek function // We must keep the channel count for the seek function
m_channelCount = info.channelCount; m_channelCount = info.channelCount;
@ -142,10 +144,12 @@ Uint64 SoundFileReaderOpus::read(Int16* samples, Uint64 maxCount)
assert(m_opus != NULL); assert(m_opus != NULL);
int samplesToRead; int samplesToRead;
// Try to read the requested number of samples, stop only on error or end of file // Try to read the requested number of samples, stop only on error or end of file
Uint64 count = 0; Uint64 count = 0;
while (maxCount > 0) while (maxCount > 0)
{ {
// since maxCount is uint64 we have to ensure that samplesToRead is <= INT_MAX (int overflow) // since maxCount is uint64 we have to ensure that samplesToRead is <= INT_MAX (int overflow)
if (maxCount > INT_MAX) if (maxCount > INT_MAX)
{ {
@ -155,6 +159,7 @@ Uint64 SoundFileReaderOpus::read(Int16* samples, Uint64 maxCount)
{ {
samplesToRead = maxCount; samplesToRead = maxCount;
} }
// op_read returns number of SAMPLES read PER CHANNEL // op_read returns number of SAMPLES read PER CHANNEL
int samplesRead = op_read(m_opus, samples, samplesToRead, NULL) * m_channelCount; int samplesRead = op_read(m_opus, samples, samplesToRead, NULL) * m_channelCount;
if (samplesRead > 0) if (samplesRead > 0)

View File

@ -34,12 +34,19 @@
#include <iostream> #include <iostream>
// Make sure to write int into buffer little endian
#define writeint(buf, offset, val) { buf[offset+3]=((val)>>24)&0xff; \ // Anonymous namespace
buf[offset+2]=((val)>>16)&0xff; \ namespace
buf[offset+1]=((val)>>8)&0xff; \ {
buf[offset]=(val)&0xff; \ // Make sure to write int into buffer little endian
} void writeInt(unsigned char buf[], size_t offset, sf::Uint32 val)
{
buf[offset+3]=((val)>>24)&0xff;
buf[offset+2]=((val)>>16)&0xff;
buf[offset+1]=((val)>>8)&0xff;
buf[offset]=(val)&0xff;
}
}
namespace sf namespace sf
{ {
@ -116,7 +123,7 @@ bool SoundFileWriterOpus::open(const std::string& filename, unsigned int sampleR
memcpy(static_cast<void*>(headerData), "OpusHead", 8); memcpy(static_cast<void*>(headerData), "OpusHead", 8);
headerData[8] = 1; // Version headerData[8] = 1; // Version
headerData[9] = channelCount; headerData[9] = channelCount;
writeint(headerData, 12, static_cast<Uint32>(sampleRate)); writeInt(headerData, 12, static_cast<Uint32>(sampleRate));
headerData[18] = channelCount > 8 ? 255 : (channelCount > 2); // Mapping family headerData[18] = channelCount > 8 ? 255 : (channelCount > 2); // Mapping family
// Map opus header to ogg packet // Map opus header to ogg packet
@ -141,12 +148,15 @@ bool SoundFileWriterOpus::open(const std::string& filename, unsigned int sampleR
// Magic bytes // Magic bytes
memcpy(static_cast<void*>(commentData), "OpusTags", 8); memcpy(static_cast<void*>(commentData), "OpusTags", 8);
// unsigned 32bit integer: Length of vendor string (encoding library) // unsigned 32bit integer: Length of vendor string (encoding library)
writeint(commentData, 8, opusVersionLength); writeInt(commentData, 8, opusVersionLength);
// Vendor string // Vendor string
memcpy(static_cast<void*>(commentData+12), opusVersion, opusVersionLength); memcpy(static_cast<void*>(commentData+12), opusVersion, opusVersionLength);
// Length of user comments (E.g. you can add a ENCODER tag for SFML) // Length of user comments (E.g. you can add a ENCODER tag for SFML)
writeint(commentData, 12+opusVersionLength, 0); writeInt(commentData, 12+opusVersionLength, 0);
op.packet = commentData; op.packet = commentData;
op.bytes = commentLength; op.bytes = commentLength;
@ -178,6 +188,7 @@ void SoundFileWriterOpus::write(const Int16* samples, Uint64 count)
while (count > 0) while (count > 0)
{ {
opus_int32 packet_size; opus_int32 packet_size;
// Check if wee need to pad the input // Check if wee need to pad the input
if (count < (frame_size * m_channelCount)) if (count < (frame_size * m_channelCount))
{ {
@ -209,6 +220,7 @@ void SoundFileWriterOpus::write(const Int16* samples, Uint64 count)
frame_number++; frame_number++;
} }
// Flush any produced block // Flush any produced block
flushBlocks(); flushBlocks();
} }
@ -232,7 +244,6 @@ void SoundFileWriterOpus::close()
if (m_file.is_open()) if (m_file.is_open())
{ {
flushBlocks(); flushBlocks();
// Close the file
m_file.close(); m_file.close();
} }

View File

@ -49,8 +49,8 @@ cd $LOCALDIR/build/libvorbis-* && sed -i 's/-version-info/-avoid-version/g' lib
rm $DESTDIR/$1/usr/lib/libvorbis*.so* rm $DESTDIR/$1/usr/lib/libvorbis*.so*
# Compile OPUS and OPUSFILE # Compile OPUS and OPUSFILE
cd $LOCALDIR/build/opus-* && sed -i '-version-info/-avoid-version/g' Makefile.in Makefile.am && ./configure $HOST $PREFIX --enabled-shared=no && make && make install cd $LOCALDIR/build/opus-* && sed -i 's/-version-info/-avoid-version/g' Makefile.in Makefile.am && ./configure $HOST $PREFIX --enabled-shared=no && make && make install
cd $LOCALDIR/build/opusfile-* && sed -i '-version-info/-avoid-version/g' Makefile.in Makefile.am && ./configure $HOST $PREFIX --enabled-shared=no && make && make install cd $LOCALDIR/build/opusfile-* && sed -i 's/-version-info/-avoid-version/g' Makefile.in Makefile.am && ./configure $HOST $PREFIX --enabled-shared=no && make && make install
rm $DESTDIR/$1/usr/lib/libopus*.so* rm $DESTDIR/$1/usr/lib/libopus*.so*
# Compile freetype # Compile freetype