-
Notifications
You must be signed in to change notification settings - Fork 1
/
sampler.h
78 lines (59 loc) · 1.66 KB
/
sampler.h
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 SAMPLER_H
#define SAMPLER
#include <stdint.h>
#define N_(n,b) (n*b)
#define C_(n) N_(n, 261.6)
#define D_(n) N_(n, 293.7)
#define E_(n) N_(n, 329.6)
#define F_(n) N_(n, 349.2)
#define G_(n) N_(n, 392.0)
#define A_(n) N_(n, 440.0)
#define B_(n) N_(n, 493.9)
#define SAMPLE_FREQ 22050
#define NUM_SAMPLERS 128
#define SAMPLE(f, x) (((Sampler)(f))->function(f, x))
#define RELEASE_SAMPLER(f) (((Sampler)(f))->release(f))
#define array_size(a) (sizeof(a)/sizeof(a[0]))
typedef int16_t (*SamplerFunction)(void*, long);
typedef void (*ReleaseSampler)(void*);
void sampler_init();
typedef struct Sampler_ {
SamplerFunction function;
ReleaseSampler release;
long start_sample;
long duration_samples;
} *Sampler;
typedef struct SinSampler_ {
struct Sampler_ sampler;
float phase; /* radians */
float radians_per_sample;
float amp;
} *SinSampler;
Sampler sinsampler_make(long start, long duration,
float freq, float amp, float phase);
typedef struct SawSampler_ {
struct Sampler_ sampler;
long samples_per_period;
long phase_samples;
float slope;
float amp;
} *SawSampler;
Sampler sawsampler_make(long start, long duration,
float freq, float amp, float phase);
#define DURATION(f) (((Sampler)f)->duration_samples)
#define START(f) (((Sampler)f)->start_sample)
#define END(f) (START(f) + DURATION(f))
typedef struct Filter_ {
int na;
int nb;
int xi;
float yi;
float *as;
float *bs;
int16_t *xs;
int16_t *ys;
} *Filter;
Filter filter_make(float* as, int na, float* bs, int nb);
int16_t filter_value(Filter filter, int16_t value);
Filter lowpass_make(float cutoff, float sample_freq);
#endif