Added a dummy instrument, soundtest can play it.
[cantaveria.git] / seq.c
blob35bf50fc31ca1651314489a17d50b6028fa7c173
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
22 evanrinehart@gmail.com
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include <seq.h>
28 #include <list.h>
29 #include <audio.h>
30 #include <util.h>
32 int tick;
33 int terr;
34 int loop_start;
35 int loop_end;
36 int looping;
38 //event lists
39 list* sequence;
40 list* blank_events;
41 list* immediate_events;
43 event* next_event;
48 void seq_init(){
49 printf(" sequencer: ... ");
51 blank_events = empty();
52 immediate_events = empty();
53 sequence = empty();
55 printf("OK\n");
58 int would_loop(){
59 return looping && next_event->tick > loop_end;
62 event* get_event_after(int tick){
63 return NULL;
66 event* get_next_event(){
67 if(would_loop()){
68 return get_event_after(loop_start);
70 else {
71 return next_event;
77 int get_next_tick(){
78 event* e = get_next_event();
79 return e ? e->tick : -1;
82 int distance_to_next(){
83 int next_tick = get_next_tick();
84 if(next_tick < 0) return -1;
86 return would_loop() ?
87 next_tick - loop_start + loop_end - tick :
88 next_tick - tick;
93 /* below are three functions that the synth uses to
94 control the sequencer. it finds control events, advances
95 the event pointer, and finally advances the tick count */
97 //returns samples from now an event will occur
98 //if no event will occur in sbound samples, returns -1
99 int seq_lookahead(int sbound){
100 return sbound;
102 int tbound = sbound*46080/1323000;
103 int T = distance_to_next();
104 if(T < 0) return -1;
105 return T > tbound ?
106 sbound :
107 T*1 + 0;
110 //returns the next event that would play
111 //if there is no such event, returns NULL
112 event* seq_get_event(){
113 return NULL;
114 //needs to handle looping
118 //advance the tick position
119 void seq_advance(int samples){
120 return;
121 // 46080 1/1323000 ticks = 1 sample
122 int N = 46080 * samples;
123 int D = 1323000;
124 terr += N;
125 tick += N/D + terr/D;
126 terr %= D;
127 //needs to handle looping
131 /* IMPORTANT
132 it might well be simpler to implement looping as
133 an event which exists at the loop point after all
134 other events that occur at that time which sends
135 the sequence to a specific tick */
140 /* *** */
141 event* make_event(int type, int chan, int val1, int val2){
142 event* e;
143 if(blank_events->next){
144 e = pop(blank_events);
146 else{
147 e = xmalloc(sizeof(event));
150 e->type = type;
151 e->chan = chan;
152 e->val1= val1;
153 e->val2= val2;
154 e->tick = 0;
155 return e;
158 void recycle_event(event* e){
159 push(blank_events, e);
163 void seq_instant(int type, int chan, int val1, int val2){
164 event* e = make_event(type, chan, val1, val2);
165 audio_lock();
166 append(immediate_events, e);
167 audio_unlock();
171 void seq_play_sound(int id){
172 seq_instant(0x90, 15, id, 0);
177 event* seq_get_immediate(){
178 event* e = pop(immediate_events);
179 if(e != NULL){
180 recycle_event(e);
181 return e;
183 else{
184 return NULL;