реализован аудиодвижок с помощью miniaudio playback
реализован класс AudioEngine интеграция miniaudio для управления аудиустройствами реализовано инициализация и очиста AudioEngine добавлено управление запуском и оставкой аудиоустройства генерация стерео float32 аудиопотока подключен осцилятор для генерации звука добавлена обработка ошибок
This commit is contained in:
@@ -1,10 +1,45 @@
|
|||||||
#ifndef AUDIO_ENGINE
|
#ifndef AUDIO_ENGINE
|
||||||
#define AUDIO_ENGINE
|
#define AUDIO_ENGINE
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include "../external/miniaudio.h"
|
||||||
|
#include "../include/synth.hpp"
|
||||||
|
|
||||||
class AudioEngine{
|
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
|
#endif
|
||||||
|
|||||||
+11
-4
@@ -8,6 +8,13 @@
|
|||||||
class Synth {
|
class Synth {
|
||||||
// методы
|
// методы
|
||||||
public:
|
public:
|
||||||
|
Synth(){
|
||||||
|
frequency = 440;
|
||||||
|
amplitude = 0.5;
|
||||||
|
sample_rate = 48000;
|
||||||
|
phase = 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
double generate();
|
double generate();
|
||||||
void generate_buffer(double* array, int size);
|
void generate_buffer(double* array, int size);
|
||||||
void set_frequency(double value);
|
void set_frequency(double value);
|
||||||
@@ -16,13 +23,13 @@ class Synth {
|
|||||||
// поля
|
// поля
|
||||||
private:
|
private:
|
||||||
// частота звука в Герцах
|
// частота звука в Герцах
|
||||||
double frequency = 440;
|
double frequency;
|
||||||
// громкость звука, от 0 до 1
|
// громкость звука, от 0 до 1
|
||||||
double amplitude = 0.5;
|
double amplitude;
|
||||||
// частота дискретизации
|
// частота дискретизации
|
||||||
uint32_t sample_rate = 48000;
|
uint32_t sample_rate;
|
||||||
// фаза. от 0 до 2 пи
|
// фаза. от 0 до 2 пи
|
||||||
double phase = 0.0;
|
double phase;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1 +1,43 @@
|
|||||||
#include "../include/audio_engine.hpp"
|
#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<AudioEngine*>(pDevice->pUserData);
|
||||||
|
float* output = static_cast<float*> (pOutput);
|
||||||
|
for (ma_uint32 i = 0; i < frameCount; ++i){
|
||||||
|
float buf = data->synth_.generate();
|
||||||
|
output[i*2] = buf;
|
||||||
|
output[i*2+1] = buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+15
-13
@@ -1,27 +1,29 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "../include/synth.hpp"
|
#include "../include/synth.hpp"
|
||||||
|
#include "../include/audio_engine.hpp"
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
std::cout << "440 гц\n";
|
std::cout << "110.гц\n";
|
||||||
Synth synth;
|
Synth synth;
|
||||||
synth.set_frequency(440);
|
synth.set_frequency(110);
|
||||||
synth.set_amplitude(0.5);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
#define MINIAUDIO_IMPLEMENTATION
|
||||||
|
#include "../external/miniaudio.h"
|
||||||
Reference in New Issue
Block a user