This commit is contained in:
2026-11-13 23:59:25 +06:00
parent 7d60addcd9
commit 5731475015
6 changed files with 126 additions and 97 deletions
+97 -81
View File
@@ -8,62 +8,78 @@
#include "../include/spsc_queue.hpp"
#include "../include/command.hpp"
void producer(SpscQueue& queue, std::atomic<bool>& 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<bool>& 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<bool>& 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<bool>& 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<int>(command.type) << "\n";
// std::cout << static_cast<int>(command.note)<< "\n";
// std::cout << static_cast<int>(command.velocity) << "\n";
// std::atomic<bool> 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;