aboutsummaryrefslogtreecommitdiff
path: root/src/sounds.h
blob: 639ce806af5919d72df26f1a79f5b0a48e2c52d4 (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
#ifndef SOUNDS_H_
#define SOUNDS_H_

#include <alsa/asoundlib.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>

#include "messages.h"
#include "midi_freqs.h"
#include "defines.h"

#define check(ret)                                                             \
  do {                                                                         \
    int res = (ret);                                                           \
    if (res < 0) {                                                             \
      fprintf(stderr, "%s:%d ERROR: %s (%d)\n", __FILE__, __LINE__,            \
              snd_strerror(res), res);                                         \
      exit(1);                                                                 \
    }                                                                          \
  } while (0)

typedef struct {
  snd_pcm_t *pcm;
  MessageQueue *queue;
  WaveData *wave_data;
} SoundThreadMeta;

typedef enum {
  ENV_OFF,
  ENV_ATTACK,
  ENV_DECAY,
  ENV_SUSTAIN,
  ENV_RELEASE,
} EnvelopeState;

typedef struct {
  int attack_time;
  int decay_time;
  float sustain_level;
  int release_time;
} EnvelopeParams;

typedef struct {
  EnvelopeState state;
  int counter;
  float current_inc;
  float release_value;
  EnvelopeParams params;
} Envelope;

typedef struct {
  bool active;
  float freq;
  float phase;
  float phase_inc;
  Envelope envelope;
} SynthVoice;

typedef struct {
  SynthVoice *buffer;
  size_t size;
} SynthVoices;

typedef struct {
  OscilatorType oscilator_type;
  float master_volume;
  EnvelopeParams envelope_params;
  float last_freq;
} SynthParams;

typedef float (*OscilatorFunc)(float phase);

void *sound_thread_start(void *ptr);
int set_hw_params(snd_pcm_t *pcm);

#endif // SOUNDS_H_