diff --git a/CMakeLists.txt b/CMakeLists.txt index e69de29..12dd077 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 4.4.5) + +project (osc VERSION 0.1 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +find_package(Threads REQUIRED) + +set(SOURCES + src/main.cpp + src/audio_engine.cpp + src/envelope.cpp + src/miniaudio.cpp + src/spsc_queue.cpp + src/synth.cpp +) + +add_executable(${PROJECT_NAME} ${SOURCES}) + +target_include_directories(${PROJECT_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}/external +) + +if (UNIX AND NOT APPLE) + target_link_libraries(${PROJECT_NAME} PRIVATE dl m) +endif() + diff --git a/include/command.hpp b/include/command.hpp new file mode 100644 index 0000000..a14679f --- /dev/null +++ b/include/command.hpp @@ -0,0 +1,10 @@ +#ifndef COMMAND +#define COMMAND + +enum class Command{ + None, + NoteOn, + NoteOff +}; + +#endif diff --git a/include/spsc_queue.hpp b/include/spsc_queue.hpp new file mode 100644 index 0000000..8c1e470 --- /dev/null +++ b/include/spsc_queue.hpp @@ -0,0 +1,29 @@ +#ifndef SPSC_QUEUE +#define SPSC_QUEUE + +#include +#include +#include + +#include "../include/command.hpp" + +class SpscQueue{ + + public: + SpscQueue(){ + head.store(0, std::memory_order_relaxed); + tail.store(0, std::memory_order_relaxed); + buffer.fill(Command::None); + } + + bool push(Command command); + bool pop(Command& command); + + + private: + std::array buffer; + std::atomic head; + std::atomic tail; +}; + +#endif diff --git a/include/synth.hpp b/include/synth.hpp index 52227fa..ea7cd4b 100644 --- a/include/synth.hpp +++ b/include/synth.hpp @@ -4,7 +4,11 @@ #include #include #include +#include + #include "../include/envelope.hpp" +#include "../include/spsc_queue.hpp" +#include "../include/command.hpp" class Synth { public: @@ -21,8 +25,8 @@ class Synth { void set_frequency(double value); void set_amplitude(double value); - void note_on(); - void note_off(); + bool note_on(); + bool note_off(); private: /* 1. частота звука в Герцах @@ -31,6 +35,7 @@ class Synth { 4. фаза. от 0 до 2 пи*/ Envelope envelope; + SpscQueue queue; double frequency; double amplitude; diff --git a/src/main.cpp b/src/main.cpp index c5ac988..1afeac9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,24 +1,51 @@ #include #include +#include +#include #include "../include/synth.hpp" #include "../include/audio_engine.hpp" +#include "../include/spsc_queue.hpp" +#include "../include/command.hpp" +void producer(SpscQueue& queue, std::atomic& producer_done){ + /* + * формирует последовательность команд; + записывает их через queue.push(); + повторяет попытку, если очередь заполнена; + после успешной отправки последней команды устанавливает producer_done = true. + */ + + + +} + +void consumer(SpscQueue& queue, std::atomic& producer_done){ + +} int main(){ std::cout << "110.гц\n"; std::string a = ""; Synth synth; + SpscQueue queue; + + std::atomic producer_done; + + // std::thread producer(); + // std::thread consumer(); + synth.set_frequency(110); 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(); diff --git a/src/spsc_queue.cpp b/src/spsc_queue.cpp new file mode 100644 index 0000000..5d1af30 --- /dev/null +++ b/src/spsc_queue.cpp @@ -0,0 +1,27 @@ +#include "../include/spsc_queue.hpp" + +bool SpscQueue::push(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()){ + return false; + } + else{ + buffer[current_head % buffer.size()] = command; + head.store(++current_head, std::memory_order_release); + return true; + } +} + +bool SpscQueue::pop(Command& command){ + std::size_t current_head = head.load(std::memory_order_acquire); + std::size_t current_tail = tail.load(std::memory_order_relaxed); + if (current_head == current_tail){ + return false; + } + else{ + command = buffer[current_tail % buffer.size()]; + tail.store(++current_tail, std::memory_order_release); + return true; + } +} diff --git a/src/synth.cpp b/src/synth.cpp index d27f092..2d84838 100644 --- a/src/synth.cpp +++ b/src/synth.cpp @@ -2,6 +2,23 @@ double Synth::generate(){ + Command command; + + while (queue.pop(command)){ + switch (command){ + case Command::None: + break; + + case Command::NoteOn: + envelope.note_on(); + break; + + case Command::NoteOff: + envelope.note_off(); + break; + } + } + phase = std::fmod(phase, (2*std::numbers::pi)); double y = amplitude*std::sin(phase); @@ -11,7 +28,6 @@ double Synth::generate(){ } void Synth::generate_buffer(double* array, int size){ - phase = 0.0; for (int i = 0; i < size; ++i){ array[i] = generate(); } @@ -29,10 +45,10 @@ void Synth::set_amplitude(double value){ else amplitude = value; } -void Synth::note_on(){ - envelope.note_on(); +bool Synth::note_on(){ + return queue.push(Command::NoteOn); } -void Synth::note_off(){ - envelope.note_off(); +bool Synth::note_off(){ + return queue.push(Command::NoteOff); }