import 0.1.7.1
[aubio.git] / src / midi_track.c
1 /* 
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Library General Public License
5  * as published by the Free Software Foundation; either version 2 of
6  * the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Library General Public License for more details.
12  *  
13  * You should have received a copy of the GNU Library General Public
14  * License along with this library; if not, write to the Free
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
16  * 02111-1307, USA
17  */
18
19 /* this file originally taken from FluidSynth - A Software Synthesizer
20  * Copyright (C) 2003  Peter Hanappe and others.
21  */
22
23
24 #include "aubio_priv.h"
25 #include "midi.h"
26 #include "midi_event.h"
27 #include "midi_track.h"
28 #include "midi_player.h"
29
30
31 /** new_aubio_track */
32 aubio_track_t* new_aubio_track(int num)
33 {
34   aubio_track_t* track;
35   track = AUBIO_NEW(aubio_track_t);
36   if (track == NULL) {
37     return NULL;
38   }
39   track->name = NULL;
40   track->num = num;
41   track->first = NULL;
42   track->cur = NULL;
43   track->last = NULL;
44   track->ticks = 0;
45   return track;
46 }
47
48 /** del_aubio_track */
49 int del_aubio_track(aubio_track_t* track)
50 {
51   if (track->name != NULL) {
52     AUBIO_FREE(track->name);
53   }
54   if (track->first != NULL) {
55     del_aubio_midi_event(track->first);
56   }
57   AUBIO_FREE(track);
58   return AUBIO_OK;
59 }
60
61 /** aubio_track_set_name */
62 int aubio_track_set_name(aubio_track_t* track, char* name)
63 {
64   int len;
65   if (track->name != NULL) {
66     AUBIO_FREE(track->name);
67   }
68   if (name == NULL) {
69     track->name = NULL;
70     return AUBIO_OK;  
71   }
72   len = AUBIO_STRLEN(name);
73   track->name = AUBIO_MALLOC(len + 1);
74   if (track->name == NULL) {
75     AUBIO_ERR( "Out of memory");
76     return AUBIO_FAIL;
77   }
78   AUBIO_STRCPY(track->name, name);
79   return AUBIO_OK;  
80 }
81
82 /** aubio_track_get_name */
83 char* aubio_track_get_name(aubio_track_t* track)
84 {
85   return track->name;
86 }
87
88 /** aubio_track_get_duration */
89 int aubio_track_get_duration(aubio_track_t* track)
90  {
91   int time = 0;
92   aubio_midi_event_t* evt = track->first;
93   while (evt != NULL) {
94     time += evt->dtime;
95     evt = evt->next;
96   }
97   return time;
98 }
99
100 /** aubio_track_count_events  */
101 int aubio_track_count_events(aubio_track_t* track, int* on, int* off)
102 {
103   aubio_midi_event_t* evt = track->first;
104   while (evt != NULL) {
105     if (evt->type == NOTE_ON) {
106       (*on)++;
107     } else if (evt->type == NOTE_OFF) {
108       (*off)++;
109     }
110     evt = evt->next;
111   }
112   return AUBIO_OK;
113 }
114
115 /*
116  * aubio_track_add_event
117  */
118 int aubio_track_add_event(aubio_track_t* track, aubio_midi_event_t* evt)
119 {
120   evt->next = NULL;
121   if (track->first == NULL) {
122     track->first = evt;
123     track->cur = evt;
124     track->last = evt;
125   } else {
126     track->last->next = evt;
127     track->last = evt;
128   }
129   return AUBIO_OK;
130 }
131
132 /*
133  * aubio_track_first_event
134  */
135 aubio_midi_event_t* aubio_track_first_event(aubio_track_t* track)
136 {
137   track->cur = track->first;
138   return track->cur;
139 }
140
141 /*
142  * aubio_track_next_event
143  */
144 aubio_midi_event_t* aubio_track_next_event(aubio_track_t* track)
145 {
146   if (track->cur != NULL) {
147     track->cur = track->cur->next;
148   }
149   return track->cur;
150 }
151
152 /*
153  * aubio_track_reset
154  */
155   int 
156 aubio_track_reset(aubio_track_t* track)
157 {
158   track->ticks = 0;
159   track->cur = track->first;
160   return AUBIO_OK;
161 }
162