mirror of
https://github.com/SFML/SFML.git
synced 2025-03-14 01:40:05 +08:00
Opus: Replaced example file, coding style and rewrote writeInt in opuswriter
This commit is contained in:
parent
96d4b28bf2
commit
90b6f3286e
Binary file not shown.
@ -54,6 +54,7 @@ namespace
|
||||
case SEEK_END:
|
||||
offset = stream->getSize() - offset;
|
||||
}
|
||||
|
||||
// Return value expected from libopusfile: 0 - Success and -1 - Failure
|
||||
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.sampleRate = opusHead->input_sample_rate;
|
||||
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
|
||||
m_channelCount = info.channelCount;
|
||||
|
||||
@ -142,10 +144,12 @@ Uint64 SoundFileReaderOpus::read(Int16* samples, Uint64 maxCount)
|
||||
assert(m_opus != NULL);
|
||||
|
||||
int samplesToRead;
|
||||
|
||||
// Try to read the requested number of samples, stop only on error or end of file
|
||||
Uint64 count = 0;
|
||||
while (maxCount > 0)
|
||||
{
|
||||
|
||||
// since maxCount is uint64 we have to ensure that samplesToRead is <= INT_MAX (int overflow)
|
||||
if (maxCount > INT_MAX)
|
||||
{
|
||||
@ -155,6 +159,7 @@ Uint64 SoundFileReaderOpus::read(Int16* samples, Uint64 maxCount)
|
||||
{
|
||||
samplesToRead = maxCount;
|
||||
}
|
||||
|
||||
// op_read returns number of SAMPLES read PER CHANNEL
|
||||
int samplesRead = op_read(m_opus, samples, samplesToRead, NULL) * m_channelCount;
|
||||
if (samplesRead > 0)
|
||||
|
@ -34,12 +34,19 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// Make sure to write int into buffer little endian
|
||||
#define writeint(buf, offset, val) { buf[offset+3]=((val)>>24)&0xff; \
|
||||
buf[offset+2]=((val)>>16)&0xff; \
|
||||
buf[offset+1]=((val)>>8)&0xff; \
|
||||
buf[offset]=(val)&0xff; \
|
||||
|
||||
// Anonymous namespace
|
||||
namespace
|
||||
{
|
||||
// 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
|
||||
{
|
||||
@ -116,7 +123,7 @@ bool SoundFileWriterOpus::open(const std::string& filename, unsigned int sampleR
|
||||
memcpy(static_cast<void*>(headerData), "OpusHead", 8);
|
||||
headerData[8] = 1; // Version
|
||||
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
|
||||
|
||||
// Map opus header to ogg packet
|
||||
@ -141,12 +148,15 @@ bool SoundFileWriterOpus::open(const std::string& filename, unsigned int sampleR
|
||||
|
||||
// Magic bytes
|
||||
memcpy(static_cast<void*>(commentData), "OpusTags", 8);
|
||||
|
||||
// unsigned 32bit integer: Length of vendor string (encoding library)
|
||||
writeint(commentData, 8, opusVersionLength);
|
||||
writeInt(commentData, 8, opusVersionLength);
|
||||
|
||||
// Vendor string
|
||||
memcpy(static_cast<void*>(commentData+12), opusVersion, opusVersionLength);
|
||||
|
||||
// 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.bytes = commentLength;
|
||||
@ -178,6 +188,7 @@ void SoundFileWriterOpus::write(const Int16* samples, Uint64 count)
|
||||
while (count > 0)
|
||||
{
|
||||
opus_int32 packet_size;
|
||||
|
||||
// Check if wee need to pad the input
|
||||
if (count < (frame_size * m_channelCount))
|
||||
{
|
||||
@ -209,6 +220,7 @@ void SoundFileWriterOpus::write(const Int16* samples, Uint64 count)
|
||||
|
||||
frame_number++;
|
||||
}
|
||||
|
||||
// Flush any produced block
|
||||
flushBlocks();
|
||||
}
|
||||
@ -232,7 +244,6 @@ void SoundFileWriterOpus::close()
|
||||
if (m_file.is_open())
|
||||
{
|
||||
flushBlocks();
|
||||
// Close the file
|
||||
m_file.close();
|
||||
}
|
||||
|
||||
|
@ -49,8 +49,8 @@ cd $LOCALDIR/build/libvorbis-* && sed -i 's/-version-info/-avoid-version/g' lib
|
||||
rm $DESTDIR/$1/usr/lib/libvorbis*.so*
|
||||
|
||||
# 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/opusfile-* && 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 '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*
|
||||
|
||||
# Compile freetype
|
||||
|
Loading…
x
Reference in New Issue
Block a user