summaryrefslogtreecommitdiffstats
path: root/src/synth_engine.h
blob: a434b53e6deb8299a556a57d11464a52156b706c (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef SYNTH_ENGINE_H
#define SYNTH_ENGINE_H

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <portaudio.h>

#include "notes.h"
#include "filter.h"
#include "adsr.h"

//#define SAMPLE_RATE   (44100)
#define SAMPLE_RATE   (48000)
#define FRAMES_PER_BUFFER  (210)

#define VIZ_BUF 1024

#define WIDTH 1024
#define HEIGHT 600

#define MIDI_NOTES 128

#ifndef M_PI
#define M_PI  (3.14159265)
#endif

typedef struct lfo_t {
  float freq;
  float amp;
  unsigned long long elapsed;
} lfo_t;

typedef struct note_t {
  float freq;
  PaTime noteOn;
  PaTime noteOff;
  int key;
  unsigned long long elapsed;
} note_t;

typedef struct midi_note_t {
  float freq;
  int channel;
  PaTime noteOn;
  PaTime noteOff;
  float velocity; // normalized
  unsigned long long elapsed;
  adsr_t * adsr;
  int active;
} midi_note_t;

typedef struct viz_t {
  int sample_rate_divider;
  float wave[VIZ_BUF];
  int wi;
} viz_t;

typedef struct {
  PaStream *stream;
  
  float freq_offset;
  float gain;
  float x;

  midi_note_t midi_note[MIDI_NOTES];

  note_t n;
  adsr_t adsr;

  lfo_t lfo;

  int octave;

  int poly;
  int multi;
  int filter;
  int clamp;

  float (*gen[4]) (float freq, unsigned long long phase, float x, unsigned int sample_rate);
  int geni;

  float cutoff;
  float resonance;

  BWLowPass* fff;
  BWBandStop* fff2;

  int active;
  
  viz_t viz;
} synth_t;

float make_sample(unsigned long long phase, void *synthData, unsigned int sample_rate, int viz);
void init_synth(synth_t * synth);
void free_synth(synth_t * synth);
int sound_gen(const void *inputBuffer,
              void *outputBuffer,
              unsigned long framesPerBuffer,
              const PaStreamCallbackTimeInfo* timeInfo,
              PaStreamCallbackFlags statusFlags,
              void *synthData);

#endif /* SYNTH_ENGINE_H */