summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-01-28 10:03:13 +0100
committerskal <pascal.massimino@gmail.com>2026-01-28 10:03:13 +0100
commita52b8909496b71e4d60a85a11de8b7364521081e (patch)
tree3312db9ce24c97f7f52a43ca116bf62943e937d3 /src
parentc0c71c164483b00c084b53b098f91057ad65135f (diff)
feat(demo): Add drum sequence using embedded assets
Incorporates kick1.spec, snare1.spec, and hihat1.spec into the demo sequence. - Updated assets.txt with new drum identifiers. - Implemented register_spec_asset helper in main.cc to load spectral data. - Added 8th-note sequencer triggering a simple drum and bass pattern.
Diffstat (limited to 'src')
-rw-r--r--src/main.cc84
1 files changed, 62 insertions, 22 deletions
diff --git a/src/main.cc b/src/main.cc
index 6428ed2..f612868 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -13,10 +13,34 @@
#include <stdio.h>
#include <string.h>
-#define DEMO_BPM 120.0f
+#define DEMO_BPM 128.0f
#define SECONDS_PER_BEAT (60.0f / DEMO_BPM)
#define SPEC_FRAMES 16
+struct SpecHeader {
+ char magic[4];
+ int32_t version;
+ int32_t dct_size;
+ int32_t num_frames;
+};
+
+int register_spec_asset(AssetId id) {
+ size_t size;
+ const uint8_t *data = GetAsset(id, &size);
+ if (!data || size < sizeof(SpecHeader))
+ return -1;
+
+ const SpecHeader *header = (const SpecHeader *)data;
+ const float *spectral_data = (const float *)(data + sizeof(SpecHeader));
+
+ Spectrogram spec;
+ spec.spectral_data_a = spectral_data;
+ spec.spectral_data_b = spectral_data; // No double-buffer for static assets
+ spec.num_frames = header->num_frames;
+
+ return synth_register_spectrogram(&spec);
+}
+
static float g_spec_buffer_a[SPEC_FRAMES * DCT_SIZE];
static float g_spec_buffer_b[SPEC_FRAMES * DCT_SIZE];
@@ -53,17 +77,16 @@ int main(int argc, char **argv) {
gpu_init(platform_get_window());
audio_init();
- generate_tone(g_spec_buffer_a, 440.0f); // A4
- generate_tone(g_spec_buffer_b, 0.0f); // A5
-
- const Spectrogram spec = {g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES};
- int tone_id = synth_register_spectrogram(&spec);
+ // Register drum assets
+ int kick_id = register_spec_asset(AssetId::ASSET_KICK_1);
+ int snare_id = register_spec_asset(AssetId::ASSET_SNARE_1);
+ int hihat_id = register_spec_asset(AssetId::ASSET_HIHAT_1);
- // Dummy call to ensure asset system is linked
- size_t dummy_size;
- const uint8_t *dummy_asset = GetAsset(AssetId::ASSET_NULL_ASSET, &dummy_size);
- (void)dummy_asset;
- (void)dummy_size;
+ // Still keep the dynamic tone for bass
+ generate_tone(g_spec_buffer_a, 110.0f); // A2
+ generate_tone(g_spec_buffer_b, 110.0f);
+ const Spectrogram bass_spec = {g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES};
+ int bass_id = synth_register_spectrogram(&bass_spec);
double last_beat_time = 0.0;
int beat_count = 0;
@@ -72,21 +95,38 @@ int main(int argc, char **argv) {
platform_poll();
double current_time = platform_get_time();
- if (current_time - last_beat_time > SECONDS_PER_BEAT) {
- const float pan = (beat_count & 1) ? -.8 : .8;
- synth_trigger_voice(tone_id, 0.8f, pan);
+ if (current_time - last_beat_time > SECONDS_PER_BEAT / 2.0) { // 8th notes
last_beat_time = current_time;
- beat_count++;
- if (beat_count % 4 == 0) {
- // Time to update the sound!
- float *back_buffer = synth_begin_update(tone_id);
+ const int step = beat_count % 16;
+
+ // Kick on 1, 9, 11, 14...
+ if (step == 0 || step == 8 || step == 10 || step == 13) {
+ synth_trigger_voice(kick_id, 1.0f, 0.0f);
+ }
+
+ // Snare on 4, 12
+ if (step == 4 || step == 12) {
+ synth_trigger_voice(snare_id, 0.8f, 0.0f);
+ }
+
+ // Hihat on every offbeat
+ if (step % 2 == 1) {
+ synth_trigger_voice(hihat_id, 0.5f, 0.3f);
+ }
+
+ // Bass pattern
+ if (step % 4 == 0) {
+ float bass_freq = (step < 8) ? 110.0f : 82.41f; // A2 then E2
+ float *back_buffer = synth_begin_update(bass_id);
if (back_buffer) {
- generate_tone(back_buffer,
- (beat_count % 8) == 4 ? 220.0f : 480.f); // A3
- synth_commit_update(tone_id);
+ generate_tone(back_buffer, bass_freq);
+ synth_commit_update(bass_id);
}
+ synth_trigger_voice(bass_id, 0.6f, -0.2f);
}
+
+ beat_count++;
}
int width, height;
@@ -102,4 +142,4 @@ int main(int argc, char **argv) {
gpu_shutdown();
platform_shutdown();
return 0;
-} \ No newline at end of file
+}