aboutsummaryrefslogtreecommitdiff
path: root/src/messages.h
blob: ff9784c112cc974d445e55f0238c07b9e7f35b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef MESSAGE_QUEUE_H_
#define MESSAGE_QUEUE_H_

#include <stddef.h>
#include <pthread.h>
#include <stdatomic.h>
#include "defines.h"

typedef enum {
  PARAM_OSC,
  PARAM_VOLUME,

  PARAM_ATTACK,
  PARAM_DECAY,
  PARAM_SUSTAIN,
  PARAM_RELEASE,
} ParamType;

typedef enum {
  OSC_SINE,
  OSC_SAW,
  OSC_SQUARE,
  OSC_TRIANGLE,
} OscilatorType;

typedef enum {
  MSG_NOTE_ON,
  MSG_NOTE_OFF,
  MSG_ALL_NOTES_OFF,
  MSG_PARAM_CHANGE,
  MSG_STOP,
} SynthMessageType;

typedef struct {
  SynthMessageType type;

  union {
	// NOTE_ON / NOTE_OFF
	struct {
	  size_t note_id;
	} note;

	// SET_PARAM;
	struct {
	  ParamType param_type;
	  float value;
	} param_change;
  };
} SynthMessage;

typedef struct {
  SynthMessage buffer[MESSAGE_QUEUE_SIZE];
  size_t head;
  size_t tail;
  pthread_mutex_t lock;
} MessageQueue;

typedef struct {
  float freq;
  float buffers[2][DISPLAY_SAMPLES];
  atomic_int write_index;
} WaveData;

int mqueue_get(MessageQueue *q, SynthMessage *msg);
int mqueue_push(MessageQueue *q, SynthMessage msg);
int mqueue_push_many(MessageQueue *q, SynthMessage *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_