Добавлена поддержка мультипотока

на данном этапе имеем:
1. синусоидальный осциллятор
2. ADSR Envelope для осцилятора
3. аудиовывод через miniaudio
4. SPSC Queue для передачи команд между потоками
5. интеграция управляющего и аудиопотока
This commit is contained in:
2026-11-13 22:18:57 +06:00
parent bb258484df
commit 7d60addcd9
4 changed files with 80 additions and 26 deletions
+4
View File
@@ -24,6 +24,10 @@ target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/external ${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) if (UNIX AND NOT APPLE)
target_link_libraries(${PROJECT_NAME} PRIVATE dl m) target_link_libraries(${PROJECT_NAME} PRIVATE dl m)
endif() endif()
-2
View File
@@ -2,7 +2,6 @@
void Envelope::note_on(){ void Envelope::note_on(){
condition = Condition::Attack; condition = Condition::Attack;
// note_is_pressed = true;
if (attack_time <= 0.0){ if (attack_time <= 0.0){
attack_time = 0.0001; attack_time = 0.0001;
} }
@@ -17,7 +16,6 @@ void Envelope::note_off(){
} }
rate = (0.0 - currnet_amplitude)/(release_time*sample_rate); rate = (0.0 - currnet_amplitude)/(release_time*sample_rate);
} }
// note_is_pressed = false;
} }
double Envelope::process(){ double Envelope::process(){
+69 -23
View File
@@ -9,50 +9,96 @@
#include "../include/command.hpp" #include "../include/command.hpp"
void producer(SpscQueue& queue, std::atomic<bool>& producer_done){ void producer(SpscQueue& queue, std::atomic<bool>& producer_done){
/* for (int i = 0; i < 32; ++i){
* формирует последовательность команд; while(!queue.push(Command::NoteOn)){
записывает их через queue.push(); std::this_thread::yield();
повторяет попытку, если очередь заполнена; }
после успешной отправки последней команды устанавливает producer_done = true. 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){ 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(){ int main(){
std::cout << "110.гц\n"; std::cout << "440.гц\n";
std::string a = ""; std::string a = "";
Synth synth; Synth synth;
SpscQueue queue; // SpscQueue queue;
std::atomic<bool> producer_done; // 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();
// std::thread producer(); synth.set_frequency(440);
// std::thread consumer();
synth.set_frequency(110);
synth.set_amplitude(0.5); synth.set_amplitude(0.5);
try{ try{
AudioEngine audio_engine(synth); AudioEngine audio_engine(synth);
audio_engine.start(); audio_engine.start();
while (true){ while (true){
std::cout << std::boolalpha
<< "Lock-free: " << std::atomic<Command>{}.is_lock_free()
<< '\n';
std::cin >> a; std::cin >> a;
if (a == "start"){ if (a == "on"){
synth.note_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){ catch (const std::exception& error){
std::cerr << error.what() << "\n"; std::cerr << error.what() << "\n";
+7 -1
View File
@@ -2,19 +2,25 @@
double Synth::generate(){ double Synth::generate(){
int processed = 0;
Command command; Command command;
while (queue.pop(command)){ while (processed < 16 && queue.pop(command)){
switch (command){ switch (command){
default:
break;
case Command::None: case Command::None:
++processed;
break; break;
case Command::NoteOn: case Command::NoteOn:
envelope.note_on(); envelope.note_on();
++processed;
break; break;
case Command::NoteOff: case Command::NoteOff:
envelope.note_off(); envelope.note_off();
++processed;
break; break;
} }
} }