.
This commit is contained in:
+9
-1
@@ -1,10 +1,18 @@
|
|||||||
#ifndef COMMAND
|
#ifndef COMMAND
|
||||||
#define COMMAND
|
#define COMMAND
|
||||||
|
|
||||||
enum class Command{
|
#include <cstdint>
|
||||||
|
|
||||||
|
enum class CommandType{
|
||||||
None,
|
None,
|
||||||
NoteOn,
|
NoteOn,
|
||||||
NoteOff
|
NoteOff
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Command{
|
||||||
|
CommandType type;
|
||||||
|
uint8_t note;
|
||||||
|
uint8_t velocity;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ class SpscQueue{
|
|||||||
SpscQueue(){
|
SpscQueue(){
|
||||||
head.store(0, std::memory_order_relaxed);
|
head.store(0, std::memory_order_relaxed);
|
||||||
tail.store(0, std::memory_order_relaxed);
|
tail.store(0, std::memory_order_relaxed);
|
||||||
buffer.fill(Command::None);
|
buffer.fill({CommandType::None, 0, 0});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool push(Command command);
|
bool push(const Command&);
|
||||||
bool pop(Command& command);
|
bool pop(Command&);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
+4
-3
@@ -13,7 +13,7 @@
|
|||||||
class Synth {
|
class Synth {
|
||||||
public:
|
public:
|
||||||
Synth(){
|
Synth(){
|
||||||
frequency = 440;
|
frequency = 440.0;
|
||||||
amplitude = 0.5;
|
amplitude = 0.5;
|
||||||
sample_rate = 48000;
|
sample_rate = 48000;
|
||||||
phase = 0.0;
|
phase = 0.0;
|
||||||
@@ -24,9 +24,10 @@ class Synth {
|
|||||||
void generate_buffer(double* array, int size);
|
void generate_buffer(double* array, int size);
|
||||||
void set_frequency(double value);
|
void set_frequency(double value);
|
||||||
void set_amplitude(double value);
|
void set_amplitude(double value);
|
||||||
|
void set_midi_note(uint8_t note);
|
||||||
|
|
||||||
bool note_on();
|
bool note_on(uint8_t note, uint8_t velocity);
|
||||||
bool note_off();
|
bool note_off(uint8_t note);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/* 1. частота звука в Герцах
|
/* 1. частота звука в Герцах
|
||||||
|
|||||||
+97
-81
@@ -8,62 +8,78 @@
|
|||||||
#include "../include/spsc_queue.hpp"
|
#include "../include/spsc_queue.hpp"
|
||||||
#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){
|
// for (int i = 0; i < 32; ++i){
|
||||||
while(!queue.push(Command::NoteOn)){
|
// while(!queue.push(Command::NoteOn)){
|
||||||
std::this_thread::yield();
|
// std::this_thread::yield();
|
||||||
}
|
// }
|
||||||
while(!queue.push(Command::NoteOff)){
|
// while(!queue.push(Command::NoteOff)){
|
||||||
std::this_thread::yield();
|
// std::this_thread::yield();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
producer_done.store(true, std::memory_order_release);
|
// 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 command;
|
||||||
Command expected = Command::NoteOn;
|
// Command expected = Command::NoteOn;
|
||||||
int counter = 0;
|
// int counter = 0;
|
||||||
|
//
|
||||||
while (true){
|
// while (true){
|
||||||
if (!queue.pop(command)){
|
// if (!queue.pop(command)){
|
||||||
if (producer_done.load(std::memory_order_acquire)) break;
|
// if (producer_done.load(std::memory_order_acquire)) break;
|
||||||
else{
|
// else{
|
||||||
std::this_thread::yield();
|
// std::this_thread::yield();
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else{
|
// else{
|
||||||
++counter;
|
// ++counter;
|
||||||
if (command != expected){
|
// if (command != expected){
|
||||||
std::cerr << "команда из очереди не совпадает с ожидаемой";
|
// std::cerr << "команда из очереди не совпадает с ожидаемой";
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
switch(command){
|
// switch(command){
|
||||||
case Command::NoteOn: {
|
// case Command::NoteOn: {
|
||||||
expected = Command::NoteOff;
|
// expected = Command::NoteOff;
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
case Command::NoteOff: {
|
// case Command::NoteOff: {
|
||||||
expected = Command::NoteOn;
|
// expected = Command::NoteOn;
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
case Command::None: {
|
// case Command::None: {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
std::cout << counter;
|
// std::cout << counter;
|
||||||
}
|
// }
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
std::cout << "440.гц\n";
|
// std::cout << "440.гц\n";
|
||||||
std::string a = "";
|
// std::string a = "";
|
||||||
Synth synth;
|
// Synth synth;
|
||||||
// SpscQueue queue;
|
|
||||||
|
Command command;
|
||||||
|
SpscQueue queue;
|
||||||
|
|
||||||
|
for (int i = 0; i < 33; ++i){
|
||||||
|
std::cout << queue.push({CommandType::NoteOn, 60, 100}) << "\n";
|
||||||
|
std::cout << queue.push({CommandType::NoteOn, 20, 80}) << "\n";
|
||||||
|
}
|
||||||
|
std::cout << "выгрузка\n";
|
||||||
|
for (int i = 0; i < 65; ++i){
|
||||||
|
std::cout << queue.pop(command) << "\n";
|
||||||
|
|
||||||
|
}
|
||||||
|
// std::cout << static_cast<int>(command.type) << "\n";
|
||||||
|
// std::cout << static_cast<int>(command.note)<< "\n";
|
||||||
|
// std::cout << static_cast<int>(command.velocity) << "\n";
|
||||||
|
|
||||||
|
|
||||||
// std::atomic<bool> producer_done{false};
|
// std::atomic<bool> producer_done{false};
|
||||||
//
|
//
|
||||||
@@ -76,34 +92,34 @@ int main(){
|
|||||||
// thread1.join();
|
// thread1.join();
|
||||||
// thread2.join();
|
// thread2.join();
|
||||||
|
|
||||||
synth.set_frequency(440);
|
// synth.set_frequency(440);
|
||||||
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::cin >> a;
|
// std::cin >> a;
|
||||||
if (a == "on"){
|
// if (a == "on"){
|
||||||
if (!synth.note_on()){
|
// if (!synth.note_on()){
|
||||||
throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
|
// throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else if (a == "off"){
|
// else if (a == "off"){
|
||||||
if (!synth.note_off()){
|
// if (!synth.note_off()){
|
||||||
throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
|
// throw std::runtime_error("ошибка: не удалось воспроизвести ноту");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else if (a == "exit"){
|
// else if (a == "exit"){
|
||||||
audio_engine.stop();
|
// audio_engine.stop();
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
catch (const std::exception& error){
|
// catch (const std::exception& error){
|
||||||
std::cerr << error.what() << "\n";
|
// std::cerr << error.what() << "\n";
|
||||||
return 1;
|
// return 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#include "../include/spsc_queue.hpp"
|
#include "../include/spsc_queue.hpp"
|
||||||
|
|
||||||
bool SpscQueue::push(Command command){
|
bool SpscQueue::push(const Command& command){
|
||||||
std::size_t current_head = head.load(std::memory_order_relaxed);
|
std::size_t current_head = head.load(std::memory_order_relaxed);
|
||||||
std::size_t current_tail = tail.load(std::memory_order_acquire);
|
std::size_t current_tail = tail.load(std::memory_order_acquire);
|
||||||
if ((current_head - current_tail) >= buffer.size()){
|
if ((current_head - current_tail) >= buffer.size()){
|
||||||
|
|||||||
+12
-8
@@ -6,19 +6,19 @@ double Synth::generate(){
|
|||||||
Command command;
|
Command command;
|
||||||
|
|
||||||
while (processed < 16 && queue.pop(command)){
|
while (processed < 16 && queue.pop(command)){
|
||||||
switch (command){
|
switch (command.type){
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
case Command::None:
|
case CommandType::None:
|
||||||
++processed;
|
++processed;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Command::NoteOn:
|
case CommandType::NoteOn:
|
||||||
envelope.note_on();
|
envelope.note_on();
|
||||||
++processed;
|
++processed;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Command::NoteOff:
|
case CommandType::NoteOff:
|
||||||
envelope.note_off();
|
envelope.note_off();
|
||||||
++processed;
|
++processed;
|
||||||
break;
|
break;
|
||||||
@@ -51,10 +51,14 @@ void Synth::set_amplitude(double value){
|
|||||||
else amplitude = value;
|
else amplitude = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Synth::note_on(){
|
void Synth::set_midi_note(uint8_t note){
|
||||||
return queue.push(Command::NoteOn);
|
set_frequency(440.0*std::pow(2.0,( (static_cast<int>(note)-69)/12.0 )));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Synth::note_off(){
|
bool Synth::note_on(uint8_t note, uint8_t velocity){
|
||||||
return queue.push(Command::NoteOff);
|
return queue.push({CommandType::NoteOn, note, velocity});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Synth::note_off(uint8_t note){
|
||||||
|
return queue.push({CommandType::NoteOff, note, 0});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user