forked from sjlongland/atinysynth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequencer.h
109 lines (93 loc) · 3.43 KB
/
sequencer.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
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
105
106
107
108
109
/*!
* Stream sequencer Polyphonic synthesizer for microcontrollers.
* (C) 2021 Luciano Martorella
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#ifndef _SEQUENCER_H
#define _SEQUENCER_H
#include <stdint.h>
/*!
* Define a single step/frame of the sequencer. It applies to the active channel.
* Contains the definition of the next waveform and envelope.
*/
struct seq_frame_t {
/*! ADSR time-scale - 1 (avoid a 16bit decrement at runtime) */
uint16_t adsr_time_scale_1;
/*! Waveform full period as `sample_freq / frequency`, or zero for pauses */
uint16_t wf_period;
/*! Waveform amplitude */
int8_t wf_amplitude;
/*! When the release period starts, time units over the ADSR_TIME_UNITS scale */
uint8_t adsr_release_start;
};
/*!
* Plays a stream sequence of frames, in the order requested by the synth.
* The frames must then be sorted in the same fetch order and not in channel order.
* Frames will be fed using the handler passed by `new_frame_require`.
*/
void seq_play_stream(uint8_t voices);
/*! Requires a new frame. The call never fails. Returns a zero frame at the end of the stream, or if EOF */
extern struct seq_frame_t seq_buf_frame;
/*! Set at the stream end */
extern uint8_t seq_end;
/*! Requires a new frame to be written in `seq_buf_frame`. The call never fails. */
void new_frame_require();
/*!
* Use it when `seq_play_stream` is in use, must be called at every sample
* It call `poly_synth_next` internally.
*/
int8_t seq_feed_synth();
/*! List of frames, used by `seq_frame_map_t` */
struct seq_frame_list_t {
/*! Frame count */
int count;
/*! List of frames */
struct seq_frame_t* frames;
};
/*!
* A frame map is an intermediate data in which frames are organized by channel.
* Typically requires dynamic memory allocation (heap) to allocate the whole tune
* upfront.
*/
struct seq_frame_map_t {
/*! Channel count */
int channel_count;
/*! Array of frame lists */
struct seq_frame_list_t* channels;
};
/*! Compile/reorder a frame-map (by channel) to a sequential stream */
void seq_compile(struct seq_frame_map_t* map, struct seq_frame_t** frame_stream, int* frame_count, int* voice_count, int* do_clip_check);
/*! Free the stream allocated by `seq_compile`. */
void seq_free(struct seq_frame_t* seq_frame_stream);
struct ref_map_t {
int count;
int* values;
int bit_count;
};
struct bit_stream_t {
struct ref_map_t refs_adsr_time_scale;
struct ref_map_t refs_wf_period;
struct ref_map_t refs_wf_amplitude;
struct ref_map_t refs_adsr_release_start;
uint8_t* data;
int data_size;
};
/*! Compress the frame stream to bit-stream */
int stream_compress(struct seq_frame_t* frame_stream, int frame_count, struct bit_stream_t* stream);
/*! Free the stream */
void stream_free(struct bit_stream_t* stream);
#endif