summaryrefslogtreecommitdiff
path: root/src/pke-at-common.cpp
blob: a2a290fa4f6926f7ef611e9ff88ff2bd952e7af7 (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

#include "pke-at-common.hpp"

#include "pke-at-settings.hpp"

#include "pke/audio-types.hpp"

void pke_at_bpm_reset(uint8_t bpm) {
	g_at.bpm.last = bpm;
	g_at.bpm.target = bpm;
	g_at.bpm.current = bpm;
	g_at.bpm.lerp_delta = 0.f;
	g_at.bpm.lerp_delta_duration = 0.f;
	g_at.bpm.delta_per_beat = 60.f / (double)bpm;
	g_at.bpm.delta_since_last_beat = 0.f;
	g_at.mtrnm.beat = 0;
	g_at.mtrnm.beats_per_bar = 4;
}

AssetHandle pke_at_audio_get_or_generate_sawtooth(double pitch_freq, double duration) {
	uint32_t u;
	uint32_t len;
	float *bytes;
	float phase;
	float phase_increment;
	pke_asset_details details{};
	AssetHandle handle;
	AssetKey key;

	snprintf(key, AssetKeyLength, "saw%.5u;%.2f", (uint32_t)pitch_freq, duration);
	fprintf(stdout, "sawtooth: %s%c\n", key, '\0');

	handle = AM_GetHandle(key);
	if (handle != AssetHandle_MAX) {
		return handle;
	}

	len = std::ceil(PKE_AUDIO_BITRATE * duration);
	bytes = pk_new_arr<float>(len, g_at.mem.bkt_transient);
	if (bytes == nullptr) {
		throw "[pke_at_audio_get_or_generate_sawtooth] failed to alloc";
	}
	phase = 0.0f;
	phase_increment = pitch_freq / float(PKE_AUDIO_BITRATE);
	for (u = 0; u < len; ++u) {
		bytes[u] = 2.f * (phase - floor(phase + 0.5f));
		phase += phase_increment;
		if (phase >= 1.f) phase -= 1.f;
	}

	// TODO
	// details.audio.
	handle = AM_Register(key, PKE_ASSET_TYPE_AUDIO, bytes, len, 64, &details);
	return handle;
}