aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorspl3g <spleefer6@yandex.ru>2025-10-10 17:56:48 +0300
committerspl3g <spleefer6@yandex.ru>2025-10-10 21:56:51 +0300
commit6e79fee0171f41a06389cfed19e050785fd33b93 (patch)
tree3b15ae776e02cb5061a1604738c0d4b3db3687e1 /src
parent75427da62eb579374d4b139084cc48e042830f21 (diff)
Move out the message queue
Diffstat (limited to 'src')
-rw-r--r--src/messages.c47
-rw-r--r--src/messages.h86
-rw-r--r--src/sounds.c46
-rw-r--r--src/sounds.h80
4 files changed, 135 insertions, 124 deletions
diff --git a/src/messages.c b/src/messages.c
new file mode 100644
index 0000000..80998a7
--- /dev/null
+++ b/src/messages.c
@@ -0,0 +1,47 @@
+#include "messages.h"
+
+int mqueue_get(message_queue *q, synth_message *msg) {
+ pthread_mutex_lock(&(q)->lock);
+ if ((q)->tail == (q)->head) {
+ pthread_mutex_unlock(&(q)->lock);
+ return 1;
+ }
+
+ *(msg) = (q)->buffer[(q)->tail];
+ (q)->tail = ((q)->tail + 1) % MESSAGE_QUEUE_SIZE;
+
+ pthread_mutex_unlock(&(q)->lock);
+ return 0;
+}
+
+int mqueue__push_no_lock(message_queue *q, synth_message msg) {
+ size_t next = ((q)->head + 1) % MESSAGE_QUEUE_SIZE;
+
+ if ((q)->tail == next) {
+ return 1;
+ }
+
+ (q)->buffer[(q)->head] = msg;
+ (q)->head = next;
+ return 0;
+}
+
+int mqueue_push(message_queue *q, synth_message msg) {
+ pthread_mutex_lock(&(q)->lock);
+ int ret = mqueue__push_no_lock(q, msg);
+ pthread_mutex_unlock(&(q)->lock);
+ return ret;
+}
+
+int mqueue_push_many(message_queue *q, synth_message *msg, size_t count) {
+ pthread_mutex_lock(&(q)->lock);
+ int ret = 0;
+ for (size_t i = 0; i < count; i++) {
+ ret = mqueue__push_no_lock(q, msg[i]);
+ if (ret != 0) {
+ break;
+ }
+ }
+ pthread_mutex_unlock(&(q)->lock);
+ return ret;
+}
diff --git a/src/messages.h b/src/messages.h
new file mode 100644
index 0000000..6dc0d44
--- /dev/null
+++ b/src/messages.h
@@ -0,0 +1,86 @@
+#ifndef MESSAGE_QUEUE_H_
+#define MESSAGE_QUEUE_H_
+
+#include <stddef.h>
+#include <pthread.h>
+
+#define MESSAGE_QUEUE_SIZE 128
+
+typedef enum {
+ PARAM_OSC,
+ PARAM_VOLUME,
+
+ PARAM_ATTACK,
+ PARAM_DECAY,
+ PARAM_SUSTAIN,
+ PARAM_RELEASE,
+} param_type;
+
+typedef enum {
+ OSC_SINE,
+ OSC_SAW,
+ OSC_SQUARE,
+ OSC_TRIANGLE,
+} oscilator_type;
+
+typedef enum {
+ MSG_NOTE_ON,
+ MSG_NOTE_OFF,
+ MSG_ALL_NOTES_OFF,
+ MSG_PARAM_CHANGE,
+ MSG_STOP,
+} synth_message_type;
+
+typedef struct {
+ synth_message_type type;
+
+ union {
+ // NOTE_ON / NOTE_OFF
+ struct {
+ size_t note_id;
+ } note;
+
+ // SET_PARAM;
+ struct {
+ param_type param_type;
+ float value;
+ } param_change;
+ };
+} synth_message;
+
+typedef struct {
+ synth_message buffer[MESSAGE_QUEUE_SIZE];
+ size_t head;
+ size_t tail;
+ pthread_mutex_t lock;
+} message_queue;
+
+int mqueue_get(message_queue *q, synth_message *msg);
+int mqueue_push(message_queue *q, synth_message msg);
+int mqueue_push_many(message_queue *q, synth_message *msg, size_t count);
+
+#define mqueue_init(q) \
+ do { \
+ (q)->head = (q)->tail = 0; \
+ pthread_mutex_init(&(q)->lock, NULL); \
+ } while (0)
+
+/* #define mqueue_get(q, msg, ok) \ */
+/* do { \ */
+/* pthread_mutex_lock(&(q)->lock); \ */
+/* if ((q)->tail == (q)->head) { \ */
+/* pthread_mutex_unlock(&(q)->lock); \ */
+/* *(ok) = false;\ */
+/* break; \ */
+/* } \ */
+/* \ */
+/* *(msg) = (q)->buffer[(q)->tail]; \ */
+/* (q)->tail = ((q)->tail + 1) % MESSAGE_QUEUE_SIZE; \ */
+/* \ */
+/* pthread_mutex_unlock(&(q)->lock); \ */
+/* *(ok) = true; \ */
+/* } while (0) */
+
+#define mqueue_empty(q) (q)->head == (q)->tail
+
+#endif // MESSAGE_QUEUE_H_
diff --git a/src/sounds.c b/src/sounds.c
index 5445df6..0086f4f 100644
--- a/src/sounds.c
+++ b/src/sounds.c
@@ -1,51 +1,5 @@
#include "sounds.h"
-int mqueue_get(message_queue *q, synth_message *msg) {
- pthread_mutex_lock(&(q)->lock);
- if ((q)->tail == (q)->head) {
- pthread_mutex_unlock(&(q)->lock);
- return 1;
- }
-
- *(msg) = (q)->buffer[(q)->tail];
- (q)->tail = ((q)->tail + 1) % MESSAGE_QUEUE_SIZE;
-
- pthread_mutex_unlock(&(q)->lock);
- return 0;
-}
-
-int mqueue__push_no_lock(message_queue *q, synth_message msg) {
- size_t next = ((q)->head + 1) % MESSAGE_QUEUE_SIZE;
-
- if ((q)->tail == next) {
- return 1;
- }
-
- (q)->buffer[(q)->head] = msg;
- (q)->head = next;
- return 0;
-}
-
-int mqueue_push(message_queue *q, synth_message msg) {
- pthread_mutex_lock(&(q)->lock);
- int ret = mqueue__push_no_lock(q, msg);
- pthread_mutex_unlock(&(q)->lock);
- return ret;
-}
-
-int mqueue_push_many(message_queue *q, synth_message *msg, size_t count) {
- pthread_mutex_lock(&(q)->lock);
- int ret = 0;
- for (size_t i = 0; i < count; i++) {
- ret = mqueue__push_no_lock(q, msg[i]);
- if (ret != 0) {
- break;
- }
- }
- pthread_mutex_unlock(&(q)->lock);
- return ret;
-}
-
float envelope_next(envelope *env) {
float value;
bool next_state = false;
diff --git a/src/sounds.h b/src/sounds.h
index 61346c6..d8c8435 100644
--- a/src/sounds.h
+++ b/src/sounds.h
@@ -7,6 +7,8 @@
#include <pthread.h>
#include <stdbool.h>
+#include "messages.h"
+
#define check(ret) \
do { \
int res = (ret); \
@@ -17,59 +19,9 @@
} \
} while (0)
-#define MESSAGE_QUEUE_SIZE 128
#define SAMPLE_RATE 48000
#define PERIOD_SIZE 480
-typedef enum {
- PARAM_OSC,
- PARAM_VOLUME,
-
- PARAM_ATTACK,
- PARAM_DECAY,
- PARAM_SUSTAIN,
- PARAM_RELEASE,
-} param_type;
-
-typedef enum {
- OSC_SINE,
- OSC_SAW,
- OSC_SQUARE,
- OSC_TRIANGLE,
-} oscilator_type;
-
-typedef enum {
- MSG_NOTE_ON,
- MSG_NOTE_OFF,
- MSG_ALL_NOTES_OFF,
- MSG_PARAM_CHANGE,
- MSG_STOP,
-} synth_message_type;
-
-typedef struct {
- synth_message_type type;
-
- union {
- // NOTE_ON / NOTE_OFF
- struct {
- size_t note_id;
- } note;
-
- // SET_PARAM;
- struct {
- param_type param_type;
- float value;
- } param_change;
- };
-} synth_message;
-
-typedef struct {
- synth_message buffer[MESSAGE_QUEUE_SIZE];
- size_t head;
- size_t tail;
- pthread_mutex_t lock;
-} message_queue;
-
typedef struct {
snd_pcm_t *pcm;
message_queue *queue;
@@ -119,34 +71,6 @@ typedef struct {
typedef float (*oscilator_func)(float phase);
-int mqueue_get(message_queue *q, synth_message *msg);
-int mqueue_push(message_queue *q, synth_message msg);
-int mqueue_push_many(message_queue *q, synth_message *msg, size_t count);
-
-#define mqueue_init(q) \
- do { \
- (q)->head = (q)->tail = 0; \
- pthread_mutex_init(&(q)->lock, NULL); \
- } while (0)
-
-/* #define mqueue_get(q, msg, ok) \ */
-/* do { \ */
-/* pthread_mutex_lock(&(q)->lock); \ */
-/* if ((q)->tail == (q)->head) { \ */
-/* pthread_mutex_unlock(&(q)->lock); \ */
-/* *(ok) = false;\ */
-/* break; \ */
-/* } \ */
-/* \ */
-/* *(msg) = (q)->buffer[(q)->tail]; \ */
-/* (q)->tail = ((q)->tail + 1) % MESSAGE_QUEUE_SIZE; \ */
-/* \ */
-/* pthread_mutex_unlock(&(q)->lock); \ */
-/* *(ok) = true; \ */
-/* } while (0) */
-
-#define mqueue_empty(q) (q)->head == (q)->tail
-
void *sound_thread_start(void *ptr);
int set_hw_params(snd_pcm_t *pcm);