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
45 list
* immediate_events
;
48 list
* event_after_loop
;
52 void advance_event(event
* now
){
53 if(now
->type
== 0x100 && looping
){
54 next_event
= event_after_loop
;
58 next_event
= next_event
->next
;
62 event
* dequeue_event(){
63 if(next_event
== NULL
) return NULL
;
65 event
* e
= next_event
->item
;
69 if(e
->type
== EVX_TEMPOCHANGE
){
70 //printf("tempo change to %d bpm\n", e->val1);
74 if(e
->type
== EVX_TICKSPERBEAT
){
75 //printf("setting tpb to %d\n", e->val1);
78 //printf("event (%d, %03x, %d, %d, %d)\n", e->tick, e->type, e->chan, e->val1, e->val2);
87 int samples_until_next(int max
){
91 if(next_event
== NULL
) return max
;
93 event
* e
= next_event
->item
;
94 if(e
->tick
- tick
> max
* D
/ N
) return max
;
96 int d
= (e
->tick
-tick
)*N
/D
;
97 return d
< max
? d
: max
;
100 event
* seq_advance(int max
, int* used
){
101 int N
= bpm
*tpb
; // bpm * tpb
102 int D
= 2646000; // srate * 60
104 int u
= samples_until_next(max
);
116 return dequeue_event();
123 static event
* make_event(int type
, int chan
, int val1
, int val2
){
125 if(blank_events
->next
){
126 e
= pop(blank_events
);
129 e
= xmalloc(sizeof(event
));
140 void recycle_event(event
* e
){
141 push(blank_events
, e
);
145 void seq_instant(int type
, int chan
, int val1
, int val2
){
146 event
* e
= make_event(type
, chan
, val1
, val2
);
148 append(immediate_events
, e
);
153 void seq_play_sound(int id
){
154 seq_instant(0x90, 15, id
, 0);
159 event
* seq_get_immediate(){
160 event
* e
= pop(immediate_events
);
172 void seq_enqueue(int when
, int type
, int chan
, int val1
, int val2
){
173 event
* e
= make_event(type
, chan
, val1
, val2
);
179 list
* ptr
= sequence
->next
;
189 void seq_append(event
* e
){
194 printf(" sequencer: ... ");
196 blank_events
= empty();
197 immediate_events
= empty();
204 next_event
= sequence
->next
;
208 void seq_load(list
* events
){
213 next_event
= sequence
->next
;
214 event_after_loop
= next_event
;
215 looping
= 0; /* fixme */
220 void seq_seek(list
* target
){
221 event
* e
= target
->item
;
237 /* needs to enqueue an 'all off' instant event */