реализован базовый синтезатор синусоидальной волны

реализован класс Synth
добавлена генерация синусоидального сигнала
реализовано управление частотой и амплитудой
добавлено ограничение допустимых значений параметров
реализовано управление фазой с сохранением непрерывности сигнала
добавлена генерация аудиобуферов через generate_buffer()
разделены объявления и реализации методов (synth.hpp / synth.cpp)
проверена непрерывность сигнала между последовательными буферами
подготовлена основа для дальнейшего подключения miniaudio
This commit is contained in:
2026-11-12 16:34:18 +06:00
parent 6f9f9dc9a1
commit 0453cfdce7
9 changed files with 97045 additions and 0 deletions
+1
View File
@@ -34,3 +34,4 @@
*.kdev4
.kdev4/
*.kate-swp
View File
+96949
View File
File diff suppressed because it is too large Load Diff
+10
View File
@@ -0,0 +1,10 @@
#ifndef AUDIO_ENGINE
#define AUDIO_ENGINE
class AudioEngine{
};
#endif
+28
View File
@@ -0,0 +1,28 @@
#ifndef SYNTH
#define SYNTH
#include <cstdint>
#include <numbers>
#include <cmath>
class Synth {
// методы
public:
double generate();
void generate_buffer(double* array, int size);
void set_frequency(double value);
void set_amplitude(double value);
// поля
private:
// частота звука в Герцах
double frequency = 440;
// громкость звука, от 0 до 1
double amplitude = 0.5;
// частота дискретизации
uint32_t sample_rate = 48000;
// фаза. от 0 до 2 пи
double phase = 0.0;
};
#endif
Executable
BIN
View File
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
#include "../include/audio_engine.hpp"
+27
View File
@@ -0,0 +1,27 @@
#include <iostream>
#include "../include/synth.hpp"
int main(){
std::cout << "440 гц\n";
Synth synth;
synth.set_frequency(440);
synth.set_amplitude(0.5);
double buffer[32];
double buffer2[32];
synth.generate_buffer(buffer, 32);
synth.generate_buffer(buffer2, 32);
for (int i = 32-5; i < 32; i++){
std::cout << buffer[i] << "\n";
}
std::cout << "\n";
for (int i = 0; i < 5; i++){
std::cout << buffer2[i] << "\n";
}
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
#include "../include/synth.hpp"
double Synth::generate(){
phase = std::fmod(phase, (2*std::numbers::pi));
double y = amplitude*std::sin(phase);
phase += (2*std::numbers::pi*frequency)/(sample_rate);
return y;
}
void Synth::generate_buffer(double* array, int size){
phase = 0;
for (int i = 0; i < size; ++i){
array[i] = generate();
}
}
void Synth::set_frequency(double value){
if (value < 0.0) frequency = 0.0;
else if (value >(sample_rate/2)) frequency = sample_rate/2;
else frequency = value;
}
void Synth::set_amplitude(double value){
if (value < 0.0) amplitude = 0.0;
else if (value > 1.0) amplitude = 1.0;
else amplitude = value;
}