1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David 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
24 #include "libsmf/smf.h"
25 #include "evoral/Event.hpp"
26 #include "evoral/SMF.hpp"
27 #include "evoral/midi_util.h"
28 #include "pbd/file_manager.h"
44 SMF::num_tracks() const
46 return _smf->number_of_tracks;
55 /** Seek to the specified track (1-based indexing)
56 * \return 0 on success
59 SMF::seek_to_track(int track)
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;
70 /** Attempt to open the SMF file for reading and/or writing.
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
77 SMF::open(const std::string& path, int track) THROW_FILE_ERROR
86 PBD::StdioFileDescriptor d (_file_path, "r");
87 FILE* f = d.allocate ();
92 if ((_smf = smf_load (f)) == 0) {
96 if ((_smf_track = smf_get_track_by_number(_smf, track)) == 0) {
100 //cerr << "Track " << track << " # events: " << _smf_track->number_of_events << endl;
101 if (_smf_track->number_of_events == 0) {
102 _smf_track->next_event_number = 0;
105 _smf_track->next_event_number = 1;
113 /** Attempt to create a new SMF file for reading and/or writing.
115 * \return 0 on success
116 * -1 if the file can not be created
117 * -2 if the track can not be created
120 SMF::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
135 if (smf_set_ppqn(_smf, ppqn) != 0) {
139 for (int i = 0; i < track; ++i) {
140 _smf_track = smf_track_new();
142 smf_add_track(_smf, _smf_track);
145 _smf_track = smf_get_track_by_number(_smf, track);
149 _smf_track->next_event_number = 0;
152 /* put a stub file on disk */
154 PBD::StdioFileDescriptor d (_file_path, "w+");
155 FILE* f = d.allocate ();
160 if (smf_save (_smf, f)) {
171 SMF::close() THROW_FILE_ERROR
181 SMF::seek_to_start() const
183 _smf_track->next_event_number = 1;
186 /** Read an event from the current position in file.
188 * File position MUST be at the beginning of a delta time, or this will die very messily.
189 * ev.buffer must be of size ev.size, and large enough for the event. The returned event
190 * will have it's time field set to it's delta time, in SMF tempo-based ticks, using the
191 * rate given by ppqn() (it is the caller's responsibility to calculate a real time).
193 * \a buf must be a pointer to a buffer allocated with malloc, or a pointer to NULL.
194 * \a size must be the capacity of \a buf. If it is not large enough, \a buf will
195 * be reallocated and *size will be set to the new size of buf.
197 * if the event is a meta-event and is an Evoral Note ID, then \a note_id will be set
198 * to the value of the NoteID; otherwise, meta-events will set \a note_id to -1.
200 * \return event length (including status byte) on success, 0 if event was
201 * a meta event, or -1 on EOF (or end of track).
204 SMF::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf, event_id_t* note_id) const
213 if ((event = smf_track_get_next_event(_smf_track)) != NULL) {
215 *delta_t = event->delta_time_pulses;
217 if (smf_event_is_metadata(event)) {
218 *note_id = -1; // "no note id in this meta-event */
220 if (event->midi_buffer[1] == 0x7f) { // Sequencer-specific
225 if (smf_extract_vlq (&event->midi_buffer[2], event->midi_buffer_length-2, &evsize, &lenlen) == 0) {
227 if (event->midi_buffer[2+lenlen] == 0x99 && // Evoral
228 event->midi_buffer[3+lenlen] == 0x1) { // Evoral Note ID
233 if (smf_extract_vlq (&event->midi_buffer[4+lenlen], event->midi_buffer_length-(4+lenlen), &id, &idlen) == 0) {
239 return 0; /* this is a meta-event */
242 int event_size = event->midi_buffer_length;
243 assert(event_size > 0);
245 // Make sure we have enough scratch buffer
246 if (*size < (unsigned)event_size) {
247 *buf = (uint8_t*)realloc(*buf, event_size);
249 memcpy(*buf, event->midi_buffer, size_t(event_size));
252 assert(midi_event_is_valid(*buf, *size));
254 /* printf("SMF::read_event @ %u: ", *delta_t);
255 for (size_t i = 0; i < *size; ++i) {
256 printf("%X ", (*buf)[i]);
266 SMF::append_event_delta(uint32_t delta_t, uint32_t size, const uint8_t* buf, event_id_t note_id)
272 /* printf("SMF::append_event_delta @ %u:", delta_t);
273 for (size_t i = 0; i < size; ++i) {
274 printf("%X ", buf[i]);
277 if (!midi_event_is_valid(buf, size)) {
278 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
284 /* XXX july 2010: currently only store event ID's for notes, program changes and bank changes
287 uint8_t const c = buf[0] & 0xf0;
288 bool const store_id = (
289 c == MIDI_CMD_NOTE_ON ||
290 c == MIDI_CMD_NOTE_OFF ||
291 c == MIDI_CMD_PGM_CHANGE ||
292 (c == MIDI_CMD_CONTROL && (buf[1] == MIDI_CTL_MSB_BANK || buf[1] == MIDI_CTL_LSB_BANK))
295 if (store_id && note_id >= 0) {
301 event = smf_event_new ();
302 assert(event != NULL);
304 /* generate VLQ representation of note ID */
305 idlen = smf_format_vlq (idbuf, sizeof(idbuf), note_id);
307 /* generate VLQ representation of meta event length,
308 which is the idlen + 2 bytes (Evoral type ID plus Note ID type)
311 lenlen = smf_format_vlq (lenbuf, sizeof(lenbuf), idlen+2);
313 event->midi_buffer_length = 2 + lenlen + 2 + idlen;
314 /* this should be allocated by malloc(3) because libsmf will
317 event->midi_buffer = (uint8_t*) malloc (sizeof (uint8_t*) * event->midi_buffer_length);
319 event->midi_buffer[0] = 0xff; // Meta-event
320 event->midi_buffer[1] = 0x7f; // Sequencer-specific
321 memcpy (&event->midi_buffer[2], lenbuf, lenlen);
322 event->midi_buffer[2+lenlen] = 0x99; // Evoral type ID
323 event->midi_buffer[3+lenlen] = 0x1; // Evoral type Note ID
324 memcpy (&event->midi_buffer[4+lenlen], idbuf, idlen);
327 smf_track_add_event_delta_pulses(_smf_track, event, 0);
330 event = smf_event_new_from_pointer(buf, size);
331 assert(event != NULL);
334 smf_track_add_event_delta_pulses(_smf_track, event, delta_t);
342 smf_track_delete(_smf_track);
344 _smf_track = smf_track_new();
347 smf_add_track(_smf, _smf_track);
348 assert(_smf->number_of_tracks == 1);
352 SMF::end_write() THROW_FILE_ERROR
354 PBD::StdioFileDescriptor d (_file_path, "w+");
355 FILE* f = d.allocate ();
357 throw FileError (_file_path);
360 if (smf_save(_smf, f) != 0) {
361 throw FileError (_file_path);
366 SMF::round_to_file_precision (double val) const
370 return round (val * div) / div;
374 SMF::set_path (const std::string& p)
379 } // namespace Evoral