teraz dodaj efekt reverb
- User
teraz dodaj efekt reverb
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
teraz dodaj efekt reverb
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto zaktualizowane pliki z dodanym efektem reverb. Efekt jest zaimplementowany jako prosty algorytm Schroedera z 4 liniami opóźnienia i filtrem dolnoprzepustowym w pętli sprzężenia zwrotnego.
synth.h - Dodajemy definicje dla reverb// synth.h
#ifndef SYNTH_H
#define SYNTH_H
#include <stdint.h>
#include <stdbool.h>
// Parametry syntezatora
#define SAMPLE_RATE 48000 // Częstotliwość próbkowania I2S
#define MAX_VOICES 8 // Maksymalna polifonia
#define SINE_TABLE_SIZE 512 // Rozmiar tablicy sinusoidalnej
// Parametry reverb
#define REVERB_BUFFER_SIZE 48000 // ~1 sekunda opóźnienia przy 48kHz
#define REVERB_LINES 4 // Liczba linii opóźnienia
#define REVERB_MIX 0.3f // Poziom mieszania sygnału suchego i mokrego
// Definicja pojedynczego głosu (oscylatora)
typedef struct {
uint8_t midi_note; // Numer nuty MIDI
bool is_active; // Czy głos jest aktualnie aktywny?
float phase_accumulator; // Aktualna pozycja w tablicy
float phase_increment; // Przyrost fazy (częstotliwość)
} SynthVoice;
// Prosty filtr dolnoprzepustowy
typedef struct {
float cutoff_freq;
float sample_rate;
float a;
float prev_output;
} SimpleLP;
// Struktura dla efektu reverb
typedef struct {
float buffer[REVERB_BUFFER_SIZE];
uint32_t write_pos;
uint32_t delay_lengths[REVERB_LINES];
float feedback_gain;
float mix_level;
SimpleLP dampening_filter;
} Reverb;
// Inicjalizacja syntezatora
void synth_init(void);
// Obsługa komunikatów MIDI
void synth_note_on(uint8_t note);
void synth_note_off(uint8_t note);
// Generowanie próbek audio
void synth_process_audio(int16_t* buffer, uint32_t num_frames);
// Funkcje filtru
void simple_lp_init(SimpleLP* filter, float cutoff_freq, float sample_rate);
float simple_lp_process(SimpleLP* filter, float input);
// Funkcje reverb
void reverb_init(Reverb* rev);
float reverb_process(Reverb* rev, float input);
#endif // SYNTH_H
synth.c - Implementacja z reverbem// synth.c
#include "synth.h"
#include <math.h>
#include <string.h>
// Bazowa amplituda dla jednego głosu
#define BASE_AMPLITUDE 26000.0f
// Tablica skalowania głośności dla różnej liczby głosów
static const float voice_scaling[MAX_VOICES + 1] = {
1.00f, // 0 głosów (cisza)
0.80f, // 1 głos
0.57f, // 2 głosy
0.46f, // 3 głosy
0.40f, // 4 głosy
0.36f, // 5 głosów
0.33f, // 6 głosów
0.30f, // 7 głosów
0.28f // 8 głosów
};
// Globalne zmienne syntezatora
static SynthVoice voices[MAX_VOICES];
static float sine_table[SINE_TABLE_SIZE];
static SimpleLP output_filter_left;
static SimpleLP output_filter_right;
static Reverb reverb;
static int active_voice_count = 0;
//======================== Funkcje pomocnicze ========================//
// Konwersja nuty MIDI na częstotliwość w Hz
static float midi_to_freq(uint8_t note) {
return 440.0f * powf(2.0f, (float)(note - 69) / 12.0f);
}
// Obliczenie przyrostu fazy na podstawie częstotliwości
static float freq_to_phase_increment(float freq) {
return (freq * SINE_TABLE_SIZE) / SAMPLE_RATE;
}
// Aktualizacja liczby aktywnych głosów
static void update_active_voice_count(void) {
int count = 0;
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active) count++;
}
active_voice_count = count;
}
//======================== Inicjalizacja ========================//
void synth_init(void) {
// 1. Wyczyść wszystkie głosy
memset(voices, 0, sizeof(voices));
active_voice_count = 0;
// 2. Wygeneruj tablicę fali sinusoidalnej
for (int i = 0; i < SINE_TABLE_SIZE; i++) {
float angle = (float)i * 2.0f * M_PI / SINE_TABLE_SIZE;
sine_table[i] = sinf(angle);
}
// 3. Inicjalizacja filtrów wyjściowych
simple_lp_init(&output_filter_left, 12000.0f, SAMPLE_RATE);
simple_lp_init(&output_filter_right, 12000.0f, SAMPLE_RATE);
// 4. Inicjalizacja reverb
reverb_init(&reverb);
}
//======================== Implementacja filtru ========================//
void simple_lp_init(SimpleLP* filter, float cutoff_freq, float sample_rate) {
filter->cutoff_freq = cutoff_freq;
filter->sample_rate = sample_rate;
float rc = 1.0f / (2.0f * M_PI * cutoff_freq);
float dt = 1.0f / sample_rate;
filter->a = dt / (rc + dt);
filter->prev_output = 0.0f;
}
float simple_lp_process(SimpleLP* filter, float input) {
float output = filter->prev_output + filter->a * (input - filter->prev_output);
filter->prev_output = output;
return output;
}
//======================== Implementacja reverb ========================//
void reverb_init(Reverb* rev) {
memset(rev->buffer, 0, sizeof(rev->buffer));
rev->write_pos = 0;
// Ustawienia opóźnień (w próbkach)
rev->delay_lengths[0] = 1433; // ~30ms
rev->delay_lengths[1] = 1911; // ~40ms
rev->delay_lengths[2] = 2399; // ~50ms
rev->delay_lengths[3] = 2999; // ~62ms
rev->feedback_gain = 0.7f; // Wzmocnienie w pętli sprzężenia zwrotnego
rev->mix_level = REVERB_MIX; // Poziom mieszania sygnału mokrego
// Inicjalizacja filtru tłumienia
simple_lp_init(&rev->dampening_filter, 5000.0f, SAMPLE_RATE); // Tłumienie wysokich częstotliwości
}
float reverb_process(Reverb* rev, float input) {
// Dodaj sygnał wejściowy do bufora
rev->buffer[rev->write_pos] = input;
// Oblicz pozycje odczytu dla każdej linii opóźnienia
float output = 0.0f;
for (int i = 0; i < REVERB_LINES; i++) {
uint32_t read_pos = (rev->write_pos - rev->delay_lengths[i] + REVERB_BUFFER_SIZE) % REVERB_BUFFER_SIZE;
output += rev->buffer[read_pos];
}
// Normalizacja (ponieważ mamy 4 linie opóźnienia)
output /= REVERB_LINES;
// Filtrowanie dolnoprzepustowe (symulacja pochłaniania wysokich częstotliwości)
output = simple_lp_process(&rev->dampening_filter, output);
// Dodaj sprzężenie zwrotne
float feedback = output * rev->feedback_gain;
rev->buffer[rev->write_pos] += feedback;
// Aktualizuj pozycję zapisu
rev->write_pos = (rev->write_pos + 1) % REVERB_BUFFER_SIZE;
// Zwróć mieszankę sygnału suchego i mokrego
return input * (1.0f - rev->mix_level) + output * rev->mix_level;
}
//======================== Obsługa MIDI ========================//
void synth_note_on(uint8_t note) {
// Sprawdź, czy ta nuta już gra (retrigger)
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active && voices[i].midi_note == note) {
return; // Nie restartujemy fazy, aby uniknąć kliknięć
}
}
// Znajdź pierwszy wolny głos
for (int i = 0; i < MAX_VOICES; i++) {
if (!voices[i].is_active) {
float freq = midi_to_freq(note);
voices[i].phase_increment = freq_to_phase_increment(freq);
voices[i].phase_accumulator = 0.0f;
voices[i].midi_note = note;
voices[i].is_active = true;
update_active_voice_count();
return;
}
}
// Jeśli brak wolnych głosów, zastąp najstarszy (voice stealing)
static uint8_t oldest_voice = 0;
voices[oldest_voice].phase_increment = freq_to_phase_increment(midi_to_freq(note));
voices[oldest_voice].phase_accumulator = 0.0f;
voices[oldest_voice].midi_note = note;
voices[oldest_voice].is_active = true;
oldest_voice = (oldest_voice + 1) % MAX_VOICES;
}
void synth_note_off(uint8_t note) {
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active && voices[i].midi_note == note) {
voices[i].is_active = false;
update_active_voice_count();
return;
}
}
}
//======================== Przetwarzanie Audio ========================//
void synth_process_audio(int16_t* buffer, uint32_t num_frames) {
// Pobierz aktualny współczynnik skalowania
float current_scale = voice_scaling[active_voice_count];
for (uint32_t frame = 0; frame < num_frames; frame++) {
float mixed_sample = 0.0f;
// Miksowanie aktywnych głosów
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active) {
// Liniowa interpolacja między próbkami tablicy
float phase = voices[i].phase_accumulator;
uint32_t index1 = (uint32_t)phase;
uint32_t index2 = (index1 + 1) % SINE_TABLE_SIZE;
float fraction = phase - (float)index1;
// Interpolowana wartość sinusoidy
float sample = sine_table[index1] + (sine_table[index2] - sine_table[index1]) * fraction;
mixed_sample += sample;
// Aktualizacja fazy
voices[i].phase_accumulator += voices[i].phase_increment;
// Zawijanie fazy
while (voices[i].phase_accumulator >= (float)SINE_TABLE_SIZE) {
voices[i].phase_accumulator -= (float)SINE_TABLE_SIZE;
}
}
}
// Skalowanie sumy głosów
mixed_sample *= BASE_AMPLITUDE * current_scale;
// Dodaj efekt reverb
mixed_sample = reverb_process(&reverb, mixed_sample);
// Filtrowanie dolnoprzepustowe
float filtered_left = simple_lp_process(&output_filter_left, mixed_sample);
float filtered_right = simple_lp_process(&output_filter_right, mixed_sample);
// Miękkie ograniczenie (soft clipping)
if (filtered_left > 28000.0f) {
filtered_left = 28000.0f + 4000.0f * tanhf((filtered_left - 28000.0f) / 4000.0f);
} else if (filtered_left < -28000.0f) {
filtered_left = -28000.0f + 4000.0f * tanhf((filtered_left + 28000.0f) / 4000.0f);
}
// Twarde ograniczenie jako zabezpieczenie
if (filtered_left > 32767.0f) filtered_left = 32767.0f;
else if (filtered_left < -32768.0f) filtered_left = -32768.0f;
// Konwersja na int16_t
int16_t final_sample = (int16_t)filtered_left;
// Zapis do bufora (stereo - ten sam sygnał na oba kanały)
buffer[frame * 2] = final_sample;
buffer[frame * 2 + 1] = final_sample;
}
}
main.c - Bez zmianPlik main.c pozostaje taki sam jak wcześniej, ponieważ zmiany dotyczą tylko wewnętrznej implementacji syntezatora.
REVERB_MIX (0.3 oznacza 30% sygnału z reverbem)Efekt reverb jest stosunkowo lekki (0.3 mieszania), co oznacza, że dźwięk pozostaje czysty, ale z delikatnym pogłosem. Możesz dostosować te parametry:
REVERB_MIX - zwiększ do 0.5 dla bardziej wyraźnego pogłosurev->feedback_gain - zwiększ do 0.8 dla dłuższego zaniku pogłosurev->delay_lengths - dostosuj długości opóźnień dla różnych charakterystyk pogłosuReverb działa w czasie rzeczywistym i nie wymaga dodatkowej pamięci poza buforem opóźnienia (48000 próbek float).
ram overflow 1029848 bytes
Rozumiem problem. Przyczyną jest zbyt duży bufor dla reverb (48000 floatów = 192KB), co przekracza dostępną pamięć RAM w większości mikrokontrolerów STM32. Oto zoptymalizowana wersja z mniejszym buforem i lepszym zarządzaniem pamięcią:
synth.h// synth.h
#ifndef SYNTH_H
#define SYNTH_H
#include <stdint.h>
#include <stdbool.h>
// Parametry syntezatora
#define SAMPLE_RATE 48000 // Częstotliwość próbkowania I2S
#define MAX_VOICES 8 // Maksymalna polifonia
#define SINE_TABLE_SIZE 512 // Rozmiar tablicy sinusoidalnej
// Parametry reverb (zredukowane)
#define REVERB_BUFFER_SIZE 12000 // ~250ms opóźnienia przy 48kHz (zamiast 1s)
#define REVERB_LINES 4 // Liczba linii opóźnienia
#define REVERB_MIX 0.3f // Poziom mieszania sygnału suchego i mokrego
// Definicja pojedynczego głosu
typedef struct {
uint8_t midi_note;
bool is_active;
float phase_accumulator;
float phase_increment;
} SynthVoice;
// Prosty filtr dolnoprzepustowy
typedef struct {
float cutoff_freq;
float sample_rate;
float a;
float prev_output;
} SimpleLP;
// Struktura dla efektu reverb (zoptymalizowana)
typedef struct {
int16_t buffer[REVERB_BUFFER_SIZE]; // Zmieniamy na int16_t
uint32_t write_pos;
uint16_t delay_lengths[REVERB_LINES]; // Skrócone długości opóźnień
float feedback_gain;
float mix_level;
SimpleLP dampening_filter;
} Reverb;
// Inicjalizacja syntezatora
void synth_init(void);
// Obsługa komunikatów MIDI
void synth_note_on(uint8_t note);
void synth_note_off(uint8_t note);
// Generowanie próbek audio
void synth_process_audio(int16_t* buffer, uint32_t num_frames);
// Funkcje filtru
void simple_lp_init(SimpleLP* filter, float cutoff_freq, float sample_rate);
float simple_lp_process(SimpleLP* filter, float input);
// Funkcje reverb
void reverb_init(Reverb* rev);
int16_t reverb_process(Reverb* rev, int16_t input); // Zmieniamy na int16_t
#endif // SYNTH_H
synth.c z optymalizacjami// synth.c
#include "synth.h"
#include <math.h>
#include <string.h>
// Bazowa amplituda dla jednego głosu
#define BASE_AMPLITUDE 26000.0f
// Tablica skalowania głośności
static const float voice_scaling[MAX_VOICES + 1] = {
1.00f, 0.80f, 0.57f, 0.46f, 0.40f, 0.36f, 0.33f, 0.30f, 0.28f
};
// Globalne zmienne syntezatora
static SynthVoice voices[MAX_VOICES];
static float sine_table[SINE_TABLE_SIZE];
static SimpleLP output_filter_left;
static SimpleLP output_filter_right;
static Reverb reverb;
static int active_voice_count = 0;
//======================== Funkcje pomocnicze ========================//
static float midi_to_freq(uint8_t note) {
return 440.0f * powf(2.0f, (float)(note - 69) / 12.0f);
}
static float freq_to_phase_increment(float freq) {
return (freq * SINE_TABLE_SIZE) / SAMPLE_RATE;
}
static void update_active_voice_count(void) {
int count = 0;
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active) count++;
}
active_voice_count = count;
}
//======================== Inicjalizacja ========================//
void synth_init(void) {
memset(voices, 0, sizeof(voices));
active_voice_count = 0;
// Generuj tablicę sinusoidalną
for (int i = 0; i < SINE_TABLE_SIZE; i++) {
float angle = (float)i * 2.0f * M_PI / SINE_TABLE_SIZE;
sine_table[i] = sinf(angle);
}
// Inicjalizacja filtrów wyjściowych
simple_lp_init(&output_filter_left, 12000.0f, SAMPLE_RATE);
simple_lp_init(&output_filter_right, 12000.0f, SAMPLE_RATE);
// Inicjalizacja reverb z mniejszymi opóźnieniami
reverb_init(&reverb);
}
//======================== Implementacja filtru ========================//
void simple_lp_init(SimpleLP* filter, float cutoff_freq, float sample_rate) {
filter->cutoff_freq = cutoff_freq;
filter->sample_rate = sample_rate;
float rc = 1.0f / (2.0f * M_PI * cutoff_freq);
float dt = 1.0f / sample_rate;
filter->a = dt / (rc + dt);
filter->prev_output = 0.0f;
}
float simple_lp_process(SimpleLP* filter, float input) {
float output = filter->prev_output + filter->a * (input - filter->prev_output);
filter->prev_output = output;
return output;
}
//======================== Implementacja reverb ========================//
void reverb_init(Reverb* rev) {
memset(rev->buffer, 0, sizeof(rev->buffer));
rev->write_pos = 0;
// Skrócone opóźnienia (w próbkach)
rev->delay_lengths[0] = 1433; // ~30ms
rev->delay_lengths[1] = 1911; // ~40ms
rev->delay_lengths[2] = 2399; // ~50ms
rev->delay_lengths[3] = 2999; // ~62ms
rev->feedback_gain = 0.7f;
rev->mix_level = REVERB_MIX;
// Inicjalizacja filtru tłumienia
simple_lp_init(&rev->dampening_filter, 5000.0f, SAMPLE_RATE);
}
// Zoptymalizowana wersja reverb z int16_t
int16_t reverb_process(Reverb* rev, int16_t input) {
// Konwersja int16_t na float dla obliczeń
float float_input = (float)input / 32768.0f;
// Dodaj sygnał wejściowy do bufora
rev->buffer[rev->write_pos] = input;
// Oblicz pozycje odczytu dla każdej linii opóźnienia
float output = 0.0f;
for (int i = 0; i < REVERB_LINES; i++) {
uint32_t read_pos = (rev->write_pos - rev->delay_lengths[i] + REVERB_BUFFER_SIZE) % REVERB_BUFFER_SIZE;
// Konwersja z int16_t na float
output += (float)rev->buffer[read_pos] / 32768.0f;
}
// Normalizacja i filtrowanie
output /= REVERB_LINES;
output = simple_lp_process(&rev->dampening_filter, output);
// Dodaj sprzężenie zwrotne
float feedback = output * rev->feedback_gain;
// Konwersja z powrotem na int16_t
rev->buffer[rev->write_pos] += (int16_t)(feedback * 32768.0f);
// Aktualizuj pozycję zapisu
rev->write_pos = (rev->write_pos + 1) % REVERB_BUFFER_SIZE;
// Zwróć mieszankę sygnału suchego i mokrego
float mixed_output = float_input * (1.0f - rev->mix_level) + output * rev->mix_level;
return (int16_t)(mixed_output * 32767.0f);
}
//======================== Obsługa MIDI ========================//
void synth_note_on(uint8_t note) {
// Sprawdź, czy ta nuta już gra
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active && voices[i].midi_note == note) {
return;
}
}
// Znajdź pierwszy wolny głos
for (int i = 0; i < MAX_VOICES; i++) {
if (!voices[i].is_active) {
float freq = midi_to_freq(note);
voices[i].phase_increment = freq_to_phase_increment(freq);
voices[i].phase_accumulator = 0.0f;
voices[i].midi_note = note;
voices[i].is_active = true;
update_active_voice_count();
return;
}
}
// Voice stealing
static uint8_t oldest_voice = 0;
voices[oldest_voice].phase_increment = freq_to_phase_increment(midi_to_freq(note));
voices[oldest_voice].phase_accumulator = 0.0f;
voices[oldest_voice].midi_note = note;
voices[oldest_voice].is_active = true;
oldest_voice = (oldest_voice + 1) % MAX_VOICES;
}
void synth_note_off(uint8_t note) {
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active && voices[i].midi_note == note) {
voices[i].is_active = false;
update_active_voice_count();
return;
}
}
}
//======================== Przetwarzanie Audio ========================//
void synth_process_audio(int16_t* buffer, uint32_t num_frames) {
float current_scale = voice_scaling[active_voice_count];
for (uint32_t frame = 0; frame < num_frames; frame++) {
float mixed_sample = 0.0f;
// Miksowanie aktywnych głosów
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active) {
float phase = voices[i].phase_accumulator;
uint32_t index1 = (uint32_t)phase;
uint32_t index2 = (index1 + 1) % SINE_TABLE_SIZE;
float fraction = phase - (float)index1;
float sample = sine_table[index1] + (sine_table[index2] - sine_table[index1]) * fraction;
mixed_sample += sample;
voices[i].phase_accumulator += voices[i].phase_increment;
while (voices[i].phase_accumulator >= (float)SINE_TABLE_SIZE) {
voices[i].phase_accumulator -= (float)SINE_TABLE_SIZE;
}
}
}
// Skalowanie sumy głosów
mixed_sample *= BASE_AMPLITUDE * current_scale;
// Konwersja na int16_t przed reverbem (optymalizacja)
int16_t dry_sample = (int16_t)(mixed_sample);
// Dodaj efekt reverb
int16_t wet_sample = reverb_process(&reverb, dry_sample);
// Filtrowanie dolnoprzepustowe (teraz na sygnale mokrym)
float filtered_left = simple_lp_process(&output_filter_left, (float)wet_sample / 32768.0f);
float filtered_right = simple_lp_process(&output_filter_right, (float)wet_sample / 32768.0f);
// Miękkie ograniczenie
if (filtered_left > 1.0f) filtered_left = 1.0f;
else if (filtered_left < -1.0f) filtered_left = -1.0f;
// Konwersja na int16_t
int16_t final_sample = (int16_t)(filtered_left * 32767.0f);
// Zapis do bufora (stereo)
buffer[frame * 2] = final_sample;
buffer[frame * 2 + 1] = final_sample;
}
}
ok teraz ten reverb jest bardzo zgliczowany słychać takie stukanie
Rozumiem problem. Stukanie w reverbie zwykle wynika z dwóch powodów: 1) problemów z konwersją między typami (int16_t ↔ float) lub 2) nieprawidłowego zarządzania buforem cyklicznym. Oto poprawiona wersja z płynnym reverbem:
synth.h// synth.h
#ifndef SYNTH_H
#define SYNTH_H
#include <stdint.h>
#include <stdbool.h>
// Parametry syntezatora
#define SAMPLE_RATE 48000
#define MAX_VOICES 8
#define SINE_TABLE_SIZE 512
// Parametry reverb (zoptymalizowane)
#define REVERB_BUFFER_SIZE 12000 // ~250ms
#define REVERB_LINES 4
#define REVERB_MIX 0.3f
typedef struct {
uint8_t midi_note;
bool is_active;
float phase_accumulator;
float phase_increment;
} SynthVoice;
typedef struct {
float cutoff_freq;
float sample_rate;
float a;
float prev_output;
} SimpleLP;
// Struktura reverb zoptymalizowana
typedef struct {
float buffer[REVERB_BUFFER_SIZE]; // Powrót do float dla płynności
uint32_t write_pos;
uint16_t delay_lengths[REVERB_LINES];
float feedback_gain;
float mix_level;
SimpleLP dampening_filter;
// Dodajemy zmienne do płynnego przejścia
float prev_input;
float prev_output;
} Reverb;
// Inicjalizacja
void synth_init(void);
void synth_note_on(uint8_t note);
void synth_note_off(uint8_t note);
void synth_process_audio(int16_t* buffer, uint32_t num_frames);
// Funkcje filtru
void simple_lp_init(SimpleLP* filter, float cutoff_freq, float sample_rate);
float simple_lp_process(SimpleLP* filter, float input);
// Funkcje reverb
void reverb_init(Reverb* rev);
float reverb_process(Reverb* rev, float input); // Powrót do float
#endif // SYNTH_H
synth.c// synth.c - tylko część dotycząca reverb (reszta bez zmian)
//======================== Implementacja reverb ========================//
void reverb_init(Reverb* rev) {
memset(rev->buffer, 0, sizeof(rev->buffer));
rev->write_pos = 0;
rev->prev_input = 0.0f;
rev->prev_output = 0.0f;
// Opóźnienia dobrane tak, aby były względnie pierwsze (mniejsze artefakty)
rev->delay_lengths[0] = 1433; // ~30ms
rev->delay_lengths[1] = 1911; // ~40ms
rev->delay_lengths[2] = 2399; // ~50ms
rev->delay_lengths[3] = 2999; // ~62ms
rev->feedback_gain = 0.65f; // Nieco mniejszy feedback dla stabilności
rev->mix_level = REVERB_MIX;
// Inicjalizacja filtru tłumienia
simple_lp_init(&rev->dampening_filter, 5000.0f, SAMPLE_RATE);
}
float reverb_process(Reverb* rev, float input) {
// Płynne przejście między próbkami (anti-click)
float smoothed_input = 0.99f * rev->prev_input + 0.01f * input;
rev->prev_input = input;
// Dodaj sygnał wejściowy do bufora
rev->buffer[rev->write_pos] = smoothed_input;
// Oblicz pozycje odczytu dla każdej linii opóźnienia
float output = 0.0f;
for (int i = 0; i < REVERB_LINES; i++) {
uint32_t read_pos = (rev->write_pos - rev->delay_lengths[i] + REVERB_BUFFER_SIZE) % REVERB_BUFFER_SIZE;
output += rev->buffer[read_pos];
}
// Normalizacja i filtrowanie
output /= REVERB_LINES;
output = simple_lp_process(&rev->dampening_filter, output);
// Dodaj sprzężenie zwrotne z płynnym przejściem
float feedback = output * rev->feedback_gain;
rev->buffer[rev->write_pos] += feedback;
// Płynne przejście wyjściowe
float smoothed_output = 0.99f * rev->prev_output + 0.01f * output;
rev->prev_output = smoothed_output;
// Aktualizuj pozycję zapisu
rev->write_pos = (rev->write_pos + 1) % REVERB_BUFFER_SIZE;
// Zwróć mieszankę sygnału suchego i mokrego
return input * (1.0f - rev->mix_level) + smoothed_output * rev->mix_level;
}
//======================== Przetwarzanie Audio ========================//
void synth_process_audio(int16_t* buffer, uint32_t num_frames) {
float current_scale = voice_scaling[active_voice_count];
for (uint32_t frame = 0; frame < num_frames; frame++) {
float mixed_sample = 0.0f;
// Miksowanie aktywnych głosów (bez zmian)
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active) {
float phase = voices[i].phase_accumulator;
uint32_t index1 = (uint32_t)phase;
uint32_t index2 = (index1 + 1) % SINE_TABLE_SIZE;
float fraction = phase - (float)index1;
float sample = sine_table[index1] + (sine_table[index2] - sine_table[index1]) * fraction;
mixed_sample += sample;
voices[i].phase_accumulator += voices[i].phase_increment;
while (voices[i].phase_accumulator >= (float)SINE_TABLE_SIZE) {
voices[i].phase_accumulator -= (float)SINE_TABLE_SIZE;
}
}
}
// Skalowanie sumy głosów
mixed_sample *= BASE_AMPLITUDE * current_scale;
// Przetwarzanie reverb (teraz w float)
float wet_sample = reverb_process(&reverb, mixed_sample);
// Filtrowanie dolnoprzepustowe
float filtered_left = simple_lp_process(&output_filter_left, wet_sample);
float filtered_right = simple_lp_process(&output_filter_right, wet_sample);
// Miękkie ograniczenie
if (filtered_left > 1.0f) filtered_left = 1.0f;
else if (filtered_left < -1.0f) filtered_left = -1.0f;
// Konwersja na int16_t
int16_t final_sample = (int16_t)(filtered_left * 32767.0f);
// Zapis do bufora (stereo)
buffer[frame * 2] = final_sample;
buffer[frame * 2 + 1] = final_sample;
}
}
napisz cały kod synth c oraz h z tym soft reverbem
Oto kompletny kod z płynnym reverbem, zoptymalizowany pod kątem pamięci i wydajności:
synth.h// synth.h
#ifndef SYNTH_H
#define SYNTH_H
#include <stdint.h>
#include <stdbool.h>
// Parametry syntezatora
#define SAMPLE_RATE 48000
#define MAX_VOICES 8
#define SINE_TABLE_SIZE 512
// Parametry reverb (zoptymalizowane)
#define REVERB_BUFFER_SIZE 12000 // ~250ms opóźnienia
#define REVERB_LINES 4
#define REVERB_MIX 0.3f
// Definicja pojedynczego głosu
typedef struct {
uint8_t midi_note;
bool is_active;
float phase_accumulator;
float phase_increment;
} SynthVoice;
// Prosty filtr dolnoprzepustowy
typedef struct {
float cutoff_freq;
float sample_rate;
float a;
float prev_output;
} SimpleLP;
// Struktura dla efektu reverb
typedef struct {
float buffer[REVERB_BUFFER_SIZE];
uint32_t write_pos;
uint16_t delay_lengths[REVERB_LINES];
float feedback_gain;
float mix_level;
SimpleLP dampening_filter;
float prev_input;
float prev_output;
} Reverb;
// Inicjalizacja syntezatora
void synth_init(void);
// Obsługa komunikatów MIDI
void synth_note_on(uint8_t note);
void synth_note_off(uint8_t note);
// Generowanie próbek audio
void synth_process_audio(int16_t* buffer, uint32_t num_frames);
// Funkcje filtru
void simple_lp_init(SimpleLP* filter, float cutoff_freq, float sample_rate);
float simple_lp_process(SimpleLP* filter, float input);
// Funkcje reverb
void reverb_init(Reverb* rev);
float reverb_process(Reverb* rev, float input);
#endif // SYNTH_H
synth.c - Kompletna implementacja// synth.c
#include "synth.h"
#include <math.h>
#include <string.h>
// Bazowa amplituda dla jednego głosu
#define BASE_AMPLITUDE 26000.0f
// Tablica skalowania głośności dla różnej liczby głosów
static const float voice_scaling[MAX_VOICES + 1] = {
1.00f, 0.80f, 0.57f, 0.46f, 0.40f, 0.36f, 0.33f, 0.30f, 0.28f
};
// Globalne zmienne syntezatora
static SynthVoice voices[MAX_VOICES];
static float sine_table[SINE_TABLE_SIZE];
static SimpleLP output_filter_left;
static SimpleLP output_filter_right;
static Reverb reverb;
static int active_voice_count = 0;
//======================== Funkcje pomocnicze ========================//
static float midi_to_freq(uint8_t note) {
return 440.0f * powf(2.0f, (float)(note - 69) / 12.0f);
}
static float freq_to_phase_increment(float freq) {
return (freq * SINE_TABLE_SIZE) / SAMPLE_RATE;
}
static void update_active_voice_count(void) {
int count = 0;
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active) count++;
}
active_voice_count = count;
}
//======================== Implementacja filtru ========================//
void simple_lp_init(SimpleLP* filter, float cutoff_freq, float sample_rate) {
filter->cutoff_freq = cutoff_freq;
filter->sample_rate = sample_rate;
float rc = 1.0f / (2.0f * M_PI * cutoff_freq);
float dt = 1.0f / sample_rate;
filter->a = dt / (rc + dt);
filter->prev_output = 0.0f;
}
float simple_lp_process(SimpleLP* filter, float input) {
float output = filter->prev_output + filter->a * (input - filter->prev_output);
filter->prev_output = output;
return output;
}
//======================== Implementacja reverb ========================//
void reverb_init(Reverb* rev) {
memset(rev->buffer, 0, sizeof(rev->buffer));
rev->write_pos = 0;
rev->prev_input = 0.0f;
rev->prev_output = 0.0f;
// Opóźnienia dobrane tak, aby były względnie pierwsze
rev->delay_lengths[0] = 1433; // ~30ms
rev->delay_lengths[1] = 1911; // ~40ms
rev->delay_lengths[2] = 2399; // ~50ms
rev->delay_lengths[3] = 2999; // ~62ms
rev->feedback_gain = 0.65f; // Optymalny feedback
rev->mix_level = REVERB_MIX;
// Inicjalizacja filtru tłumienia
simple_lp_init(&rev->dampening_filter, 5000.0f, SAMPLE_RATE);
}
float reverb_process(Reverb* rev, float input) {
// Płynne przejście między próbkami (anti-click)
float smoothed_input = 0.99f * rev->prev_input + 0.01f * input;
rev->prev_input = input;
// Dodaj sygnał wejściowy do bufora
rev->buffer[rev->write_pos] = smoothed_input;
// Oblicz pozycje odczytu dla każdej linii opóźnienia
float output = 0.0f;
for (int i = 0; i < REVERB_LINES; i++) {
uint32_t read_pos = (rev->write_pos - rev->delay_lengths[i] + REVERB_BUFFER_SIZE) % REVERB_BUFFER_SIZE;
output += rev->buffer[read_pos];
}
// Normalizacja i filtrowanie
output /= REVERB_LINES;
output = simple_lp_process(&rev->dampening_filter, output);
// Dodaj sprzężenie zwrotne z płynnym przejściem
float feedback = output * rev->feedback_gain;
rev->buffer[rev->write_pos] += feedback;
// Płynne przejście wyjściowe
float smoothed_output = 0.99f * rev->prev_output + 0.01f * output;
rev->prev_output = smoothed_output;
// Aktualizuj pozycję zapisu
rev->write_pos = (rev->write_pos + 1) % REVERB_BUFFER_SIZE;
// Zwróć mieszankę sygnału suchego i mokrego
return input * (1.0f - rev->mix_level) + smoothed_output * rev->mix_level;
}
//======================== Inicjalizacja ========================//
void synth_init(void) {
memset(voices, 0, sizeof(voices));
active_voice_count = 0;
// Generuj tablicę sinusoidalną
for (int i = 0; i < SINE_TABLE_SIZE; i++) {
float angle = (float)i * 2.0f * M_PI / SINE_TABLE_SIZE;
sine_table[i] = sinf(angle);
}
// Inicjalizacja filtrów wyjściowych
simple_lp_init(&output_filter_left, 12000.0f, SAMPLE_RATE);
simple_lp_init(&output_filter_right, 12000.0f, SAMPLE_RATE);
// Inicjalizacja reverb
reverb_init(&reverb);
}
//======================== Obsługa MIDI ========================//
void synth_note_on(uint8_t note) {
// Sprawdź, czy ta nuta już gra
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active && voices[i].midi_note == note) {
return;
}
}
// Znajdź pierwszy wolny głos
for (int i = 0; i < MAX_VOICES; i++) {
if (!voices[i].is_active) {
float freq = midi_to_freq(note);
voices[i].phase_increment = freq_to_phase_increment(freq);
voices[i].phase_accumulator = 0.0f;
voices[i].midi_note = note;
voices[i].is_active = true;
update_active_voice_count();
return;
}
}
// Voice stealing
static uint8_t oldest_voice = 0;
voices[oldest_voice].phase_increment = freq_to_phase_increment(midi_to_freq(note));
voices[oldest_voice].phase_accumulator = 0.0f;
voices[oldest_voice].midi_note = note;
voices[oldest_voice].is_active = true;
oldest_voice = (oldest_voice + 1) % MAX_VOICES;
}
void synth_note_off(uint8_t note) {
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active && voices[i].midi_note == note) {
voices[i].is_active = false;
update_active_voice_count();
return;
}
}
}
//======================== Przetwarzanie Audio ========================//
void synth_process_audio(int16_t* buffer, uint32_t num_frames) {
float current_scale = voice_scaling[active_voice_count];
for (uint32_t frame = 0; frame < num_frames; frame++) {
float mixed_sample = 0.0f;
// Miksowanie aktywnych głosów
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].is_active) {
float phase = voices[i].phase_accumulator;
uint32_t index1 = (uint32_t)phase;
uint32_t index2 = (index1 + 1) % SINE_TABLE_SIZE;
float fraction = phase - (float)index1;
float sample = sine_table[index1] + (sine_table[index2] - sine_table[index1]) * fraction;
mixed_sample += sample;
voices[i].phase_accumulator += voices[i].phase_increment;
while (voices[i].phase_accumulator >= (float)SINE_TABLE_SIZE) {
voices[i].phase_accumulator -= (float)SINE_TABLE_SIZE;
}
}
}
// Skalowanie sumy głosów
mixed_sample *= BASE_AMPLITUDE * current_scale;
// Przetwarzanie reverb
float wet_sample = reverb_process(&reverb, mixed_sample);
// Filtrowanie dolnoprzepustowe
float filtered_left = simple_lp_process(&output_filter_left, wet_sample);
float filtered_right = simple_lp_process(&output_filter_right, wet_sample);
// Miękkie ograniczenie
if (filtered_left > 1.0f) filtered_left = 1.0f;
else if (filtered_left < -1.0f) filtered_left = -1.0f;
// Konwersja na int16_t
int16_t final_sample = (int16_t)(filtered_left * 32767.0f);
// Zapis do bufora (stereo)
buffer[frame * 2] = final_sample;
buffer[frame * 2 + 1] = final_sample;
}
}
Jeśli nadal masz problemy z pamięcią, możesz dalej zmniejszyć REVERB_BUFFER_SIZE do 8000 (166ms) - efekt będzie krótszy, ale nadal słyszalny.
Możesz zmniejszyć liczbę linii opóźnienia (REVERB_LINES) z 4 do 2 lub 3.
Jeśli używasz STM32 z DSP (np. STM32F4), możesz użyć instrukcji SIMD do przyspieszenia obliczeń.
Rozważ użycie bufora cyklicznego zamiast pełnego bufora dla reverb (ale to komplikuje implementację).
Te zmiany powinny znacznie zmniejszyć zużycie pamięci RAM, zachowując przy tym efekt reverb. Efekt będzie nieco krótszy (250ms zamiast 1s), ale nadal będzie słyszalny i użyteczny.
smoothed_input dla płynnego wejściasmoothed_output dla płynnego wyjściaprev_input, prev_output) do przechowywania poprzednich wartościTe zmiany powinny wyeliminować stukanie i dać płynny efekt reverb. Jeśli nadal słyszysz artefakty, możesz spróbować:
feedback_gain do 0.6 lub 0.55Efekt powinien być teraz płynny i pozbawiony artefaktów.
Płynny reverb z eliminacją artefaktów:
Optymalizacja pamięci:
Dobre skalowanie głośności:
voice_scaling zapewnia odpowiednie poziomy dla różnych liczb głosówStabilny algorytm reverb:
Dobra jakość dźwięku:
Ten kod powinien działać płynnie na większości mikrokontrolerów STM32 z wystarczającą ilością pamięci RAM (potrzebuje około 48KB dla bufora reverb). Jeśli nadal masz problemy z pamięcią, możesz zmniejszyć REVERB_BUFFER_SIZE do 8000 (166ms), co nadal da słyszalny efekt reverb.