diff --git a/include/audio_engine.hpp b/include/audio_engine.hpp index 09baecb..03c197b 100644 --- a/include/audio_engine.hpp +++ b/include/audio_engine.hpp @@ -1,10 +1,45 @@ #ifndef AUDIO_ENGINE #define AUDIO_ENGINE +#include +#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 diff --git a/include/synth.hpp b/include/synth.hpp index 7f61752..8659375 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -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 diff --git a/main b/main deleted file mode 100755 index 8602228..0000000 Binary files a/main and /dev/null differ diff --git a/src/audio_engine.cpp b/src/audio_engine.cpp index c616a57..947c49a 100644 --- a/src/audio_engine.cpp +++ b/src/audio_engine.cpp @@ -1 +1,43 @@ #include "../include/audio_engine.hpp" + +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(pDevice->pUserData); + float* output = static_cast (pOutput); + for (ma_uint32 i = 0; i < frameCount; ++i){ + float buf = data->synth_.generate(); + output[i*2] = buf; + output[i*2+1] = buf; + } +} diff --git a/src/main.cpp b/src/main.cpp index 25597d0..aaf32ca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,27 +1,29 @@ #include +#include #include "../include/synth.hpp" +#include "../include/audio_engine.hpp" int main(){ - std::cout << "440 гц\n"; + std::cout << "110.гц\n"; Synth synth; - synth.set_frequency(440); + synth.set_frequency(110); 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"; + + + try{ + AudioEngine audio_engine(synth); + audio_engine.start(); + std::cin.get(); + audio_engine.stop(); + } + catch (const std::exception& error){ + std::cerr << error.what() << "\n"; + return 1; } - std::cout << "\n"; - - for (int i = 0; i < 5; i++){ - std::cout << buffer2[i] << "\n"; - } return 0; } diff --git a/src/miniaudio.cpp b/src/miniaudio.cpp new file mode 100644 index 0000000..03a9173 --- /dev/null +++ b/src/miniaudio.cpp @@ -0,0 +1,2 @@ +#define MINIAUDIO_IMPLEMENTATION +#include "../external/miniaudio.h"