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