2103ad8eae7afad837e68bd710874db886cea1fd
[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 <cmath>
23 #include <iostream>
24 #include <stdint.h>
25 #include "libsmf/smf.h"
26 #include "evoral/Event.hpp"
27 #include "evoral/SMF.hpp"
28 #include "evoral/midi_util.h"
29
30 using namespace std;
31
32 namespace Evoral {
33
34 SMF::~SMF()
35 {       
36         if (_smf) {
37                 smf_delete(_smf);
38                 _smf = 0;
39                 _smf_track = 0;
40         } 
41 }
42
43 uint16_t
44 SMF::num_tracks() const
45 {
46         return _smf->number_of_tracks;
47 }
48
49 uint16_t
50 SMF::ppqn() const
51 {
52         return _smf->ppqn;
53 }
54
55 /** Seek to the specified track (1-based indexing)
56  * \return 0 on success
57  */
58 int
59 SMF::seek_to_track(int track)
60 {
61         _smf_track = smf_get_track_by_number(_smf, track);
62         if (_smf_track != NULL) {
63                 _smf_track->next_event_number = (_smf_track->number_of_events == 0) ? 0 : 1;
64                 return 0;
65         } else {
66                 return -1;
67         }
68 }
69
70 /** Attempt to open the SMF file for reading and/or writing.
71  *
72  * \return  0 on success
73  *         -1 if the file can not be opened or created
74  *         -2 if the file exists but specified track does not exist
75  */
76 int
77 SMF::open(const std::string& path, int track) THROW_FILE_ERROR
78 {
79         assert(track >= 1);
80         if (_smf) { 
81                 smf_delete(_smf);
82         }
83         
84         _file_path = path;
85         _smf = smf_load(_file_path.c_str());
86         if (_smf == NULL) {
87                 return -1;
88         }
89
90         _smf_track = smf_get_track_by_number(_smf, track);
91         if (!_smf_track)
92                 return -2;
93
94         //cerr << "Track " << track << " # events: " << _smf_track->number_of_events << endl;
95         if (_smf_track->number_of_events == 0) {
96                 _smf_track->next_event_number = 0;
97                 _empty = true;
98         } else {
99                 _smf_track->next_event_number = 1;
100                 _empty = false;
101         }
102         
103         return 0;
104 }
105
106
107 /** Attempt to create a new SMF file for reading and/or writing.
108  *
109  * \return  0 on success
110  *         -1 if the file can not be created
111  *         -2 if the track can not be created
112  */
113 int
114 SMF::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
115 {
116         assert(track >= 1);
117         if (_smf) { 
118                 smf_delete(_smf);
119         }
120         
121         _file_path = path;
122
123         _smf = smf_new();
124         if (smf_set_ppqn(_smf, ppqn) != 0) {
125                 throw FileError();
126         }
127         
128         if (_smf == NULL) {
129                 return -1;
130         }
131         
132         for (int i = 0; i < track; ++i) {
133                 _smf_track = smf_track_new();
134                 assert(_smf_track);
135                 smf_add_track(_smf, _smf_track);
136         }
137
138         _smf_track = smf_get_track_by_number(_smf, track);
139         if (!_smf_track)
140                 return -2;
141
142         _smf_track->next_event_number = 0;
143         _empty = true;
144         
145         return 0;
146 }
147
148 void
149 SMF::close() THROW_FILE_ERROR
150 {
151         if (_smf) {
152                 if (smf_save(_smf, _file_path.c_str()) != 0) {
153                         throw FileError();
154                 }
155                 smf_delete(_smf);
156                 _smf = 0;
157                 _smf_track = 0;
158         }
159 }
160
161 void
162 SMF::seek_to_start() const
163 {
164         _smf_track->next_event_number = 1;
165 }
166
167 /** Read an event from the current position in file.
168  *
169  * File position MUST be at the beginning of a delta time, or this will die very messily.
170  * ev.buffer must be of size ev.size, and large enough for the event.  The returned event
171  * will have it's time field set to it's delta time, in SMF tempo-based ticks, using the
172  * rate given by ppqn() (it is the caller's responsibility to calculate a real time).
173  *
174  * \a buf must be a pointer to a buffer allocated with malloc, or a pointer to NULL.
175  * \a size must be the capacity of \a buf.  If it is not large enough, \a buf will
176  * be reallocated and *size will be set to the new size of buf.
177  *
178  * \return event length (including status byte) on success, 0 if event was
179  * skipped (e.g. a meta event), or -1 on EOF (or end of track).
180  */
181 int
182 SMF::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const
183 {
184         smf_event_t* event;
185         
186         assert(delta_t);
187         assert(size);
188         assert(buf);
189         
190     if ((event = smf_track_get_next_event(_smf_track)) != NULL) {
191         if (smf_event_is_metadata(event)) {
192                 return 0;
193         }
194         *delta_t = event->delta_time_pulses;
195         
196         int event_size = event->midi_buffer_length;
197         assert(event_size > 0);
198                 
199         // Make sure we have enough scratch buffer
200         if (*size < (unsigned)event_size) {
201                 *buf = (uint8_t*)realloc(*buf, event_size);
202         }
203         memcpy(*buf, event->midi_buffer, size_t(event_size));
204         *size = event_size;
205         
206                 assert(midi_event_is_valid(*buf, *size));
207
208                 /*printf("SMF::read_event:\n");
209                 for (size_t i = 0; i < *size; ++i) {
210                         printf("%X ", (*buf)[i]);
211                 } printf("\n");*/
212         
213         return event_size;
214     } else {
215         return -1;
216     }
217 }
218
219 void
220 SMF::append_event_delta(uint32_t delta_t, uint32_t size, const uint8_t* buf)
221 {
222         if (size == 0) {
223                 return;
224         }
225         
226         /*printf("SMF::append_event_delta:\n");
227         for (size_t i = 0; i < size; ++i) {
228                 printf("%X ", buf[i]);
229         } printf("\n");*/
230
231         if (!midi_event_is_valid(buf, size)) {
232                 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
233                 return;
234         }
235
236         smf_event_t* event;
237
238         event = smf_event_new_from_pointer(buf, size);
239         assert(event != NULL);
240         
241         assert(_smf_track);
242         smf_track_add_event_delta_pulses(_smf_track, event, delta_t);
243         _empty = false;
244 }
245
246 void
247 SMF::begin_write()
248 {
249         assert(_smf_track);
250         smf_track_delete(_smf_track);
251         
252         _smf_track = smf_track_new();
253         assert(_smf_track);
254         
255         smf_add_track(_smf, _smf_track);
256         assert(_smf->number_of_tracks == 1);
257 }
258
259 void
260 SMF::end_write() THROW_FILE_ERROR
261 {
262         if (smf_save(_smf, _file_path.c_str()) != 0)
263                 throw FileError();
264 }
265
266 double
267 SMF::round_to_file_precision (double val) const
268 {
269         double div = ppqn();
270
271         return round (val * div) / div;
272 }
273
274
275 } // namespace Evoral