summaryrefslogtreecommitdiff
path: root/tests/pke-test-audio.cpp
blob: 2c1ac971f1b6acbbaddc458565e2174d74508d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 <chrono>
#include <thread>
#include <threads.h>

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<float>(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<float>(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;
}