atomic
This commit is contained in:
@@ -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()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef COMMAND
|
||||||
|
#define COMMAND
|
||||||
|
|
||||||
|
enum class Command{
|
||||||
|
None,
|
||||||
|
NoteOn,
|
||||||
|
NoteOff
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef SPSC_QUEUE
|
||||||
|
#define SPSC_QUEUE
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <array>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
#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<Command, 64> buffer;
|
||||||
|
std::atomic<std::size_t> head;
|
||||||
|
std::atomic<std::size_t> tail;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+7
-2
@@ -4,7 +4,11 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <numbers>
|
#include <numbers>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
#include "../include/envelope.hpp"
|
#include "../include/envelope.hpp"
|
||||||
|
#include "../include/spsc_queue.hpp"
|
||||||
|
#include "../include/command.hpp"
|
||||||
|
|
||||||
class Synth {
|
class Synth {
|
||||||
public:
|
public:
|
||||||
@@ -21,8 +25,8 @@ class Synth {
|
|||||||
void set_frequency(double value);
|
void set_frequency(double value);
|
||||||
void set_amplitude(double value);
|
void set_amplitude(double value);
|
||||||
|
|
||||||
void note_on();
|
bool note_on();
|
||||||
void note_off();
|
bool note_off();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/* 1. частота звука в Герцах
|
/* 1. частота звука в Герцах
|
||||||
@@ -31,6 +35,7 @@ class Synth {
|
|||||||
4. фаза. от 0 до 2 пи*/
|
4. фаза. от 0 до 2 пи*/
|
||||||
|
|
||||||
Envelope envelope;
|
Envelope envelope;
|
||||||
|
SpscQueue queue;
|
||||||
|
|
||||||
double frequency;
|
double frequency;
|
||||||
double amplitude;
|
double amplitude;
|
||||||
|
|||||||
+29
-2
@@ -1,24 +1,51 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <thread>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
#include "../include/synth.hpp"
|
#include "../include/synth.hpp"
|
||||||
#include "../include/audio_engine.hpp"
|
#include "../include/audio_engine.hpp"
|
||||||
|
#include "../include/spsc_queue.hpp"
|
||||||
|
#include "../include/command.hpp"
|
||||||
|
|
||||||
|
void producer(SpscQueue& queue, std::atomic<bool>& producer_done){
|
||||||
|
/*
|
||||||
|
* формирует последовательность команд;
|
||||||
|
записывает их через queue.push();
|
||||||
|
повторяет попытку, если очередь заполнена;
|
||||||
|
после успешной отправки последней команды устанавливает producer_done = true.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void consumer(SpscQueue& queue, std::atomic<bool>& producer_done){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
std::cout << "110.гц\n";
|
std::cout << "110.гц\n";
|
||||||
std::string a = "";
|
std::string a = "";
|
||||||
Synth synth;
|
Synth synth;
|
||||||
|
SpscQueue queue;
|
||||||
|
|
||||||
|
std::atomic<bool> producer_done;
|
||||||
|
|
||||||
|
// std::thread producer();
|
||||||
|
// std::thread consumer();
|
||||||
|
|
||||||
synth.set_frequency(110);
|
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 == "start"){
|
||||||
synth.note_on();
|
synth.note_on();
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+21
-5
@@ -2,6 +2,23 @@
|
|||||||
|
|
||||||
|
|
||||||
double Synth::generate(){
|
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));
|
phase = std::fmod(phase, (2*std::numbers::pi));
|
||||||
|
|
||||||
double y = amplitude*std::sin(phase);
|
double y = amplitude*std::sin(phase);
|
||||||
@@ -11,7 +28,6 @@ double Synth::generate(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Synth::generate_buffer(double* array, int size){
|
void Synth::generate_buffer(double* array, int size){
|
||||||
phase = 0.0;
|
|
||||||
for (int i = 0; i < size; ++i){
|
for (int i = 0; i < size; ++i){
|
||||||
array[i] = generate();
|
array[i] = generate();
|
||||||
}
|
}
|
||||||
@@ -29,10 +45,10 @@ void Synth::set_amplitude(double value){
|
|||||||
else amplitude = value;
|
else amplitude = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Synth::note_on(){
|
bool Synth::note_on(){
|
||||||
envelope.note_on();
|
return queue.push(Command::NoteOn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Synth::note_off(){
|
bool Synth::note_off(){
|
||||||
envelope.note_off();
|
return queue.push(Command::NoteOff);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user