Merge branch 'list' into stage
[cantaveria.git] / seq.c
blobf72931701390725faf53e52e93b91a4fd1ec8550
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 <stdlib.h>
26 #include <seq.h>
28 static struct {
29 int tick;
30 int terr; // number 1/1323000 ticks
32 int loop_start;
33 int loop_end;
34 int looping;
36 event* next_event;
37 event* events;
38 } my;
40 void seq_init(){
44 int would_loop(){
45 return my.looping && my.next_event->tick > my.loop_end;
48 event* get_event_after(int tick){
49 return NULL;
52 event* get_next_event(){
53 if(would_loop()){
54 return get_event_after(my.loop_start);
56 else {
57 return my.next_event;
63 int get_next_tick(){
64 event* e = get_next_event();
65 return e ? e->tick : -1;
68 int distance_to_next(){
69 int next_tick = get_next_tick();
70 if(next_tick < 0) return -1;
72 return would_loop() ?
73 next_tick - my.loop_start + my.loop_end - my.tick :
74 next_tick - my.tick;
79 /* below are three functions that the synth uses to
80 control the sequencer. it finds control events, advances
81 the event pointer, and finally advances the tick count */
83 //returns samples from now an event will occur
84 //if no event will occur in sbound samples, returns -1
85 int seq_lookahead(int sbound){
86 return -1;
87 int tbound = sbound*46080/1323000;
88 int T = distance_to_next();
89 if(T < 0) return -1;
90 return T > tbound ?
91 -1 :
92 T*1 + 0;
95 //returns the next event that would play
96 //if there is no such event, returns NULL
97 event* seq_get_event(){
98 return NULL;
99 //needs to handle looping
103 //advance the tick position
104 void seq_advance(int samples){
105 return;
106 // 46080 1/1323000 ticks = 1 sample
107 int N = 46080 * samples;
108 int D = 1323000;
109 my.terr += N;
110 my.tick += N/D + my.terr/D;
111 my.terr %= D;
112 //needs to handle looping
116 /* IMPORTANT
117 it might well be simpler to implement looping as
118 an event which exists at the loop point after all
119 other events that occur at that time which sends
120 the sequence to a specific tick */