Skip to content

Commit

Permalink
Add: Support sound effects in Ogg Opus format.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterN committed Apr 30, 2024
1 parent bf902bd commit 3809dba
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
libicu-dev \
liblzma-dev \
liblzo2-dev \
libopusfile-dev \
libsoxr-dev \
${{ inputs.libraries }} \
zlib1g-dev \
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-mingw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
mingw-w64-${{ inputs.arch }}-libpng
mingw-w64-${{ inputs.arch }}-lld
mingw-w64-${{ inputs.arch }}-ninja
mingw-w64-${{ inputs.arch }}-opusfile
mingw-w64-${{ inputs.arch }}-soxr
- name: Install OpenGFX
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
libicu-dev \
liblzma-dev \
liblzo2-dev \
libopusfile-dev \
libsdl2-dev \
libsoxr-dev \
zlib1g-dev \
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ if(NOT OPTION_DEDICATED)
find_package(ICU OPTIONAL_COMPONENTS i18n)
endif()
endif()
find_package(OpusFile)
find_package(Soxr)
endif()
if(APPLE)
Expand Down Expand Up @@ -332,6 +333,7 @@ if(NOT OPTION_DEDICATED)
link_package(Fontconfig TARGET Fontconfig::Fontconfig)
link_package(Harfbuzz TARGET harfbuzz::harfbuzz)
link_package(ICU_i18n)
link_package(OpusFile TARGET OpusFile::opusfile)
link_package(Soxr)

if(SDL2_FOUND AND OPENGL_FOUND AND UNIX)
Expand Down
37 changes: 37 additions & 0 deletions cmake/FindOpusFile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
include(FindPackageHandleStandardArgs)

find_library(OpusFile_LIBRARY
NAMES opusfile
)

set(OpusFile_COMPILE_OPTIONS "" CACHE STRING "Extra compile options of opusfile")

set(OpusFile_LINK_LIBRARIES "" CACHE STRING "Extra link libraries of opusfile")

set(OpusFile_LINK_FLAGS "" CACHE STRING "Extra link flags of opusfile")

find_path(OpusFile_INCLUDE_PATH
NAMES opusfile.h
PATH_SUFFIXES opus
)

find_package_handle_standard_args(OpusFile
REQUIRED_VARS OpusFile_LIBRARY OpusFile_INCLUDE_PATH
)

if (OpusFile_FOUND)
set(OpusFile_dirs ${OpusFile_INCLUDE_PATH})
if(EXISTS "${OpusFile_INCLUDE_PATH}/opus")
list(APPEND OpusFile_dirs "${OpusFile_INCLUDE_PATH}/opus")
endif()
if (NOT TARGET OpusFile::opusfile)
add_library(OpusFile::opusfile UNKNOWN IMPORTED)
set_target_properties(OpusFile::opusfile PROPERTIES
IMPORTED_LOCATION "${OpusFile_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${OpusFile_dirs}"
INTERFACE_COMPILE_OPTIONS "${OpusFile_COMPILE_OPTIONS}"
INTERFACE_LINK_LIBRARIES "${OpusFile_LINK_LIBRARIES}"
INTERFACE_LINK_FLAGS "${OpusFile_LINK_FLAGS}"
)
endif()
endif()
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ add_files(
CONDITION ICU_i18n_FOUND AND HARFBUZZ_FOUND
)

add_files(
soundloader_opus.cpp
CONDITION OPUSFILE_FOUND
)

add_files(
aircraft.h
aircraft_cmd.cpp
Expand Down
92 changes: 92 additions & 0 deletions src/soundloader_opus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file sound_opus.cpp Loading of opus sounds. */

#include "stdafx.h"
#include "core/bitmath_func.hpp"
#include "random_access_file_type.h"
#include "sound_type.h"
#include "soundloader_type.h"

#include <opusfile.h>

#include "safeguards.h"

/** Opus sound louder. */
class SoundLoader_Opus : public SoundLoader {
public:
SoundLoader_Opus() : SoundLoader("opus", 10) {}

/* For good results, you will need at least 57 bytes (for a pure Opus-only stream). */
static constexpr size_t MIN_OPUS_FILE_SIZE = 57U;

/* It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (5760 values per channel).
* Smaller buffers will simply return less data, possibly consuming more memory to buffer the data internally. */
static constexpr size_t DECODE_BUFFER_SAMPLES = 5760 * 2;
static constexpr size_t DECODE_BUFFER_BYTES = DECODE_BUFFER_SAMPLES * sizeof(opus_int16);

bool Load(SoundEntry &sound, bool new_format, std::vector<uint8_t> &data) override
{
if (!new_format) return false;

/* At least 57 bytes are needed for an Opus-only file. */
if (sound.file_size < MIN_OPUS_FILE_SIZE) return false;

/* Test if data is an Ogg Opus stream. */
auto filepos = sound.file->GetPos();
std::vector<uint8_t> tmp(std::min(sound.file_size, MIN_OPUS_FILE_SIZE));
sound.file->ReadBlock(tmp.data(), tmp.size());
if (op_test(nullptr, tmp.data(), tmp.size()) != 0) return false;

/* Read the whole file into memory. */
tmp.resize(sound.file_size);
sound.file->SeekTo(filepos, SEEK_SET);
sound.file->ReadBlock(tmp.data(), tmp.size());

int error = 0;
auto of = std::unique_ptr<OggOpusFile, OggOpusFileDeleter>(op_open_memory(tmp.data(), tmp.size(), &error));
if (error != 0) return false;

size_t datapos = 0;
for (;;) {
data.resize(datapos + DECODE_BUFFER_BYTES);
int li;
int read = op_read(of.get(), reinterpret_cast<opus_int16 *>(&data[datapos]), DECODE_BUFFER_BYTES, &li);
if (read == 0) break;

if (read < 0 || op_channel_count(of.get(), li) != 1) {
/* Error reading, or incorrect channel count. */
data.clear();
return false;
}

datapos += read * sizeof(opus_int16);
}

/* OpusFile always decodes at 48kHz. */
sound.channels = 1;
sound.bits_per_sample = 16;
sound.rate = 48000;

/* We resized by DECODE_BUFFER_BYTES just before finally reading zero bytes, undo this. */
data.resize(data.size() - DECODE_BUFFER_BYTES);

return true;
}

private:
/** Helper class to properly RAII release an OggOpusFile. */
struct OggOpusFileDeleter {
void operator()(OggOpusFile *of)
{
if (of != nullptr) op_free(of);
}
};
};

static SoundLoader_Opus _sound_loader_opus;
3 changes: 3 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
{
"name": "lzo"
},
{
"name": "opusfile"
},
{
"name": "soxr"
},
Expand Down

0 comments on commit 3809dba

Please sign in to comment.