From 5731475015737e0e46c9f8d847b8421fafaed417 Mon Sep 17 00:00:00 2001 From: s13bby Date: Fri, 13 Nov 2026 23:59:25 +0600 Subject: [PATCH] . --- include/command.hpp | 10 ++- include/spsc_queue.hpp | 6 +- include/synth.hpp | 7 +- src/main.cpp | 178 ++++++++++++++++++++++------------------- src/spsc_queue.cpp | 2 +- src/synth.cpp | 20 +++-- 6 files changed, 126 insertions(+), 97 deletions(-) diff --git a/include/command.hpp b/include/command.hpp index a14679f..1102698 100644 --- a/include/command.hpp +++ b/include/command.hpp @@ -1,10 +1,18 @@ #ifndef COMMAND #define COMMAND -enum class Command{ +#include + +enum class CommandType{ None, NoteOn, NoteOff }; +struct Command{ + CommandType type; + uint8_t note; + uint8_t velocity; +}; + #endif diff --git a/include/spsc_queue.hpp b/include/spsc_queue.hpp index 8c1e470..585b139 100644 --- a/include/spsc_queue.hpp +++ b/include/spsc_queue.hpp @@ -13,11 +13,11 @@ class SpscQueue{ SpscQueue(){ head.store(0, std::memory_order_relaxed); tail.store(0, std::memory_order_relaxed); - buffer.fill(Command::None); + buffer.fill({CommandType::None, 0, 0}); } - bool push(Command command); - bool pop(Command& command); + bool push(const Command&); + bool pop(Command&); private: diff --git a/include/synth.hpp b/include/synth.hpp index ea7cd4b..10d0671 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -13,7 +13,7 @@ class Synth { public: Synth(){ - frequency = 440; + frequency = 440.0; amplitude = 0.5; sample_rate = 48000; phase = 0.0; @@ -24,9 +24,10 @@ class Synth { void generate_buffer(double* array, int size); void set_frequency(double value); void set_amplitude(double value); + void set_midi_note(uint8_t note); - bool note_on(); - bool note_off(); + bool note_on(uint8_t note, uint8_t velocity); + bool note_off(uint8_t note); private: /* 1. частота звука в Герцах diff --git a/src/main.cpp b/src/main.cpp index 3244e1d..3417a6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,62 +8,78 @@ #include "../include/spsc_queue.hpp" #include "../include/command.hpp" -void producer(SpscQueue& queue, std::atomic& producer_done){ - for (int i = 0; i < 32; ++i){ - while(!queue.push(Command::NoteOn)){ - std::this_thread::yield(); - } - while(!queue.push(Command::NoteOff)){ - std::this_thread::yield(); - } - } - producer_done.store(true, std::memory_order_release); -} - -void consumer(SpscQueue& queue, std::atomic& producer_done){ - Command command; - Command expected = Command::NoteOn; - int counter = 0; - - while (true){ - if (!queue.pop(command)){ - if (producer_done.load(std::memory_order_acquire)) break; - else{ - std::this_thread::yield(); - continue; - } - } - else{ - ++counter; - if (command != expected){ - std::cerr << "команда из очереди не совпадает с ожидаемой"; - break; - } - switch(command){ - case Command::NoteOn: { - expected = Command::NoteOff; - break; - } - case Command::NoteOff: { - expected = Command::NoteOn; - break; - } - case Command::None: { - break; - } - } - - } - } - std::cout << counter; -} +// void producer(SpscQueue& queue, std::atomic& producer_done){ +// for (int i = 0; i < 32; ++i){ +// while(!queue.push(Command::NoteOn)){ +// std::this_thread::yield(); +// } +// while(!queue.push(Command::NoteOff)){ +// std::this_thread::yield(); +// } +// } +// producer_done.store(true, std::memory_order_release); +// } +// +// void consumer(SpscQueue& queue, std::atomic& producer_done){ +// Command command; +// Command expected = Command::NoteOn; +// int counter = 0; +// +// while (true){ +// if (!queue.pop(command)){ +// if (producer_done.load(std::memory_order_acquire)) break; +// else{ +// std::this_thread::yield(); +// continue; +// } +// } +// else{ +// ++counter; +// if (command != expected){ +// std::cerr << "команда из очереди не совпадает с ожидаемой"; +// break; +// } +// switch(command){ +// case Command::NoteOn: { +// expected = Command::NoteOff; +// break; +// } +// case Command::NoteOff: { +// expected = Command::NoteOn; +// break; +// } +// case Command::None: { +// break; +// } +// } +// +// } +// } +// std::cout << counter; +// } int main(){ - std::cout << "440.гц\n"; - std::string a = ""; - Synth synth; - // SpscQueue queue; + // std::cout << "440.гц\n"; + // std::string a = ""; + // Synth synth; + + Command command; + SpscQueue queue; + + for (int i = 0; i < 33; ++i){ + std::cout << queue.push({CommandType::NoteOn, 60, 100}) << "\n"; + std::cout << queue.push({CommandType::NoteOn, 20, 80}) << "\n"; + } + std::cout << "выгрузка\n"; + for (int i = 0; i < 65; ++i){ + std::cout << queue.pop(command) << "\n"; + + } + // std::cout << static_cast(command.type) << "\n"; + // std::cout << static_cast(command.note)<< "\n"; + // std::cout << static_cast(command.velocity) << "\n"; + // std::atomic producer_done{false}; // @@ -76,34 +92,34 @@ int main(){ // thread1.join(); // thread2.join(); - synth.set_frequency(440); - synth.set_amplitude(0.5); - - try{ - AudioEngine audio_engine(synth); - audio_engine.start(); - while (true){ - std::cin >> a; - if (a == "on"){ - if (!synth.note_on()){ - throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); - } - } - else if (a == "off"){ - if (!synth.note_off()){ - throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); - } - } - else if (a == "exit"){ - audio_engine.stop(); - return 0; - } - } - } - catch (const std::exception& error){ - std::cerr << error.what() << "\n"; - return 1; - } + // synth.set_frequency(440); + // synth.set_amplitude(0.5); + // + // try{ + // AudioEngine audio_engine(synth); + // audio_engine.start(); + // while (true){ + // std::cin >> a; + // if (a == "on"){ + // if (!synth.note_on()){ + // throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); + // } + // } + // else if (a == "off"){ + // if (!synth.note_off()){ + // throw std::runtime_error("ошибка: не удалось воспроизвести ноту"); + // } + // } + // else if (a == "exit"){ + // audio_engine.stop(); + // return 0; + // } + // } + // } + // catch (const std::exception& error){ + // std::cerr << error.what() << "\n"; + // return 1; + // } return 0; diff --git a/src/spsc_queue.cpp b/src/spsc_queue.cpp index 5d1af30..9b71d17 100644 --- a/src/spsc_queue.cpp +++ b/src/spsc_queue.cpp @@ -1,6 +1,6 @@ #include "../include/spsc_queue.hpp" -bool SpscQueue::push(Command command){ +bool SpscQueue::push(const Command& command){ std::size_t current_head = head.load(std::memory_order_relaxed); std::size_t current_tail = tail.load(std::memory_order_acquire); if ((current_head - current_tail) >= buffer.size()){ diff --git a/src/synth.cpp b/src/synth.cpp index 39c5857..51774c6 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -6,19 +6,19 @@ double Synth::generate(){ Command command; while (processed < 16 && queue.pop(command)){ - switch (command){ + switch (command.type){ default: break; - case Command::None: + case CommandType::None: ++processed; break; - case Command::NoteOn: + case CommandType::NoteOn: envelope.note_on(); ++processed; break; - case Command::NoteOff: + case CommandType::NoteOff: envelope.note_off(); ++processed; break; @@ -51,10 +51,14 @@ void Synth::set_amplitude(double value){ else amplitude = value; } -bool Synth::note_on(){ - return queue.push(Command::NoteOn); +void Synth::set_midi_note(uint8_t note){ + set_frequency(440.0*std::pow(2.0,( (static_cast(note)-69)/12.0 ))); } -bool Synth::note_off(){ - return queue.push(Command::NoteOff); +bool Synth::note_on(uint8_t note, uint8_t velocity){ + return queue.push({CommandType::NoteOn, note, velocity}); +} + +bool Synth::note_off(uint8_t note){ + return queue.push({CommandType::NoteOff, note, 0}); }