use crate::plugin::{Plugin, String, Vec};
use crate::synth::{fm_sine, midi_to_hz, sine};
use crate::{
plugin::{overlay_samples, SampleStepConfig},
synth::{envelope, noise_burst, square},
DrumPattern, DrumStep,
};
use crate::{BassNote, MelodyNote, StemSample};
pub const RAP_BPM_BASE: i32 = 92;
pub const RAP_BPM_VARIATION: i32 = 16;
pub const RAP_KICK_PATTERNS: [[u8; 16]; 3] = [
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0], ];
pub const RAP_SNARE_PATTERNS: [[u8; 16]; 3] = [
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0], ];
pub const RAP_HAT_PATTERNS: [[u8; 16]; 3] = [
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], [1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], ];
pub const RAP_CHORD_PROGRESSIONS: [[usize; 4]; 4] = [
[0, 2, 0, 3], [0, 3, 2, 1], [0, 1, 2, 1], [0, 2, 3, 0], ];
#[derive(Debug, Clone)]
pub struct RapPlugin {}
impl Plugin for RapPlugin {
fn mode(&self) -> &'static str {
"rap"
}
fn mode_string(&self) -> String {
String::from("rap")
}
fn chord_progression_from_seed(&self, seed: u32) -> Vec<usize> {
let idx = (seed as usize) % RAP_CHORD_PROGRESSIONS.len();
RAP_CHORD_PROGRESSIONS[idx].to_vec()
}
fn tempo_from_seed(&self, seed: u32) -> i32 {
let variation = ((seed >> 8) % RAP_BPM_VARIATION as u32) as i32 - RAP_BPM_VARIATION / 2;
RAP_BPM_BASE + variation }
fn drum_pattern_from_seed(&self, seed: &[u8; 8]) -> DrumPattern {
let kick = RAP_KICK_PATTERNS[(seed[0] % 3) as usize];
let snare = RAP_SNARE_PATTERNS[(seed[1] % 3) as usize];
let hat = RAP_HAT_PATTERNS[(seed[2] % 3) as usize];
let mut steps = [DrumStep::default(); 16];
for offset in 0..16 {
steps[offset] = DrumStep {
kick: kick[offset] == 1,
snare: snare[offset] == 1,
hat: hat[offset] == 1,
open_hat: false,
offset,
};
}
if seed[5] & 0x01 != 0 {
steps[8].hat = true;
steps[8].open_hat = true;
}
DrumPattern { steps }
}
fn sample_melody_note(&self, note: MelodyNote, sample_rate: u32) -> Vec<StemSample> {
let sr = sample_rate as f64;
let total = (sr * note.duration).floor() as usize;
if total == 0 {
return Vec::new();
}
let dur = total as f64 / sr;
let midi = (note.midi - 12.0).max(21.0);
let harmony_midi = (note.harmony_midi - 12.0).max(21.0);
let mel = fm_sine(sr, midi_to_hz(midi), dur, 0.28, 1.5, 1.0);
let mel = envelope(sr, &mel, 0.004, 0.18, 0.45, 0.08);
let harm = fm_sine(sr, midi_to_hz(harmony_midi), dur, 0.12, 1.5, 1.0);
let harm = envelope(sr, &harm, 0.004, 0.18, 0.45, 0.08);
(0..total)
.map(|i| StemSample {
value: mel.get(i).copied().unwrap_or(0.0) + harm.get(i).copied().unwrap_or(0.0),
byte_index: note.byte_index,
})
.collect()
}
fn sample_bass_note(&self, note: BassNote, sample_rate: u32) -> Vec<StemSample> {
let sr = sample_rate as f64;
let total = (sr * note.duration).floor() as usize;
if total == 0 {
return Vec::new();
}
let dur = total as f64 / sr;
let raw = sine(sr, midi_to_hz(note.midi), dur, 0.45);
let with_env = envelope(sr, &raw, 0.005, 0.07, 0.60, 0.06);
(0..total.min(with_env.len()))
.map(|i| StemSample {
value: with_env[i],
byte_index: note.byte_index,
})
.collect()
}
fn sample_step(&self, step: &DrumStep, config: SampleStepConfig, samples: &mut Vec<f32>) {
if step.kick {
let freq = 72.0 + f64::from(config.color % 15);
let dur_sec = (0.13_f64).min(config.step_duration * 2.0);
let thud = square(config.sample_rate, freq, dur_sec, 0.50, 0.25);
overlay_samples(
&envelope(config.sample_rate, &thud, 0.001, 0.08, 0.0, 0.02),
samples,
);
let sub = sine(config.sample_rate, 52.0, dur_sec, 0.25);
overlay_samples(
&envelope(config.sample_rate, &sub, 0.001, 0.10, 0.0, 0.02),
samples,
);
}
if step.snare {
let accent = config.pattern == 4 || config.pattern == 12;
let amp = if accent { 0.30 } else { 0.12 };
let dur_n = (0.08_f64).min(config.step_duration);
let noise = noise_burst(config.sample_rate, config.random, dur_n, amp);
let tone = sine(config.sample_rate, 220.0, dur_n, amp * 0.50);
let mixed: alloc::vec::Vec<f32> = noise
.iter()
.zip(tone.iter())
.map(|(&n, &t)| n + t)
.collect();
overlay_samples(
&envelope(config.sample_rate, &mixed, 0.001, 0.05, 0.0, 0.025),
samples,
);
}
if step.hat {
overlay_samples(
&if step.open_hat {
let d = (0.08_f64).min(config.step_duration * 2.0);
let s = noise_burst(config.sample_rate, config.random, d, 0.14);
envelope(config.sample_rate, &s, 0.001, 0.05, 0.18, 0.03)
} else {
let amp = if config.pattern % 2 == 0 { 0.12 } else { 0.07 };
let d = (0.018_f64).min(config.step_duration * 0.42);
noise_burst(config.sample_rate, config.random, d, amp)
},
samples,
);
}
}
}