summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-01-31 00:43:16 +0100
committerskal <pascal.massimino@gmail.com>2026-01-31 00:43:43 +0100
commit8e199322ea4cb51d81c29d84120e4b142f7241b5 (patch)
tree905e387f1c346ebcf2a8fb03f07e7789314d9ed9 /src/main.cc
parent0f757ca901dbf00a953a39bd2d452ff423a45969 (diff)
add notes
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main.cc b/src/main.cc
index 5da76ba..0bc28d6 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -4,6 +4,7 @@
#include "assets.h" // Include generated asset header
#include "audio/audio.h"
+#include "audio/gen.h"
#include "audio/synth.h"
#include "gpu/gpu.h"
#include "platform.h"
@@ -12,6 +13,7 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
+#include <vector>
#define DEMO_BPM 128.0f
#define SECONDS_PER_BEAT (60.0f / DEMO_BPM)
@@ -44,6 +46,65 @@ int register_spec_asset(AssetId id) {
static float g_spec_buffer_a[SPEC_FRAMES * DCT_SIZE];
static float g_spec_buffer_b[SPEC_FRAMES * DCT_SIZE];
+// Global storage for the melody to ensure it persists
+std::vector<float> g_melody_data;
+
+int generate_melody() {
+ g_melody_data.clear();
+ int melody_frames = 0;
+
+ // Simple C Minor pentatonic-ish sequence
+ float notes[] = {261.63f, 311.13f, 349.23f, 392.00f, 466.16f, 523.25f};
+ int num_notes = 6;
+
+ // 128 beats at 128 BPM = 60 seconds
+ // Generate a random sequence
+ srand(12345); // Fixed seed for reproducibility
+
+ for (int i = 0; i < 128; ++i) {
+ if (i % 4 == 0) continue; // Rest on beat 1 of every bar
+
+ NoteParams params = {};
+ params.base_freq = notes[rand() % num_notes];
+ if (rand() % 4 == 0) params.base_freq *= 2.0f; // Occasional octave up
+
+ params.duration_sec = (rand() % 2 == 0) ? 0.2f : 0.4f;
+ params.amplitude = 0.4f;
+ params.attack_sec = 0.05f;
+ params.decay_sec = 0.1f;
+ params.vibrato_rate = 6.0f;
+ params.vibrato_depth = 1.5f;
+ params.num_harmonics = 4;
+ params.harmonic_decay = 0.6f;
+ params.pitch_randomness = 0.5f;
+ params.amp_randomness = 0.05f;
+
+ int note_frames = 0;
+ std::vector<float> note_data = generate_note_spectrogram(params, &note_frames);
+
+ // Apply some post-processing for texture
+ apply_spectral_noise(note_data, note_frames, 0.2f); // Add grit
+ if (i % 2 == 0) {
+ apply_spectral_comb(note_data, note_frames, 10.0f, 0.8f); // Phaser-like effect
+ }
+
+ // Calculate offset in frames
+ // i is the beat index (quarter notes)
+ // 1 beat = 60 / 128 seconds = 0.46875 sec
+ float beat_time = i * SECONDS_PER_BEAT;
+ int frame_offset = (int)(beat_time * 32000.0f / DCT_SIZE);
+
+ paste_spectrogram(g_melody_data, &melody_frames, note_data, note_frames, frame_offset);
+ }
+
+ Spectrogram spec;
+ spec.spectral_data_a = g_melody_data.data();
+ spec.spectral_data_b = g_melody_data.data();
+ spec.num_frames = melody_frames;
+
+ return synth_register_spectrogram(&spec);
+}
+
void generate_tone(float *buffer, float freq) {
memset(buffer, 0, SPEC_FRAMES * DCT_SIZE * sizeof(float));
for (int frame = 0; frame < SPEC_FRAMES; ++frame) {
@@ -88,6 +149,10 @@ int main(int argc, char **argv) {
const Spectrogram bass_spec = {g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES};
int bass_id = synth_register_spectrogram(&bass_spec);
+ // Generate and play melody
+ int melody_id = generate_melody();
+ synth_trigger_voice(melody_id, 0.6f, 0.0f);
+
double last_beat_time = 0.0;
int beat_count = 0;