реализован аудиодвижок с помощью miniaudio playback

реализован класс AudioEngine
интеграция miniaudio для управления аудиустройствами
реализовано инициализация и очиста AudioEngine
добавлено управление запуском и оставкой аудиоустройства
генерация стерео float32 аудиопотока
подключен осцилятор для генерации звука
добавлена обработка ошибок
This commit is contained in:
2026-11-12 20:19:00 +06:00
parent 0453cfdce7
commit 87e59c84e5
6 changed files with 107 additions and 19 deletions
+37 -2
View File
@@ -1,10 +1,45 @@
#ifndef AUDIO_ENGINE
#define AUDIO_ENGINE
#include <stdexcept>
#include "../external/miniaudio.h"
#include "../include/synth.hpp"
class AudioEngine{
public:
AudioEngine(Synth& synth) : synth_(synth){
config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32;
config.playback.channels = 2;
config.sampleRate = 48000;
config.pUserData = this;
config.dataCallback = data_callback;
result_init = ma_device_init(NULL, &config, &device);
if (result_init == MA_SUCCESS) device_is_init = true;
else{
throw std::runtime_error("ошибка: не удалось проинициализировать девайс");
}
}
~AudioEngine(){
if (device_is_init) ma_device_uninit(&device);
}
static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);
int start();
int stop();
private:
bool device_is_init = false;
bool device_is_started = false;
Synth& synth_;
ma_device_config config = {};
ma_device device = {};
ma_result result_init;
ma_result result_start;
ma_result result_stop;
};
#endif
+11 -4
View File
@@ -8,6 +8,13 @@
class Synth {
// методы
public:
Synth(){
frequency = 440;
amplitude = 0.5;
sample_rate = 48000;
phase = 0.0;
};
double generate();
void generate_buffer(double* array, int size);
void set_frequency(double value);
@@ -16,13 +23,13 @@ class Synth {
// поля
private:
// частота звука в Герцах
double frequency = 440;
double frequency;
// громкость звука, от 0 до 1
double amplitude = 0.5;
double amplitude;
// частота дискретизации
uint32_t sample_rate = 48000;
uint32_t sample_rate;
// фаза. от 0 до 2 пи
double phase = 0.0;
double phase;
};
#endif