diff --git a/CMakeLists.txt b/CMakeLists.txt index 12dd077..bc191ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,10 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external ) +target_compile_options(osc PRIVATE -fsanitize=thread -g -O1) +target_link_options(osc PRIVATE -fsanitize=thread) + + if (UNIX AND NOT APPLE) target_link_libraries(${PROJECT_NAME} PRIVATE dl m) endif() diff --git a/src/envelope.cpp b/src/envelope.cpp index 8e89e26..5fb3dfe 100644 --- a/src/envelope.cpp +++ b/src/envelope.cpp @@ -2,7 +2,6 @@ void Envelope::note_on(){ condition = Condition::Attack; - // note_is_pressed = true; if (attack_time <= 0.0){ attack_time = 0.0001; } @@ -17,7 +16,6 @@ void Envelope::note_off(){ } rate = (0.0 - currnet_amplitude)/(release_time*sample_rate); } - // note_is_pressed = false; } double Envelope::process(){ diff --git a/src/main.cpp b/src/main.cpp index 1afeac9..3244e1d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,50 +9,96 @@ #include "../include/command.hpp" void producer(SpscQueue& queue, std::atomic& producer_done){ - /* - * формирует последовательность команд; - записывает их через queue.push(); - повторяет попытку, если очередь заполнена; - после успешной отправки последней команды устанавливает producer_done = true. - */ - - - + 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 << "110.гц\n"; + std::cout << "440.гц\n"; std::string a = ""; Synth synth; - SpscQueue queue; + // SpscQueue queue; - std::atomic producer_done; + // std::atomic 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(); - // std::thread producer(); - // std::thread consumer(); - - synth.set_frequency(110); + synth.set_frequency(440); synth.set_amplitude(0.5); try{ AudioEngine audio_engine(synth); audio_engine.start(); while (true){ - std::cout << std::boolalpha - << "Lock-free: " << std::atomic{}.is_lock_free() - << '\n'; std::cin >> a; - if (a == "start"){ - synth.note_on(); + 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; } - else synth.note_off(); } - // audio_engine.stop(); } catch (const std::exception& error){ std::cerr << error.what() << "\n"; diff --git a/src/synth.cpp b/src/synth.cpp index 2d84838..39c5857 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -2,19 +2,25 @@ double Synth::generate(){ + int processed = 0; Command command; - while (queue.pop(command)){ + while (processed < 16 && queue.pop(command)){ switch (command){ + default: + break; case Command::None: + ++processed; break; case Command::NoteOn: envelope.note_on(); + ++processed; break; case Command::NoteOff: envelope.note_off(); + ++processed; break; } }