2b47cbfe4bf74cbf1735db9e45c459bd9fba85cf
[ardour.git] / libs / evoral / src / SMF.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  * Author: Hans Baier
5  * 
6  * Evoral is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  * 
11  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
14  * 
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #define __STDC_LIMIT_MACROS 1
21 #include <cassert>
22 #include <iostream>
23 #include <stdint.h>
24 #include "evoral/Event.hpp"
25 #include "evoral/SMF.hpp"
26 #include "libsmf/smf.h"
27
28 using namespace std;
29
30 namespace Evoral {
31
32 template<typename Time>
33 SMF<Time>::~SMF()
34 {       
35         if (_smf) {
36                 smf_delete(_smf);
37                 _smf = 0;
38                 _smf_track = 0;
39         } 
40 }
41
42 template<typename Time>
43 uint16_t
44 SMF<Time>::num_tracks() const
45 {
46         return _smf->number_of_tracks;
47 }
48
49 template<typename Time>
50 uint16_t
51 SMF<Time>::ppqn() const
52 {
53         return _smf->ppqn;
54 }
55
56 /** Seek to the specified track (1-based indexing)
57  * \return 0 on success
58  */
59 template<typename Time>
60 int
61 SMF<Time>::seek_to_track(int track)
62 {
63         _smf_track = smf_get_track_by_number(_smf, track);
64         if (_smf_track != NULL) {
65                 _smf_track->next_event_number = (_smf_track->number_of_events == 0) ? 0 : 1;
66                 return 0;
67         } else {
68                 return -1;
69         }
70 }
71
72 /** Attempt to open the SMF file for reading and/or writing.
73  *
74  * \return  0 on success
75  *         -1 if the file can not be opened or created
76  *         -2 if the file exists but specified track does not exist
77  */
78 template<typename Time>
79 int
80 SMF<Time>::open(const std::string& path, int track) THROW_FILE_ERROR
81 {
82         assert(track >= 1);
83         if (_smf) { 
84                 smf_delete(_smf);
85         }
86         
87         _path = path;
88         _smf = smf_load(_path.c_str());
89         if (_smf == NULL) {
90                 return -1;
91         }
92
93         _smf_track = smf_get_track_by_number(_smf, track);
94         if (!_smf_track)
95                 return -2;
96
97         cerr << "Track " << track << " # events: " << _smf_track->number_of_events << endl;
98         if (_smf_track->number_of_events == 0) {
99                 _smf_track->next_event_number = 0;
100                 _empty = true;
101         } else {
102                 _smf_track->next_event_number = 1;
103                 _empty = false;
104         }
105         
106         return 0;
107 }
108
109
110 /** Attempt to create a new SMF file for reading and/or writing.
111  *
112  * \return  0 on success
113  *         -1 if the file can not be created
114  *         -2 if the track can not be created
115  */
116 template<typename Time>
117 int
118 SMF<Time>::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
119 {
120         assert(track >= 1);
121         if (_smf) { 
122                 smf_delete(_smf);
123         }
124         
125         _path = path;
126
127         _smf = smf_new();
128         if (smf_set_ppqn(_smf, ppqn) != 0) {
129                 throw FileError();
130         }
131         
132         if (_smf == NULL) {
133                 return -1;
134         }
135         
136         for (int i = 0; i < track; ++i) {
137                 _smf_track = smf_track_new();
138                 assert(_smf_track);
139                 smf_add_track(_smf, _smf_track);
140         }
141
142         _smf_track = smf_get_track_by_number(_smf, track);
143         if (!_smf_track)
144                 return -2;
145
146         _smf_track->next_event_number = 0;
147         _empty = true;
148         
149         return 0;
150 }
151
152 template<typename Time>
153 void
154 SMF<Time>::close() THROW_FILE_ERROR
155 {
156         if (_smf) {
157                 if (smf_save(_smf, _path.c_str()) != 0) {
158                         throw FileError();
159                 }
160                 smf_delete(_smf);
161                 _smf = 0;
162                 _smf_track = 0;
163         }
164 }
165
166 template<typename Time>
167 void
168 SMF<Time>::seek_to_start() const
169 {
170         _smf_track->next_event_number = 1;
171 }
172
173 /** Read an event from the current position in file.
174  *
175  * File position MUST be at the beginning of a delta time, or this will die very messily.
176  * ev.buffer must be of size ev.size, and large enough for the event.  The returned event
177  * will have it's time field set to it's delta time, in SMF tempo-based ticks, using the
178  * rate given by ppqn() (it is the caller's responsibility to calculate a real time).
179  *
180  * \a buf must be a pointer to a buffer allocated with malloc, or a pointer to NULL.
181  * \a size must be the capacity of \a buf.  If it is not large enough, \a buf will
182  * be reallocated and *size will be set to the new size of buf.
183  *
184  * \return event length (including status byte) on success, 0 if event was
185  * skipped (e.g. a meta event), or -1 on EOF (or end of track).
186  */
187 template<typename Time>
188 int
189 SMF<Time>::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const
190 {
191         smf_event_t* event;
192         
193         assert(delta_t);
194         assert(size);
195         assert(buf);
196         
197     if ((event = smf_track_get_next_event(_smf_track)) != NULL) {
198         if (smf_event_is_metadata(event)) {
199                 return 0;
200         }
201         *delta_t = event->delta_time_pulses;
202         
203         int event_size = event->midi_buffer_length;
204         assert(event_size > 0);
205                 
206         // Make sure we have enough scratch buffer
207         if (*size < (unsigned)event_size) {
208                 *buf = (uint8_t*)realloc(*buf, event_size);
209         }
210         memcpy(*buf, event->midi_buffer, size_t(event_size));
211         *size = event_size;
212         
213                 /*printf("SMF::read_event:\n");
214                 for (size_t i=0; i < *size; ++i) {
215                         printf("%X ", (*buf)[i]);
216                 } printf("\n");*/
217         
218         return event_size;
219     } else {
220         return -1;
221     }
222 }
223
224 template<typename Time>
225 void
226 SMF<Time>::append_event_delta(uint32_t delta_t, const Event<Time>& ev)
227 {
228         assert(ev.size() > 0);
229         
230         /*printf("SMF::append_event_delta:\n");
231         for (size_t i=0; i < ev.size(); ++i) {
232                 printf("%X ", ev.buffer()[i]);
233         } printf("\n");*/
234
235         smf_event_t* event;
236
237         event = smf_event_new_from_pointer((void *) ev.buffer(), int(ev.size()));
238         assert(event != NULL);
239         
240         memcpy(event->midi_buffer, ev.buffer(), ev.size());
241         
242         assert(_smf_track);
243         smf_track_add_event_delta_pulses(_smf_track, event, int(delta_t));
244         _last_ev_time = ev.time();
245         
246         if (ev.size() > 0) {
247                 _empty = false;
248         }
249 }
250
251 template<typename Time>
252 void
253 SMF<Time>::begin_write()
254 {
255         assert(_smf_track);
256         smf_track_delete(_smf_track);
257         
258         _smf_track = smf_track_new();
259         assert(_smf_track);
260         
261         smf_add_track(_smf, _smf_track);
262         assert(_smf->number_of_tracks == 1);
263         
264         _last_ev_time = 0;
265 }
266
267 template<typename Time>
268 void
269 SMF<Time>::end_write() THROW_FILE_ERROR
270 {
271         if (smf_save(_smf, _path.c_str()) != 0)
272                 throw FileError();
273 }
274
275 template class SMF<double>;
276
277 } // namespace Evoral