smflite
view src/smf.c @ 0:4264abea8b06
smf-lite initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Jan 2012 11:25:11 +0200 |
parents | |
children |
line source
1 /*-
2 * Copyright (c) 2007, 2008 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * ALTHOUGH THIS SOFTWARE IS MADE OF WIN AND SCIENCE, IT IS PROVIDED BY THE
15 * AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
28 /**
29 * \file
30 *
31 * Various functions.
32 *
33 */
35 /* Reference: http://www.borg.com/~jglatt/tech/midifile.htm */
37 #include <stdlib.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <math.h>
41 #include <errno.h>
42 #ifdef __MINGW32__
43 #include <windows.h>
44 #else /* ! __MINGW32__ */
45 #include <arpa/inet.h>
46 #endif /* ! __MINGW32__ */
47 #include "smf.h"
48 #include "smf_private.h"
50 /**
51 * Allocates new smf_t structure.
52 * \return pointer to smf_t or NULL.
53 */
54 smf_t *
55 smf_new(void)
56 {
57 int cantfail;
59 smf_t *smf = malloc(sizeof(smf_t));
60 if (smf == NULL) {
61 fg_critical("Cannot allocate smf_t structure: %s", strerror(errno));
62 return (NULL);
63 }
65 memset(smf, 0, sizeof(smf_t));
67 smf->tracks_array = fg_ptr_array_new();
68 assert(smf->tracks_array);
70 smf->tempo_array = fg_ptr_array_new();
71 assert(smf->tempo_array);
73 cantfail = smf_set_ppqn(smf, 120);
74 assert(!cantfail);
76 cantfail = smf_set_format(smf, 0);
77 assert(!cantfail);
79 smf_init_tempo(smf);
81 return (smf);
82 }
84 /**
85 * Frees smf and all it's descendant structures.
86 */
87 void
88 smf_delete(smf_t *smf)
89 {
90 /* Remove all the tracks, from last to first. */
91 while (smf->tracks_array->len > 0)
92 smf_track_delete(fg_ptr_array_index(smf->tracks_array, smf->tracks_array->len - 1));
94 smf_fini_tempo(smf);
96 assert(smf->tracks_array->len == 0);
97 assert(smf->number_of_tracks == 0);
98 fg_ptr_array_free(smf->tracks_array, TRUE);
99 fg_ptr_array_free(smf->tempo_array, TRUE);
101 memset(smf, 0, sizeof(smf_t));
102 free(smf);
103 }
105 /**
106 * Allocates new smf_track_t structure.
107 * \return pointer to smf_track_t or NULL.
108 */
109 smf_track_t *
110 smf_track_new(void)
111 {
112 smf_track_t *track = malloc(sizeof(smf_track_t));
113 if (track == NULL) {
114 fg_critical("Cannot allocate smf_track_t structure: %s", strerror(errno));
115 return (NULL);
116 }
118 memset(track, 0, sizeof(smf_track_t));
119 track->next_event_number = -1;
121 track->events_array = fg_ptr_array_new();
122 assert(track->events_array);
124 return (track);
125 }
127 /**
128 * Detaches track from its smf and frees it.
129 */
130 void
131 smf_track_delete(smf_track_t *track)
132 {
133 assert(track);
134 assert(track->events_array);
136 /* Remove all the events, from last to first. */
137 while (track->events_array->len > 0)
138 smf_event_delete(fg_ptr_array_index(track->events_array, track->events_array->len - 1));
140 if (track->smf)
141 smf_track_remove_from_smf(track);
143 assert(track->events_array->len == 0);
144 assert(track->number_of_events == 0);
145 fg_ptr_array_free(track->events_array, TRUE);
147 memset(track, 0, sizeof(smf_track_t));
148 free(track);
149 }
152 /**
153 * Appends smf_track_t to smf.
154 */
155 void
156 smf_add_track(smf_t *smf, smf_track_t *track)
157 {
158 int cantfail;
160 assert(track->smf == NULL);
162 track->smf = smf;
163 fg_ptr_array_add(smf->tracks_array, track);
165 smf->number_of_tracks++;
166 track->track_number = smf->number_of_tracks;
168 if (smf->number_of_tracks > 1) {
169 cantfail = smf_set_format(smf, 1);
170 assert(!cantfail);
171 }
172 }
174 /**
175 * Detaches track from the smf.
176 */
177 void
178 smf_track_remove_from_smf(smf_track_t *track)
179 {
180 int i, j;
181 smf_track_t *tmp;
182 smf_event_t *ev;
184 assert(track->smf != NULL);
186 track->smf->number_of_tracks--;
188 assert(track->smf->tracks_array);
189 fg_ptr_array_remove(track->smf->tracks_array, track);
191 /* Renumber the rest of the tracks, so they are consecutively numbered. */
192 for (i = track->track_number; i <= track->smf->number_of_tracks; i++) {
193 tmp = smf_get_track_by_number(track->smf, i);
194 tmp->track_number = i;
196 /*
197 * Events have track numbers too. I guess this wasn't a wise
198 * decision. ;-/
199 */
200 for (j = 1; j <= tmp->number_of_events; j++) {
201 ev = smf_track_get_event_by_number(tmp, j);
202 ev->track_number = i;
203 }
204 }
206 track->track_number = -1;
207 track->smf = NULL;
208 }
210 /**
211 * Allocates new smf_event_t structure. The caller is responsible for allocating
212 * event->midi_buffer, filling it with MIDI data and setting event->midi_buffer_length properly.
213 * Note that event->midi_buffer will be freed by smf_event_delete.
214 * \return pointer to smf_event_t or NULL.
215 */
216 smf_event_t *
217 smf_event_new(void)
218 {
219 smf_event_t *event = malloc(sizeof(smf_event_t));
220 if (event == NULL) {
221 fg_critical("Cannot allocate smf_event_t structure: %s", strerror(errno));
222 return (NULL);
223 }
225 memset(event, 0, sizeof(smf_event_t));
227 event->delta_time_pulses = -1;
228 event->time_pulses = -1;
229 event->time_seconds = -1.0;
230 event->track_number = -1;
232 return (event);
233 }
235 /**
236 * Allocates an smf_event_t structure and fills it with "len" bytes copied
237 * from "midi_data".
238 * \param midi_data Pointer to MIDI data. It sill be copied to the newly allocated event->midi_buffer.
239 * \param len Length of the buffer. It must be proper MIDI event length, e.g. 3 for Note On event.
240 * \return Event containing MIDI data or NULL.
241 */
242 smf_event_t *
243 smf_event_new_from_pointer(void *midi_data, int len)
244 {
245 smf_event_t *event;
247 event = smf_event_new();
248 if (event == NULL)
249 return (NULL);
251 event->midi_buffer_length = len;
252 event->midi_buffer = malloc(event->midi_buffer_length);
253 if (event->midi_buffer == NULL) {
254 fg_critical("Cannot allocate MIDI buffer structure: %s", strerror(errno));
255 smf_event_delete(event);
257 return (NULL);
258 }
260 memcpy(event->midi_buffer, midi_data, len);
262 return (event);
263 }
265 /**
266 * Allocates an smf_event_t structure and fills it with at most three bytes of data.
267 * For example, if you need to create Note On event, do something like this:
268 *
269 * smf_event_new_from_bytes(0x90, 0x3C, 0x7f);
270 *
271 * To create event for MIDI message that is shorter than three bytes, do something
272 * like this:
273 *
274 * smf_event_new_from_bytes(0xC0, 0x42, -1);
275 *
276 * \param first_byte First byte of MIDI message. Must be valid status byte.
277 * \param second_byte Second byte of MIDI message or -1, if message is one byte long.
278 * \param third_byte Third byte of MIDI message or -1, if message is two bytes long.
279 * \return Event containing MIDI data or NULL.
280 */
281 smf_event_t *
282 smf_event_new_from_bytes(int first_byte, int second_byte, int third_byte)
283 {
284 int len;
286 smf_event_t *event;
288 event = smf_event_new();
289 if (event == NULL)
290 return (NULL);
292 if (first_byte < 0) {
293 fg_critical("First byte of MIDI message cannot be < 0");
294 smf_event_delete(event);
296 return (NULL);
297 }
299 if (first_byte > 255) {
300 fg_critical("smf_event_new_from_bytes: first byte is %d, which is larger than 255.", first_byte);
301 return (NULL);
302 }
304 if (!is_status_byte(first_byte)) {
305 fg_critical("smf_event_new_from_bytes: first byte is not a valid status byte.");
306 return (NULL);
307 }
310 if (second_byte < 0)
311 len = 1;
312 else if (third_byte < 0)
313 len = 2;
314 else
315 len = 3;
317 if (len > 1) {
318 if (second_byte > 255) {
319 fg_critical("smf_event_new_from_bytes: second byte is %d, which is larger than 255.", second_byte);
320 return (NULL);
321 }
323 if (is_status_byte(second_byte)) {
324 fg_critical("smf_event_new_from_bytes: second byte cannot be a status byte.");
325 return (NULL);
326 }
327 }
329 if (len > 2) {
330 if (third_byte > 255) {
331 fg_critical("smf_event_new_from_bytes: third byte is %d, which is larger than 255.", third_byte);
332 return (NULL);
333 }
335 if (is_status_byte(third_byte)) {
336 fg_critical("smf_event_new_from_bytes: third byte cannot be a status byte.");
337 return (NULL);
338 }
339 }
341 event->midi_buffer_length = len;
342 event->midi_buffer = malloc(event->midi_buffer_length);
343 if (event->midi_buffer == NULL) {
344 fg_critical("Cannot allocate MIDI buffer structure: %s", strerror(errno));
345 smf_event_delete(event);
347 return (NULL);
348 }
350 event->midi_buffer[0] = first_byte;
351 if (len > 1)
352 event->midi_buffer[1] = second_byte;
353 if (len > 2)
354 event->midi_buffer[2] = third_byte;
356 return (event);
357 }
359 /**
360 * Detaches event from its track and frees it.
361 */
362 void
363 smf_event_delete(smf_event_t *event)
364 {
365 if (event->track != NULL)
366 smf_event_remove_from_track(event);
368 if (event->midi_buffer != NULL) {
369 memset(event->midi_buffer, 0, event->midi_buffer_length);
370 free(event->midi_buffer);
371 }
373 memset(event, 0, sizeof(smf_event_t));
374 free(event);
375 }
377 /**
378 * Used for sorting track->events_array.
379 */
380 static gint
381 events_array_compare_function(gconstpointer aa, gconstpointer bb)
382 {
383 smf_event_t *a, *b;
385 /* "The comparison function for fg_ptr_array_sort() doesn't take the pointers
386 from the array as arguments, it takes pointers to the pointers in the array." */
387 a = (smf_event_t *)*(gpointer *)aa;
388 b = (smf_event_t *)*(gpointer *)bb;
390 if (a->time_pulses < b->time_pulses)
391 return (-1);
393 if (a->time_pulses > b->time_pulses)
394 return (1);
396 /*
397 * We need to preserve original order, otherwise things will break
398 * when there are several events with the same ->time_pulses.
399 * XXX: This is an ugly hack; we should remove sorting altogether.
400 */
402 if (a->event_number < b->event_number)
403 return (-1);
405 if (a->event_number > b->event_number)
406 return (1);
408 return (0);
409 }
411 /*
412 * An assumption here is that if there is an EOT event, it will be at the end of the track.
413 */
414 static void
415 remove_eot_if_before_pulses(smf_track_t *track, int pulses)
416 {
417 smf_event_t *event;
419 event = smf_track_get_last_event(track);
421 if (event == NULL)
422 return;
424 if (!smf_event_is_eot(event))
425 return;
427 if (event->time_pulses > pulses)
428 return;
430 smf_event_remove_from_track(event);
431 }
433 /**
434 * Adds the event to the track and computes ->delta_pulses. Note that it is faster
435 * to append events to the end of the track than to insert them in the middle.
436 * Usually you want to use smf_track_add_event_seconds or smf_track_add_event_pulses
437 * instead of this one. Event needs to have ->time_pulses and ->time_seconds already set.
438 * If you try to add event after an EOT, EOT event will be automatically deleted.
439 */
440 void
441 smf_track_add_event(smf_track_t *track, smf_event_t *event)
442 {
443 int i, last_pulses = 0;
445 assert(track->smf != NULL);
446 assert(event->track == NULL);
447 assert(event->delta_time_pulses == -1);
448 assert(event->time_pulses >= 0);
449 assert(event->time_seconds >= 0.0);
451 remove_eot_if_before_pulses(track, event->time_pulses);
453 event->track = track;
454 event->track_number = track->track_number;
456 if (track->number_of_events == 0) {
457 assert(track->next_event_number == -1);
458 track->next_event_number = 1;
459 }
461 if (track->number_of_events > 0)
462 last_pulses = smf_track_get_last_event(track)->time_pulses;
464 track->number_of_events++;
466 /* Are we just appending element at the end of the track? */
467 if (last_pulses <= event->time_pulses) {
468 event->delta_time_pulses = event->time_pulses - last_pulses;
469 assert(event->delta_time_pulses >= 0);
470 fg_ptr_array_add(track->events_array, event);
471 event->event_number = track->number_of_events;
473 /* We need to insert in the middle of the track. XXX: This is slow. */
474 } else {
475 /* Append, then sort according to ->time_pulses. */
476 fg_ptr_array_add(track->events_array, event);
477 fg_ptr_array_sort(track->events_array, events_array_compare_function);
479 /* Renumber entries and fix their ->delta_pulses. */
480 for (i = 1; i <= track->number_of_events; i++) {
481 smf_event_t *tmp = smf_track_get_event_by_number(track, i);
482 tmp->event_number = i;
484 if (tmp->delta_time_pulses != -1)
485 continue;
487 if (i == 1) {
488 tmp->delta_time_pulses = tmp->time_pulses;
489 } else {
490 tmp->delta_time_pulses = tmp->time_pulses -
491 smf_track_get_event_by_number(track, i - 1)->time_pulses;
492 assert(tmp->delta_time_pulses >= 0);
493 }
494 }
496 /* Adjust ->delta_time_pulses of the next event. */
497 if (event->event_number < track->number_of_events) {
498 smf_event_t *next_event = smf_track_get_event_by_number(track, event->event_number + 1);
499 assert(next_event);
500 assert(next_event->time_pulses >= event->time_pulses);
501 next_event->delta_time_pulses -= event->delta_time_pulses;
502 assert(next_event->delta_time_pulses >= 0);
503 }
504 }
506 if (smf_event_is_tempo_change_or_time_signature(event)) {
507 if (smf_event_is_last(event))
508 maybe_add_to_tempo_map(event);
509 else
510 smf_create_tempo_map_and_compute_seconds(event->track->smf);
511 }
512 }
514 /**
515 * Add End Of Track metaevent. Using it is optional, libsmf will automatically
516 * add EOT to the tracks during smf_save, with delta_pulses 0. If you try to add EOT
517 * in the middle of the track, it will fail and nonzero value will be returned.
518 * If you try to add EOT after another EOT event, it will be added, but the existing
519 * EOT event will be removed.
520 *
521 * \return 0 if everything went ok, nonzero otherwise.
522 */
523 int
524 smf_track_add_eot_delta_pulses(smf_track_t *track, int delta)
525 {
526 smf_event_t *event;
528 event = smf_event_new_from_bytes(0xFF, 0x2F, 0x00);
529 if (event == NULL)
530 return (-1);
532 smf_track_add_event_delta_pulses(track, event, delta);
534 return (0);
535 }
537 int
538 smf_track_add_eot_pulses(smf_track_t *track, int pulses)
539 {
540 smf_event_t *event, *last_event;
542 last_event = smf_track_get_last_event(track);
543 if (last_event != NULL) {
544 if (last_event->time_pulses > pulses)
545 return (-2);
546 }
548 event = smf_event_new_from_bytes(0xFF, 0x2F, 0x00);
549 if (event == NULL)
550 return (-3);
552 smf_track_add_event_pulses(track, event, pulses);
554 return (0);
555 }
557 int
558 smf_track_add_eot_seconds(smf_track_t *track, double seconds)
559 {
560 smf_event_t *event, *last_event;
562 last_event = smf_track_get_last_event(track);
563 if (last_event != NULL) {
564 if (last_event->time_seconds > seconds)
565 return (-2);
566 }
568 event = smf_event_new_from_bytes(0xFF, 0x2F, 0x00);
569 if (event == NULL)
570 return (-1);
572 smf_track_add_event_seconds(track, event, seconds);
574 return (0);
575 }
577 /**
578 * Detaches event from its track.
579 */
580 void
581 smf_event_remove_from_track(smf_event_t *event)
582 {
583 int i, was_last;
584 smf_event_t *tmp;
585 smf_track_t *track;
587 assert(event->track != NULL);
588 assert(event->track->smf != NULL);
590 track = event->track;
591 was_last = smf_event_is_last(event);
593 /* Adjust ->delta_time_pulses of the next event. */
594 if (event->event_number < track->number_of_events) {
595 tmp = smf_track_get_event_by_number(track, event->event_number + 1);
596 assert(tmp);
597 tmp->delta_time_pulses += event->delta_time_pulses;
598 }
600 track->number_of_events--;
601 fg_ptr_array_remove(track->events_array, event);
603 if (track->number_of_events == 0)
604 track->next_event_number = -1;
606 /* Renumber the rest of the events, so they are consecutively numbered. */
607 for (i = event->event_number; i <= track->number_of_events; i++) {
608 tmp = smf_track_get_event_by_number(track, i);
609 tmp->event_number = i;
610 }
612 if (smf_event_is_tempo_change_or_time_signature(event)) {
613 /* XXX: This will cause problems, when there is more than one Tempo Change event at a given time. */
614 if (was_last)
615 remove_last_tempo_with_pulses(event->track->smf, event->time_pulses);
616 else
617 smf_create_tempo_map_and_compute_seconds(track->smf);
618 }
620 event->track = NULL;
621 event->event_number = -1;
622 event->delta_time_pulses = -1;
623 event->time_pulses = -1;
624 event->time_seconds = -1.0;
625 }
627 /**
628 * \return Nonzero if event is Tempo Change or Time Signature metaevent.
629 */
630 int
631 smf_event_is_tempo_change_or_time_signature(const smf_event_t *event)
632 {
633 if (!smf_event_is_metadata(event))
634 return (0);
636 assert(event->midi_buffer_length >= 2);
638 if (event->midi_buffer[1] == 0x51 || event->midi_buffer[1] == 0x58)
639 return (1);
641 return (0);
642 }
644 /**
645 * Sets "Format" field of MThd header to the specified value. Note that you
646 * don't really need to use this, as libsmf will automatically change format
647 * from 0 to 1 when you add the second track.
648 * \param smf SMF.
649 * \param format 0 for one track per file, 1 for several tracks per file.
650 */
651 int
652 smf_set_format(smf_t *smf, int format)
653 {
654 assert(format == 0 || format == 1);
656 if (smf->number_of_tracks > 1 && format == 0) {
657 fg_critical("There is more than one track, cannot set format to 0.");
658 return (-1);
659 }
661 smf->format = format;
663 return (0);
664 }
666 /**
667 * Sets the PPQN ("Division") field of MThd header. This is mandatory, you
668 * should call it right after smf_new. Note that changing PPQN will change time_seconds
669 * of all the events.
670 * \param smf SMF.
671 * \param ppqn New PPQN.
672 */
673 int
674 smf_set_ppqn(smf_t *smf, int ppqn)
675 {
676 assert(ppqn > 0);
678 smf->ppqn = ppqn;
680 return (0);
681 }
683 /**
684 * Returns next event from the track given and advances next event counter.
685 * Do not depend on End Of Track event being the last event on the track - it
686 * is possible that the track will not end with EOT if you haven't added it
687 * yet. EOTs are added automatically during smf_save().
688 *
689 * \return Event or NULL, if there are no more events left in this track.
690 */
691 smf_event_t *
692 smf_track_get_next_event(smf_track_t *track)
693 {
694 smf_event_t *event, *next_event;
696 /* End of track? */
697 if (track->next_event_number == -1)
698 return (NULL);
700 assert(track->next_event_number >= 1);
701 assert(track->number_of_events > 0);
703 event = smf_track_get_event_by_number(track, track->next_event_number);
705 assert(event != NULL);
707 /* Is this the last event in the track? */
708 if (track->next_event_number < track->number_of_events) {
709 next_event = smf_track_get_event_by_number(track, track->next_event_number + 1);
710 assert(next_event);
712 track->time_of_next_event = next_event->time_pulses;
713 track->next_event_number++;
714 } else {
715 track->next_event_number = -1;
716 }
718 return (event);
719 }
721 /**
722 * Returns next event from the track given. Does not change next event counter,
723 * so repeatedly calling this routine will return the same event.
724 * \return Event or NULL, if there are no more events left in this track.
725 */
726 static smf_event_t *
727 smf_peek_next_event_from_track(smf_track_t *track)
728 {
729 smf_event_t *event;
731 /* End of track? */
732 if (track->next_event_number == -1)
733 return (NULL);
735 assert(track->next_event_number >= 1);
736 assert(track->events_array->len != 0);
738 event = smf_track_get_event_by_number(track, track->next_event_number);
740 return (event);
741 }
743 /**
744 * \return Track with a given number or NULL, if there is no such track.
745 * Tracks are numbered consecutively starting from one.
746 */
747 smf_track_t *
748 smf_get_track_by_number(const smf_t *smf, int track_number)
749 {
750 smf_track_t *track;
752 assert(track_number >= 1);
754 if (track_number > smf->number_of_tracks)
755 return (NULL);
757 track = (smf_track_t *)fg_ptr_array_index(smf->tracks_array, track_number - 1);
759 assert(track);
761 return (track);
762 }
764 /**
765 * \return Event with a given number or NULL, if there is no such event.
766 * Events are numbered consecutively starting from one.
767 */
768 smf_event_t *
769 smf_track_get_event_by_number(const smf_track_t *track, int event_number)
770 {
771 smf_event_t *event;
773 assert(event_number >= 1);
775 if (event_number > track->number_of_events)
776 return (NULL);
778 event = fg_ptr_array_index(track->events_array, event_number - 1);
780 assert(event);
782 return (event);
783 }
785 /**
786 * \return Last event on the track or NULL, if track is empty.
787 */
788 smf_event_t *
789 smf_track_get_last_event(const smf_track_t *track)
790 {
791 smf_event_t *event;
793 if (track->number_of_events == 0)
794 return (NULL);
796 event = smf_track_get_event_by_number(track, track->number_of_events);
798 return (event);
799 }
801 /**
802 * Searches for track that contains next event, in time order. In other words,
803 * returns the track that contains event that should be played next.
804 * \return Track with next event or NULL, if there are no events left.
805 */
806 smf_track_t *
807 smf_find_track_with_next_event(smf_t *smf)
808 {
809 int i, min_time = 0;
810 smf_track_t *track = NULL, *min_time_track = NULL;
812 /* Find track with event that should be played next. */
813 for (i = 1; i <= smf->number_of_tracks; i++) {
814 track = smf_get_track_by_number(smf, i);
816 assert(track);
818 /* No more events in this track? */
819 if (track->next_event_number == -1)
820 continue;
822 if (track->time_of_next_event < min_time || min_time_track == NULL) {
823 min_time = track->time_of_next_event;
824 min_time_track = track;
825 }
826 }
828 return (min_time_track);
829 }
831 /**
832 * \return Next event, in time order, or NULL, if there are none left.
833 */
834 smf_event_t *
835 smf_get_next_event(smf_t *smf)
836 {
837 smf_event_t *event;
838 smf_track_t *track = smf_find_track_with_next_event(smf);
840 if (track == NULL) {
841 #if 0
842 g_debug("End of the song.");
843 #endif
845 return (NULL);
846 }
848 event = smf_track_get_next_event(track);
850 assert(event != NULL);
852 event->track->smf->last_seek_position = -1.0;
854 return (event);
855 }
857 /**
858 * Advance the "next event counter". This is functionally the same as calling
859 * smf_get_next_event and ignoring the return value.
860 */
861 void
862 smf_skip_next_event(smf_t *smf)
863 {
864 void *notused;
866 notused = smf_get_next_event(smf);
867 }
869 /**
870 * \return Next event, in time order, or NULL, if there are none left. Does
871 * not advance position in song.
872 */
873 smf_event_t *
874 smf_peek_next_event(smf_t *smf)
875 {
876 smf_event_t *event;
877 smf_track_t *track = smf_find_track_with_next_event(smf);
879 if (track == NULL) {
880 #if 0
881 g_debug("End of the song.");
882 #endif
884 return (NULL);
885 }
887 event = smf_peek_next_event_from_track(track);
889 assert(event != NULL);
891 return (event);
892 }
894 /**
895 * Rewinds the SMF. What that means is, after calling this routine, smf_get_next_event
896 * will return first event in the song.
897 */
898 void
899 smf_rewind(smf_t *smf)
900 {
901 int i;
902 smf_track_t *track = NULL;
903 smf_event_t *event;
905 assert(smf);
907 smf->last_seek_position = 0.0;
909 for (i = 1; i <= smf->number_of_tracks; i++) {
910 track = smf_get_track_by_number(smf, i);
912 assert(track != NULL);
914 if (track->number_of_events > 0) {
915 track->next_event_number = 1;
916 event = smf_peek_next_event_from_track(track);
917 assert(event);
918 track->time_of_next_event = event->time_pulses;
919 } else {
920 track->next_event_number = -1;
921 track->time_of_next_event = 0;
922 #if 0
923 g_warning("Warning: empty track.");
924 #endif
925 }
926 }
927 }
929 /**
930 * Seeks the SMF to the given event. After calling this routine, smf_get_next_event
931 * will return the event that was the second argument of this call.
932 */
933 int
934 smf_seek_to_event(smf_t *smf, const smf_event_t *target)
935 {
936 smf_event_t *event;
938 smf_rewind(smf);
940 #if 0
941 g_debug("Seeking to event %d, track %d.", target->event_number, target->track->track_number);
942 #endif
944 for (;;) {
945 event = smf_peek_next_event(smf);
947 /* There can't be NULL here, unless "target" is not in this smf. */
948 assert(event);
950 if (event != target)
951 smf_skip_next_event(smf);
952 else
953 break;
954 }
956 smf->last_seek_position = event->time_seconds;
958 return (0);
959 }
961 /**
962 * Seeks the SMF to the given position. For example, after seeking to 1.0 seconds,
963 * smf_get_next_event will return first event that happens after the first second of song.
964 */
965 int
966 smf_seek_to_seconds(smf_t *smf, double seconds)
967 {
968 smf_event_t *event;
970 assert(seconds >= 0.0);
972 if (seconds == smf->last_seek_position) {
973 #if 0
974 g_debug("Avoiding seek to %f seconds.", seconds);
975 #endif
976 return (0);
977 }
979 smf_rewind(smf);
981 #if 0
982 g_debug("Seeking to %f seconds.", seconds);
983 #endif
985 for (;;) {
986 event = smf_peek_next_event(smf);
988 if (event == NULL) {
989 fg_critical("Trying to seek past the end of song.");
990 return (-1);
991 }
993 if (event->time_seconds < seconds)
994 smf_skip_next_event(smf);
995 else
996 break;
997 }
999 smf->last_seek_position = seconds;
1001 return (0);
1002 }
1004 /**
1005 * Seeks the SMF to the given position. For example, after seeking to 10 pulses,
1006 * smf_get_next_event will return first event that happens after the first ten pulses.
1007 */
1008 int
1009 smf_seek_to_pulses(smf_t *smf, int pulses)
1010 {
1011 smf_event_t *event;
1013 assert(pulses >= 0);
1015 smf_rewind(smf);
1017 #if 0
1018 g_debug("Seeking to %d pulses.", pulses);
1019 #endif
1021 for (;;) {
1022 event = smf_peek_next_event(smf);
1024 if (event == NULL) {
1025 fg_critical("Trying to seek past the end of song.");
1026 return (-1);
1027 }
1029 if (event->time_pulses < pulses)
1030 smf_skip_next_event(smf);
1031 else
1032 break;
1033 }
1035 smf->last_seek_position = event->time_seconds;
1037 return (0);
1038 }
1040 /**
1041 * \return Length of SMF, in pulses.
1042 */
1043 int
1044 smf_get_length_pulses(const smf_t *smf)
1045 {
1046 int pulses = 0, i;
1048 for (i = 1; i <= smf->number_of_tracks; i++) {
1049 smf_track_t *track;
1050 smf_event_t *event;
1052 track = smf_get_track_by_number(smf, i);
1053 assert(track);
1055 event = smf_track_get_last_event(track);
1056 /* Empty track? */
1057 if (event == NULL)
1058 continue;
1060 if (event->time_pulses > pulses)
1061 pulses = event->time_pulses;
1062 }
1064 return (pulses);
1065 }
1067 /**
1068 * \return Length of SMF, in seconds.
1069 */
1070 double
1071 smf_get_length_seconds(const smf_t *smf)
1072 {
1073 int i;
1074 double seconds = 0.0;
1076 for (i = 1; i <= smf->number_of_tracks; i++) {
1077 smf_track_t *track;
1078 smf_event_t *event;
1080 track = smf_get_track_by_number(smf, i);
1081 assert(track);
1083 event = smf_track_get_last_event(track);
1084 /* Empty track? */
1085 if (event == NULL)
1086 continue;
1088 if (event->time_seconds > seconds)
1089 seconds = event->time_seconds;
1090 }
1092 return (seconds);
1093 }
1095 /**
1096 * \return Nonzero, if there are no events in the SMF after this one.
1097 * Note that may be more than one "last event", if they occur at the same time.
1098 */
1099 int
1100 smf_event_is_last(const smf_event_t *event)
1101 {
1102 if (smf_get_length_pulses(event->track->smf) <= event->time_pulses)
1103 return (1);
1105 return (0);
1106 }
1108 /**
1109 * \return Version of libsmf.
1110 */
1111 const char *
1112 smf_get_version(void)
1113 {
1114 return (SMF_VERSION);
1115 }