1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
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
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.
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
20 #define __STDC_LIMIT_MACROS 1
24 #include "evoral/Event.hpp"
25 #include "evoral/SMF.hpp"
26 #include "libsmf/smf.h"
32 template<typename Time>
42 template<typename Time>
44 SMF<Time>::num_tracks() const
46 return _smf->number_of_tracks;
49 template<typename Time>
51 SMF<Time>::ppqn() const
56 /** Seek to the specified track (1-based indexing)
57 * \return 0 on success
59 template<typename Time>
61 SMF<Time>::seek_to_track(int track)
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;
72 /** Attempt to open the SMF file for reading and/or writing.
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
78 template<typename Time>
80 SMF<Time>::open(const std::string& path, int track) THROW_FILE_ERROR
88 _smf = smf_load(_path.c_str());
93 _smf_track = smf_get_track_by_number(_smf, track);
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;
102 _smf_track->next_event_number = 1;
110 /** Attempt to create a new SMF file for reading and/or writing.
112 * \return 0 on success
113 * -1 if the file can not be created
114 * -2 if the track can not be created
116 template<typename Time>
118 SMF<Time>::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
128 if (smf_set_ppqn(_smf, ppqn) != 0) {
136 for (int i = 0; i < track; ++i) {
137 _smf_track = smf_track_new();
139 smf_add_track(_smf, _smf_track);
142 _smf_track = smf_get_track_by_number(_smf, track);
146 _smf_track->next_event_number = 0;
152 template<typename Time>
154 SMF<Time>::close() THROW_FILE_ERROR
157 if (smf_save(_smf, _path.c_str()) != 0) {
166 template<typename Time>
168 SMF<Time>::seek_to_start() const
170 _smf_track->next_event_number = 1;
173 /** Read an event from the current position in file.
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).
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.
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).
187 template<typename Time>
189 SMF<Time>::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const
197 if ((event = smf_track_get_next_event(_smf_track)) != NULL) {
198 if (smf_event_is_metadata(event)) {
201 *delta_t = event->delta_time_pulses;
203 int event_size = event->midi_buffer_length;
204 assert(event_size > 0);
206 // Make sure we have enough scratch buffer
207 if (*size < (unsigned)event_size) {
208 *buf = (uint8_t*)realloc(*buf, event_size);
210 memcpy(*buf, event->midi_buffer, size_t(event_size));
213 /*printf("SMF::read_event:\n");
214 for (size_t i=0; i < *size; ++i) {
215 printf("%X ", (*buf)[i]);
224 template<typename Time>
226 SMF<Time>::append_event_delta(uint32_t delta_t, const Event<Time>& ev)
228 assert(ev.size() > 0);
230 /*printf("SMF::append_event_delta:\n");
231 for (size_t i=0; i < ev.size(); ++i) {
232 printf("%X ", ev.buffer()[i]);
237 event = smf_event_new_from_pointer((void *) ev.buffer(), int(ev.size()));
238 assert(event != NULL);
240 memcpy(event->midi_buffer, ev.buffer(), ev.size());
243 smf_track_add_event_delta_pulses(_smf_track, event, int(delta_t));
250 template<typename Time>
252 SMF<Time>::begin_write()
255 smf_track_delete(_smf_track);
257 _smf_track = smf_track_new();
260 smf_add_track(_smf, _smf_track);
261 assert(_smf->number_of_tracks == 1);
264 template<typename Time>
266 SMF<Time>::end_write() THROW_FILE_ERROR
268 if (smf_save(_smf, _path.c_str()) != 0)
272 template class SMF<double>;
274 } // namespace Evoral