An event_id has been added to all events, to ensure an event with
[ahxm.git] / song.c
blob19d7983ca1385a7220aedbba07034bfca29d899c
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 Angel Ortega <angel@triptico.com>
6 song.c - Device-independent song event stream management
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
33 #include "annhell.h"
35 /*******************
36 Data
37 ********************/
39 /* the song event stream */
40 union song_ev * song = NULL;
42 /* number of song events */
43 int n_song_ev = 0;
45 /* solo track (-1, no solo) */
46 int solo_track = -1;
48 /*******************
49 Code
50 ********************/
52 /**
53 * song_clear - Clears the song stream
55 * Clears the song stream.
57 void song_clear(void)
59 if(song != NULL)
61 free(song);
62 song=NULL;
65 n_song_ev=0;
69 /**
70 * add_song_ev - Adds a song event to the song stream
71 * @ev: the event
73 * Adds a song event to the song stream.
75 void add_song_ev(union song_ev * ev)
77 /* skip tracks if a solo is requested */
78 if(solo_track != -1 && ev->generic.trk_id >= 0 &&
79 ev->generic.trk_id != solo_track)
80 return;
82 /* reallocs */
83 song=(union song_ev *)realloc(song,
84 (n_song_ev + 1) * sizeof(union song_ev));
86 ev->generic.event_id = n_song_ev;
88 /* store */
89 memcpy(&song[n_song_ev], ev, sizeof(union song_ev));
91 n_song_ev++;
95 static int song_ev_cmp(const void * v1, const void * v2)
97 struct song_ev_generic * e1;
98 struct song_ev_generic * e2;
99 int ret;
101 e1=(struct song_ev_generic *)v1; e2=(struct song_ev_generic *)v2;
103 ret = (int) ((e1->time * 10000.0) - (e2->time * 10000.0));
105 /* same time? order by type of event */
106 if(ret == 0)
107 ret = e1->type - e2->type;
109 /* same time and same event? order by event id */
110 if(ret == 0)
111 ret = e1->event_id - e2->event_id;
113 return(ret);
118 * song_sort - Sorts the song stream
120 * Sorts the song stream.
122 void song_sort(void)
124 qsort(song, n_song_ev, sizeof(union song_ev), song_ev_cmp);
129 * song_test_measure_boundary - Does a measure boundary check
130 * @ev_time: event time
131 * @num: meter numerator
132 * @den: meter denominator
134 * Does a measure boundary check. Returns 0 if the event time falls
135 * exactly between two measures, or nonzero otherwise.
137 int song_test_measure_boundary(double ev_time, int num, int den, int line)
139 int ret;
141 if((ret=((int)(ev_time * (double) den)) % num))
142 printf("Measure boundary check failed in line %d\n", line);
144 return(ret);