procedural audio buffer, add mods to synth

This commit is contained in:
2025-08-03 11:18:23 +02:00
parent 9095a6952a
commit 3d79d24eff
2 changed files with 44 additions and 90 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
#include "audio_buffer.h"
#include <array>
#include <cstddef>
#include <vector>
@@ -8,15 +9,15 @@ using namespace std;
namespace trnr {
enum m_event_type {
enum midi_event_type {
note_on = 0,
note_off,
pitch_wheel,
mod_wheel
};
struct m_event {
m_event_type type;
struct midi_event {
midi_event_type type;
int offset;
int midi_note;
float velocity;
@@ -26,35 +27,36 @@ struct m_event {
constexpr size_t MAX_VOICES = 8;
constexpr size_t MAX_EVENTS_PER_VOICE = 32;
template <typename voice, typename sample>
void voice_process_block(voice& v, sample** audio, int num_frames, const array<m_event, MAX_EVENTS_PER_VOICE>& events,
size_t num_events);
template <typename t_voice, typename t_sample>
void voice_process_block(t_voice& v, t_sample** frames, size_t num_frames, midi_event* events, size_t num_events,
const vector<audio_buffer<t_sample>>& mods = {});
template <typename voice>
template <typename t_voice>
struct synth {
array<voice, MAX_VOICES> voices;
array<array<m_event, MAX_EVENTS_PER_VOICE>, MAX_VOICES> voice_events;
array<t_voice, MAX_VOICES> voices;
array<array<midi_event, MAX_EVENTS_PER_VOICE>, MAX_VOICES> voice_events;
array<size_t, MAX_VOICES> counts;
int active_voice_count = 1;
size_t index_to_steal = 0;
};
template <typename voice>
void synth_init(synth<voice>& s, double samplerate)
template <typename t_voice>
void synth_init(synth<t_voice>& s, double samplerate)
{
for (size_t i = 0; i < MAX_VOICES; ++i) {
s.voices[i] = voice();
s.voices[i] = t_voice();
s.voices[i].voice_init(samplerate);
s.counts[i] = 0;
}
}
template <typename voice, typename sample>
void synth_process_block(synth<voice>& s, sample** audio, const vector<m_event>& midi_events, int num_frames)
template <typename t_voice, typename t_sample>
void synth_process_block(synth<t_voice>& s, t_sample** frames, int num_frames, const vector<midi_event>& midi_events,
const vector<audio_buffer<t_sample>>& mods = {})
{
// reset voice events and counts
for (int i = 0; i < MAX_VOICES; i++) {
s.voice_events[i].fill(m_event {});
s.voice_events[i].fill(midi_event {});
s.counts[i] = 0;
}
@@ -106,8 +108,9 @@ void synth_process_block(synth<voice>& s, sample** audio, const vector<m_event>&
for (size_t i = 0; i < s.active_voice_count; ++i) {
auto& v = s.voices[i];
auto& events = s.voice_events[i];
voice_process_block(v, audio, num_frames, events, s.counts[i]);
auto& events = s.voice_events[i].data();
size_t num_events = s.counts[i];
voice_process_block(v, frames, num_frames, events, num_events, mods);
}
}
} // namespace trnr