diff --git a/examples/sound/resources/error.opus b/examples/sound/resources/error.opus index b3197dd1..55823645 100644 Binary files a/examples/sound/resources/error.opus and b/examples/sound/resources/error.opus differ diff --git a/src/SFML/Audio/SoundFileReaderOpus.cpp b/src/SFML/Audio/SoundFileReaderOpus.cpp index 0ba7a60a..3990461b 100644 --- a/src/SFML/Audio/SoundFileReaderOpus.cpp +++ b/src/SFML/Audio/SoundFileReaderOpus.cpp @@ -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(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(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) diff --git a/src/SFML/Audio/SoundFileWriterOpus.cpp b/src/SFML/Audio/SoundFileWriterOpus.cpp index ce790c9d..8918833c 100644 --- a/src/SFML/Audio/SoundFileWriterOpus.cpp +++ b/src/SFML/Audio/SoundFileWriterOpus.cpp @@ -34,12 +34,19 @@ #include -// 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(headerData), "OpusHead", 8); headerData[8] = 1; // Version headerData[9] = channelCount; - writeint(headerData, 12, static_cast(sampleRate)); + writeInt(headerData, 12, static_cast(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(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(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(); } diff --git a/tools/android/compile_libs.sh b/tools/android/compile_libs.sh index a66bb751..e8349f32 100644 --- a/tools/android/compile_libs.sh +++ b/tools/android/compile_libs.sh @@ -49,14 +49,14 @@ 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 cd $LOCALDIR/build/freetype-* && sed -i 's/-version-info/-avoid-version/g' builds/unix/unix-cc.in && ./configure $HOST $PREFIX && make && make install # Compile OpenAL-Soft -cd $LOCALDIR/build/openal-soft-android-master && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_CMAKE_TOOLCHAIN -DANDROID_ABI=$ANDROID_ABI -DANDROID_NATIVE_API_LEVEL=android-9 -DANDROID_USE_STLPORT=1 .. && make openal && mv libopenal.so $DESTDIR/$1/usr/lib +cd $LOCALDIR/build/openal-soft-android-master && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_CMAKE_TOOLCHAIN -DANDROID_ABI=$ANDROID_ABI -DANDROID_NATIVE_API_LEVEL=android-9 -DANDROID_USE_STLPORT=1 .. && make openal && mkdir -p "$DESTDIR/$1/usr/lib" && mv libopenal.so $DESTDIR/$1/usr/lib export PATH=$OPATH