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