2026-11-12 16:34:18 +06:00
|
|
|
#include "../include/audio_engine.hpp"
|
2026-11-12 20:19:00 +06:00
|
|
|
|
2026-11-13 02:02:50 +06:00
|
|
|
|
2026-11-12 20:19:00 +06:00
|
|
|
int AudioEngine::start(){
|
|
|
|
|
if (device_is_started) throw std::runtime_error("ошибка: повторный запуск");
|
|
|
|
|
if (device_is_init){
|
|
|
|
|
result_start = ma_device_start(&device);
|
|
|
|
|
|
|
|
|
|
if (result_start == MA_SUCCESS){
|
|
|
|
|
device_is_started = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw std::runtime_error("ошибка: запуск не удался");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
throw std::runtime_error("ошибка: запуск не удался. девайс не был проинициализирован");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AudioEngine::stop(){
|
|
|
|
|
if (!device_is_started) throw std::runtime_error("ошибка: повторная остановка");
|
|
|
|
|
result_stop = ma_device_stop(&device);
|
|
|
|
|
|
|
|
|
|
if (result_stop == MA_SUCCESS){
|
|
|
|
|
device_is_started = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioEngine::data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount){
|
|
|
|
|
AudioEngine* data = static_cast<AudioEngine*>(pDevice->pUserData);
|
2026-11-16 00:49:29 +06:00
|
|
|
float* output = static_cast<float*>(pOutput);
|
2026-11-12 20:19:00 +06:00
|
|
|
for (ma_uint32 i = 0; i < frameCount; ++i){
|
2026-11-16 00:49:29 +06:00
|
|
|
float left_acc = 0.0f;
|
|
|
|
|
float right_acc = 0.0f;
|
|
|
|
|
for (auto& synth : data->synths){
|
|
|
|
|
StereoSample buf = synth->generate();
|
|
|
|
|
left_acc += buf.left;
|
|
|
|
|
right_acc += buf.right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output[i * 2] = left_acc;
|
|
|
|
|
output[i * 2 + 1] = right_acc;
|
2026-11-12 20:19:00 +06:00
|
|
|
}
|
|
|
|
|
}
|