From 34863f5b702c0dbb00d8db5c00efd43d895fcd4c Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Fri, 4 Jul 2025 07:06:45 -0400 Subject: pke: audio: first-pass actually play an asset --- tests/pke-test-audio.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/pke-test-audio.h | 16 +++++++++ tests/pke-test.cpp | 1 + 3 files changed, 110 insertions(+) create mode 100644 tests/pke-test-audio.cpp create mode 100644 tests/pke-test-audio.h (limited to 'tests') diff --git a/tests/pke-test-audio.cpp b/tests/pke-test-audio.cpp new file mode 100644 index 0000000..2c1ac97 --- /dev/null +++ b/tests/pke-test-audio.cpp @@ -0,0 +1,93 @@ + +#include "./pke-test-audio.h" + +#include "game-settings.hpp" +#include "pk.h" + +#include "audio-types.hpp" +#include "audio.hpp" +#include "asset-manager.hpp" +#include "thread-pool.hpp" +#include "window.hpp" + +#include +#include +#include + +pk_membucket *bkt = nullptr; + +void pke_test_audio_spinup() { + // pk_funcinstr_init(); + pkeSettings.isSimulationPaused = true; + PkeThreads_Init(); + AM_Init(); + pke_audio_init(); + // pk_funcinstr_teardown(); +}; + +void pke_test_audio_teardown() { + // pk_funcinstr_init(); + pke_audio_teardown(); + AM_Teardown(); + PkeThreads_Teardown(); + bkt = nullptr; + // pk_funcinstr_teardown(); +}; + + +int pke_test_audio_001() { + uint32_t i; + try { + bkt = pk_mem_bucket_create("pke_test_serialization", PK_MEM_DEFAULT_BUCKET_SIZE, PK_MEMBUCKET_FLAG_NONE); + + UBO.model = glm::translate(glm::mat4(1.f), glm::vec3(0, 0, 0)); + + float *zip_bop_bytes = pk_new(48000, bkt); + float phase = 0.0f; + float phase_increment = 440.f / 48000.f; + for (i = 0; i < 48000; ++i) { + zip_bop_bytes[i] = 2.f * (phase - floor(phase + 0.5f)); + phase += phase_increment; + if (phase >= 1.f) phase -= 1.f; + } + const AssetKey ak_sawtooth {"sawthooth"}; + AssetHandle ah_sawtooth = AM_Register(ak_sawtooth, PKE_ASSET_TYPE_AUDIO, zip_bop_bytes, sizeof(float) * 48000, 64); + pk_delete(zip_bop_bytes, 48000, bkt); + + pke_audio_play(ah_sawtooth, pke_audio_source_music, glm::vec3(0), pke_audio_flag_none); + + while (pke_audio_mstr.playing_objects.next > 0) { + pke_audio_tick(0.001f); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + AM_Release(ah_sawtooth); + + } catch (const std::exception &ex) { + pk_mem_bucket_destroy(bkt); + throw; + } + pk_mem_bucket_destroy(bkt); + return 0; +} + +struct pke_test_group *pke_test_audio_get_group() { + static const uint64_t test_count = 1; + static struct pke_test tests[test_count] = { + { + .title = "test 001", + .func = pke_test_audio_001, + .expected_result = 0, + }, + }; + static struct pke_test_group group = {}; + group.title = "audio"; + group.group_setup = nullptr; + group.group_teardown = nullptr; + group.test_setup = pke_test_audio_spinup; + group.test_teardown = pke_test_audio_teardown; + group.n_tests = test_count; + group.tests = &tests[0]; + + return &group; +} diff --git a/tests/pke-test-audio.h b/tests/pke-test-audio.h new file mode 100644 index 0000000..851c695 --- /dev/null +++ b/tests/pke-test-audio.h @@ -0,0 +1,16 @@ +#ifndef PKE_PKE_TEST_AUDIO_H +#define PKE_PKE_TEST_AUDIO_H + +#include "pke-test-types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct pke_test_group *pke_test_audio_get_group(); + +#ifdef __cplusplus +} +#endif + +#endif /* PKE_PKE_TEST_AUDIO_H */ diff --git a/tests/pke-test.cpp b/tests/pke-test.cpp index 473327e..d695aa8 100644 --- a/tests/pke-test.cpp +++ b/tests/pke-test.cpp @@ -1,6 +1,7 @@ #include "./pke-test-types.h" +#include "./pke-test-audio.h" #include "./pke-test-asset-manager.h" #include "./pke-test-dummy.h" #include "./pke-test-serialization.h" -- cgit v1.2.3