2026-11-12 16:34:18 +06:00
|
|
|
#include <iostream>
|
2026-11-12 20:19:00 +06:00
|
|
|
#include <stdexcept>
|
2026-11-13 18:31:07 +06:00
|
|
|
#include <thread>
|
|
|
|
|
#include <atomic>
|
2026-11-12 16:34:18 +06:00
|
|
|
|
|
|
|
|
#include "../include/synth.hpp"
|
2026-11-12 20:19:00 +06:00
|
|
|
#include "../include/audio_engine.hpp"
|
2026-11-13 18:31:07 +06:00
|
|
|
#include "../include/spsc_queue.hpp"
|
|
|
|
|
#include "../include/command.hpp"
|
2026-11-12 16:34:18 +06:00
|
|
|
|
2026-11-13 18:31:07 +06:00
|
|
|
void producer(SpscQueue& queue, std::atomic<bool>& producer_done){
|
2026-11-13 22:18:57 +06:00
|
|
|
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);
|
2026-11-13 18:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void consumer(SpscQueue& queue, std::atomic<bool>& producer_done){
|
2026-11-13 22:18:57 +06:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-13 18:31:07 +06:00
|
|
|
|
2026-11-13 22:18:57 +06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::cout << counter;
|
2026-11-13 18:31:07 +06:00
|
|
|
}
|
2026-11-13 02:02:50 +06:00
|
|
|
|
2026-11-12 16:34:18 +06:00
|
|
|
int main(){
|
|
|
|
|
|
2026-11-13 22:18:57 +06:00
|
|
|
std::cout << "440.гц\n";
|
2026-11-12 20:58:48 +06:00
|
|
|
std::string a = "";
|
2026-11-12 16:34:18 +06:00
|
|
|
Synth synth;
|
2026-11-13 22:18:57 +06:00
|
|
|
// SpscQueue queue;
|
2026-11-12 16:34:18 +06:00
|
|
|
|
2026-11-13 22:18:57 +06:00
|
|
|
// std::atomic<bool> producer_done{false};
|
|
|
|
|
//
|
|
|
|
|
// std::thread thread1 (
|
|
|
|
|
// producer, std::ref(queue), std::ref(producer_done)
|
|
|
|
|
// );
|
|
|
|
|
//
|
|
|
|
|
// std::thread thread2 (consumer, std::ref(queue), std::ref(producer_done));
|
|
|
|
|
//
|
|
|
|
|
// thread1.join();
|
|
|
|
|
// thread2.join();
|
2026-11-13 18:31:07 +06:00
|
|
|
|
2026-11-13 22:18:57 +06:00
|
|
|
synth.set_frequency(440);
|
2026-11-13 18:31:07 +06:00
|
|
|
synth.set_amplitude(0.5);
|
2026-11-12 16:34:18 +06:00
|
|
|
|
2026-11-12 20:19:00 +06:00
|
|
|
try{
|
|
|
|
|
AudioEngine audio_engine(synth);
|
2026-11-13 02:02:50 +06:00
|
|
|
audio_engine.start();
|
2026-11-12 20:58:48 +06:00
|
|
|
while (true){
|
|
|
|
|
std::cin >> a;
|
2026-11-13 22:18:57 +06:00
|
|
|
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;
|
2026-11-12 20:58:48 +06:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-12 20:19:00 +06:00
|
|
|
}
|
|
|
|
|
catch (const std::exception& error){
|
|
|
|
|
std::cerr << error.what() << "\n";
|
|
|
|
|
return 1;
|
2026-11-12 16:34:18 +06:00
|
|
|
}
|
|
|
|
|
|
2026-11-12 20:19:00 +06:00
|
|
|
|
2026-11-12 16:34:18 +06:00
|
|
|
return 0;
|
|
|
|
|
}
|