Skip to content

Commit

Permalink
vorbis decoding of ogg via miniaudio working, specifies all licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Dec 17, 2023
1 parent 981de77 commit 228af52
Show file tree
Hide file tree
Showing 438 changed files with 114,032 additions and 103 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ else()

endif()

add_subdirectory(include/vendored/vorbis)
set(${VORBIS_INCLUDE_DIRS} include/vendored/vorbis/include)

file(GLOB MINIAUDIO_SRC "include/vendored/miniaudio/*.c")
include_directories(include/vendored/miniaudio)
include_directories(include/vendored/miniaudio ${VORBIS_INCLUDE_DIRS})
add_library(Miniaudio STATIC ${MINIAUDIO_SRC})
target_link_libraries(Miniaudio vorbisfile)

include_directories(include include/vendored ${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE} ${ZLIB_DEPS_DIR})

Expand All @@ -141,6 +145,7 @@ file(GLOB SRC
"src/Debug/*.cpp"
"src/Console/*.cpp"
"src/System/Rendering/*.cpp"
"src/System/Sound/*.cpp"
)

if (NOT ANDROID)
Expand Down
File renamed without changes.
390 changes: 390 additions & 0 deletions LICENSES.md

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ A lightweight 2D game engine, in C++ with embedded Lua
- OpenGL rendering (soon to be provided by jGL for OpenGL/Vulkan interchangeability)
- Perlin (marching squares) and Tile map, editable tile based worlds
- Lua console, interoperating with the ECS, physics, and rendering systems

### OSS Dependencies and Licenses

- Freetype is licensed under the The FreeType Project LICENSE
- GLEW is licensed under aModified BSD License, the Mesa 3-D License (MIT) and the Khronos License (MIT).
- GLFW is licensed under the zlib/libpng
- GLM is licensed under the MIT License (but also, no bunnies have been made unhappy)
- Lua is licensed under the MIT license
- Miniaudio is licensed under the MIT-0 license
- stduuid is licensed under the MIT license
- vorbis is licensed under a BSD license
- zlib is licensed under the zlib license

We thank: David Turner, Robert Wilhelm, and Werner Lemberg (Freetype), Milan Ikits <milan ikits[]ieee org>, Marcelo E. Magallon <mmagallo[]debian org>, and Lev Povalahev Brian Paul, The Khronos Group Inc (GLEW), Marcus Geelnard and Camilla Löwy (GLFW), G-Truc Creation (GLM), Lua.org, PUC-Rio (Lua), David Reid (Miniaudio), Marius Bancila https://github.com/mariusbancila/stduuid#MIT-1-ov-file (stduuid), Xiph.org Foundation (vorbis), and Jean-loup Gailly and Mark Adler (zlib).
2 changes: 2 additions & 0 deletions demo/desktop/softBodyTetris/standalone/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ int main(int argc, char ** argv)

Hop::System::Sound::sSound & sound = manager.getSystem<Hop::System::Sound::sSound>();

sound.setLoopSound("rain");

while (display.isOpen())
{

Expand Down
2 changes: 1 addition & 1 deletion demo/desktop/softBodyTetris/standalone/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace std::chrono;
#include <System/sPhysics.h>
#include <System/Rendering/sRender.h>
#include <System/sCollision.h>
#include <System/sSound.h>
#include <System/Sound/sSound.h>

#include <World/world.h>
#include <World/marchingWorld.h>
Expand Down
2 changes: 1 addition & 1 deletion include/Object/entityComponentSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <System/Rendering/sSpriteRender.h>
#include <System/sPhysics.h>
#include <System/sCollision.h>
#include <System/sSound.h>
#include <System/Sound/sSound.h>

#include <unordered_map>
#include <map>
Expand Down
154 changes: 154 additions & 0 deletions include/System/Sound/sSound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#ifndef SSOUND_H
#define SSOUND_H

#define MA_NO_VORBIS
#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio/miniaudio.h>
#include <System/Sound/vorbis.h>

#include <log.h>
#include <sstream>

#include <Object/entityComponentSystem.h>

namespace Hop::Object
{
class EntityComponentSystem;
}

namespace Hop::System::Sound
{

void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);

std::string maErrorToString(ma_result code);

enum class DECODER {MA, VORBIS, NONE};

class sSound : public System
{

public:

sSound()
: loopingFile("")
{
ma_result result = ma_engine_init(NULL, &engine);
}

~sSound()
{
ma_engine_uninit(&engine);
ma_device_uninit(&device);
ma_decoder_uninit(&decoder);
}

ma_result decode(std::string file)
{
ma_result result = ma_decoder_init_file(file.c_str(), NULL, &decoder);

if (result != MA_SUCCESS)
{
vorbisConfig = ma_decoder_config_init_default();
vorbisConfig.pCustomBackendUserData = NULL;
vorbisConfig.ppCustomBackendVTables = pCustomBackendVTables;
vorbisConfig.customBackendCount = sizeof(pCustomBackendVTables) / sizeof(pCustomBackendVTables[0]);

result = ma_decoder_init_file(file.c_str(), &vorbisConfig, &vorbisDecoder);

if (result == MA_SUCCESS)
{
decoderInUse = DECODER::VORBIS;
}
}
else
{
decoderInUse = DECODER::MA;
}

return result;
}

void setLoopSound(std::string filename)
{
if (loopingFile == "")
{
loopingFile = filename;

ma_result result = decode(filename);

if (result != MA_SUCCESS)
{
Hop::Logging::Log log;
std::stringstream ss;
ss << "Could not decode audio file: " << filename << ", got error: " << maErrorToString(result);
Hop::Logging::ERROR(ss.str()) >> log;
}

if (decoderInUse == DECODER::MA)
{
ma_data_source_set_looping(&decoder, MA_TRUE);

config = ma_device_config_init(ma_device_type_playback);

config.playback.format = decoder.outputFormat;
config.playback.channels = decoder.outputChannels;
config.sampleRate = decoder.outputSampleRate;
config.dataCallback = data_callback;
config.pUserData = &decoder;
}
else
{
ma_data_source_set_looping(&vorbisDecoder, MA_TRUE);

config = ma_device_config_init(ma_device_type_playback);

config.playback.format = vorbisDecoder.outputFormat;
config.playback.channels = vorbisDecoder.outputChannels;
config.sampleRate = vorbisDecoder.outputSampleRate;
config.dataCallback = data_callback;
config.pUserData = &vorbisDecoder;
}

ma_device_init(NULL, &config, &device);

ma_device_start(&device);
looping = true;
}
}

void stopLoopSound()
{
if (looping)
{
ma_device_stop(&device);
}
}

private:

ma_engine engine;

ma_decoder decoder;

ma_decoder vorbisDecoder;
ma_decoder_config vorbisConfig;

ma_device device;
ma_device_config config;

std::string loopingFile;
bool looping = false;

DECODER decoderInUse = DECODER::NONE;

ma_decoding_backend_vtable* pCustomBackendVTables[1] =
{
&g_ma_decoding_backend_vtable_libvorbis
};


};

}
#endif /* SSOUND_H */
91 changes: 91 additions & 0 deletions include/System/Sound/vorbis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#ifndef VORBIS_H
#define VORBIS_H

#include <miniaudio/miniaudio.h>

/*
Miniaudio's libvorbis example
https://miniaud.io/docs/examples/custom_decoder.html
*/

namespace Hop::System::Sound
{
static ma_result ma_decoding_backend_init__libvorbis(void* pUserData, ma_read_proc onRead, ma_seek_proc onSeek, ma_tell_proc onTell, void* pReadSeekTellUserData, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_data_source** ppBackend)
{
ma_result result;
ma_libvorbis* pVorbis;

(void)pUserData;

pVorbis = (ma_libvorbis*)ma_malloc(sizeof(*pVorbis), pAllocationCallbacks);
if (pVorbis == NULL) {
return MA_OUT_OF_MEMORY;
}

result = ma_libvorbis_init(onRead, onSeek, onTell, pReadSeekTellUserData, pConfig, pAllocationCallbacks, pVorbis);
if (result != MA_SUCCESS) {
ma_free(pVorbis, pAllocationCallbacks);
return result;
}

*ppBackend = pVorbis;

return MA_SUCCESS;
}

static ma_result ma_decoding_backend_init_file__libvorbis(void* pUserData, const char* pFilePath, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_data_source** ppBackend)
{
ma_result result;
ma_libvorbis* pVorbis;

(void)pUserData;

pVorbis = (ma_libvorbis*)ma_malloc(sizeof(*pVorbis), pAllocationCallbacks);
if (pVorbis == NULL) {
return MA_OUT_OF_MEMORY;
}

result = ma_libvorbis_init_file(pFilePath, pConfig, pAllocationCallbacks, pVorbis);
if (result != MA_SUCCESS) {
ma_free(pVorbis, pAllocationCallbacks);
return result;
}

*ppBackend = pVorbis;

return MA_SUCCESS;
}

static void ma_decoding_backend_uninit__libvorbis(void* pUserData, ma_data_source* pBackend, const ma_allocation_callbacks* pAllocationCallbacks)
{
ma_libvorbis* pVorbis = (ma_libvorbis*)pBackend;

(void)pUserData;

ma_libvorbis_uninit(pVorbis, pAllocationCallbacks);
ma_free(pVorbis, pAllocationCallbacks);
}

static ma_result ma_decoding_backend_get_channel_map__libvorbis(void* pUserData, ma_data_source* pBackend, ma_channel* pChannelMap, size_t channelMapCap)
{
ma_libvorbis* pVorbis = (ma_libvorbis*)pBackend;

(void)pUserData;

return ma_libvorbis_get_data_format(pVorbis, NULL, NULL, NULL, pChannelMap, channelMapCap);
}

static ma_decoding_backend_vtable g_ma_decoding_backend_vtable_libvorbis =
{
ma_decoding_backend_init__libvorbis,
ma_decoding_backend_init_file__libvorbis,
NULL, /* onInitFileW() */
NULL, /* onInitMemory() */
ma_decoding_backend_uninit__libvorbis
};

}

#endif /* VORBIS_H */
83 changes: 0 additions & 83 deletions include/System/sSound.h

This file was deleted.

Loading

0 comments on commit 228af52

Please sign in to comment.