Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions res/lottie/emoji_audio.json

Large diffs are not rendered by default.

130 changes: 130 additions & 0 deletions src/LottieAudio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2026 ThorVG project. All rights reserved.

* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:

* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

#include <thorvg-1/thorvg_lottie.h>
#include "Example.h"


/************************************************************************/
/* ThorVG Drawing Contents */
/************************************************************************/

struct UserExample : tvgexam::Example
{
unique_ptr<tvg::LottieAnimation> lottie;

ma_engine engine;
ma_decoder decoder;
ma_sound sound;
bool engineReady = false;
bool decoderReady = false;
bool soundReady = false;
float prevProgress = 0.0f;

~UserExample()
{
if (soundReady) ma_sound_uninit(&sound);
if (decoderReady) ma_decoder_uninit(&decoder);
if (engineReady) ma_engine_uninit(&engine);
}

void onAudio(const tvg::AudioInfo& info)
{
if (info.active) {
//lazy-init the sound from embedded audio bytes
if (!soundReady && !info.path && info.src && info.size > 0) {
if (ma_decoder_init_memory(info.src, info.size, nullptr, &decoder) == MA_SUCCESS) {
decoderReady = true;
auto flags = MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_NO_SPATIALIZATION;
soundReady = (ma_sound_init_from_data_source(&engine, &decoder, flags, nullptr, &sound) == MA_SUCCESS);
}
}

if (soundReady) {
auto sr = ma_engine_get_sample_rate(&engine);
ma_sound_seek_to_pcm_frame(&sound, static_cast<ma_uint64>(info.offset * sr));
ma_sound_set_volume(&sound, info.volume);
ma_sound_start(&sound);
}
} else if (soundReady) {
ma_sound_stop(&sound);
}
}

bool content(tvg::Canvas* canvas, uint32_t w, uint32_t h) override
{
if (ma_engine_init(nullptr, &engine) != MA_SUCCESS) return false;
engineReady = true;

//background
auto* bg = tvg::Shape::gen();
bg->appendRect(0, 0, w, h);
bg->fill(30, 30, 35);
canvas->add(bg);

//lottie animation
lottie = unique_ptr<tvg::LottieAnimation>(tvg::LottieAnimation::gen());
auto* pic = lottie->picture();
if (!tvgexam::verify(pic->load(EXAMPLE_DIR"/lottie/emoji_audio.json"))) return false;

float pw, ph;
pic->size(&pw, &ph);
auto scale = (pw / ph > float(w) / h) ? float(w) / pw : float(h) / ph;
pic->scale(scale);
pic->translate((w - pw * scale) * 0.5f, (h - ph * scale) * 0.5f);
canvas->add(pic);

//register the audio resolver
lottie->audioResolver([this](const tvg::AudioInfo& info, void*) { onAudio(info); }, nullptr);

return true;
}

bool update(tvg::Canvas* canvas, uint32_t elapsed) override
{
auto progress = tvgexam::progress(elapsed, lottie->duration());

if (progress < prevProgress && soundReady) {
ma_sound_seek_to_pcm_frame(&sound, 0);
ma_sound_start(&sound);
}
prevProgress = progress;

lottie->frame(lottie->totalFrame() * progress);

canvas->update();
return true;
}
};


/************************************************************************/
/* Entry Point */
/************************************************************************/

int main(int argc, char **argv)
{
return tvgexam::main(new UserExample, argc, argv, false, 1024, 600, 0);
}
32 changes: 31 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,41 @@ lottie_example = '''
}
'''

audio_dep = declare_dependency()

if cc.links(lottie_example, dependencies : thorvg_dep, include_directories : thorvg_inc)
source_file += [
'Lottie.cpp',
'LottieAudio.cpp',
'LottieExpressions.cpp',
'LottieExtension.cpp',
'LottieInteraction.cpp',
'LottieBigSize.cpp',
'LottieTweening.cpp'
]

# LottieAudio backend (miniaudio)
audio_link_args = []
if host_machine.system() == 'darwin'
audio_link_args += ['-framework', 'CoreAudio', '-framework', 'CoreFoundation', '-framework', 'AudioToolbox']
elif host_machine.system() == 'linux'
audio_link_args += ['-ldl', '-lpthread', '-lm']
alsa_dep = dependency('alsa', required: false)
if alsa_dep.found()
audio_link_args += ['-lasound']
else
pulse_dep = dependency('libpulse', required: false)
if pulse_dep.found()
audio_link_args += ['-lpulse']
endif
endif
elif host_machine.system() == 'windows'
audio_link_args += ['-lole32', '-lwinmm']
endif
audio_dep = declare_dependency(
link_args : audio_link_args,
include_directories : include_directories('../vendor'))

message('ThorVG Lottie found. Enabled Lottie examples.')
else
message('ThorVG Lottie not found. Skipping Lottie examples.')
Expand All @@ -121,8 +147,12 @@ endif

foreach current_file : source_file
name = current_file.split('.')[0]
deps = examples_dep
if name == 'LottieAudio'
deps += audio_dep
endif
executable(name, current_file,
dependencies: examples_dep,
dependencies: deps,
cpp_args : compiler_flags)
endforeach

Expand Down
Loading
Loading