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
25 #include "libsmf/smf.h"
26 #include "evoral/Event.hpp"
27 #include "evoral/SMF.hpp"
28 #include "evoral/midi_util.h"
31 extern double round(double x);
44 SMF::num_tracks() const
46 Glib::Threads::Mutex::Lock lm (_smf_lock);
47 return _smf ? _smf->number_of_tracks : 0;
53 Glib::Threads::Mutex::Lock lm (_smf_lock);
57 /** Seek to the specified track (1-based indexing)
58 * \return 0 on success
61 SMF::seek_to_track(int track)
63 Glib::Threads::Mutex::Lock lm (_smf_lock);
64 _smf_track = smf_get_track_by_number(_smf, track);
65 if (_smf_track != NULL) {
66 _smf_track->next_event_number = (_smf_track->number_of_events == 0) ? 0 : 1;
73 /** Attempt to open the SMF file just to see if it is valid.
75 * \return true on success
79 SMF::test(const std::string& path)
81 FILE* f = fopen(path.c_str(), "r");
86 smf_t* test_smf = smf_load(f);
89 const bool success = (test_smf != NULL);
95 /** Attempt to open the SMF file for reading and/or writing.
97 * \return 0 on success
98 * -1 if the file can not be opened or created
99 * -2 if the file exists but specified track does not exist
102 SMF::open(const std::string& path, int track) THROW_FILE_ERROR
104 Glib::Threads::Mutex::Lock lm (_smf_lock);
111 FILE* f = fopen(path.c_str(), "r");
114 } else if ((_smf = smf_load(f)) == 0) {
117 } else if ((_smf_track = smf_get_track_by_number(_smf, track)) == 0) {
122 //cerr << "Track " << track << " # events: " << _smf_track->number_of_events << endl;
123 if (_smf_track->number_of_events == 0) {
124 _smf_track->next_event_number = 0;
127 _smf_track->next_event_number = 1;
136 /** Attempt to create a new SMF file for reading and/or writing.
138 * \return 0 on success
139 * -1 if the file can not be created
140 * -2 if the track can not be created
143 SMF::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
145 Glib::Threads::Mutex::Lock lm (_smf_lock);
158 if (smf_set_ppqn(_smf, ppqn) != 0) {
162 for (int i = 0; i < track; ++i) {
163 _smf_track = smf_track_new();
167 smf_add_track(_smf, _smf_track);
170 _smf_track = smf_get_track_by_number(_smf, track);
174 _smf_track->next_event_number = 0;
177 /* put a stub file on disk */
179 FILE* f = fopen (path.c_str(), "w+");
184 if (smf_save (_smf, f)) {
197 SMF::close() THROW_FILE_ERROR
199 Glib::Threads::Mutex::Lock lm (_smf_lock);
209 SMF::seek_to_start() const
211 Glib::Threads::Mutex::Lock lm (_smf_lock);
213 _smf_track->next_event_number = std::min(_smf_track->number_of_events, (size_t)1);
215 cerr << "WARNING: SMF seek_to_start() with no track" << endl;
219 /** Read an event from the current position in file.
221 * File position MUST be at the beginning of a delta time, or this will die very messily.
222 * ev.buffer must be of size ev.size, and large enough for the event. The returned event
223 * will have it's time field set to it's delta time, in SMF tempo-based ticks, using the
224 * rate given by ppqn() (it is the caller's responsibility to calculate a real time).
226 * \a buf must be a pointer to a buffer allocated with malloc, or a pointer to NULL.
227 * \a size must be the capacity of \a buf. If it is not large enough, \a buf will
228 * be reallocated and *size will be set to the new size of buf.
230 * if the event is a meta-event and is an Evoral Note ID, then \a note_id will be set
231 * to the value of the NoteID; otherwise, meta-events will set \a note_id to -1.
233 * \return event length (including status byte) on success, 0 if event was
234 * a meta event, or -1 on EOF (or end of track).
237 SMF::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf, event_id_t* note_id) const
239 Glib::Threads::Mutex::Lock lm (_smf_lock);
248 if ((event = smf_track_get_next_event(_smf_track)) != NULL) {
250 *delta_t = event->delta_time_pulses;
252 if (smf_event_is_metadata(event)) {
253 *note_id = -1; // "no note id in this meta-event */
255 if (event->midi_buffer[1] == 0x7f) { // Sequencer-specific
260 if (smf_extract_vlq (&event->midi_buffer[2], event->midi_buffer_length-2, &evsize, &lenlen) == 0) {
262 if (event->midi_buffer[2+lenlen] == 0x99 && // Evoral
263 event->midi_buffer[3+lenlen] == 0x1) { // Evoral Note ID
268 if (smf_extract_vlq (&event->midi_buffer[4+lenlen], event->midi_buffer_length-(4+lenlen), &id, &idlen) == 0) {
274 return 0; /* this is a meta-event */
277 int event_size = event->midi_buffer_length;
278 assert(event_size > 0);
280 // Make sure we have enough scratch buffer
281 if (*size < (unsigned)event_size) {
282 *buf = (uint8_t*)realloc(*buf, event_size);
284 memcpy(*buf, event->midi_buffer, size_t(event_size));
286 if (((*buf)[0] & 0xF0) == 0x90 && (*buf)[2] == 0) {
287 /* normalize note on with velocity 0 to proper note off */
288 (*buf)[0] = 0x80 | ((*buf)[0] & 0x0F); /* note off */
289 (*buf)[2] = 0x40; /* default velocity */
292 if (!midi_event_is_valid(*buf, *size)) {
293 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
298 /* printf("SMF::read_event @ %u: ", *delta_t);
299 for (size_t i = 0; i < *size; ++i) {
300 printf("%X ", (*buf)[i]);
310 SMF::append_event_delta(uint32_t delta_t, uint32_t size, const uint8_t* buf, event_id_t note_id)
312 Glib::Threads::Mutex::Lock lm (_smf_lock);
318 /* printf("SMF::append_event_delta @ %u:", delta_t);
319 for (size_t i = 0; i < size; ++i) {
320 printf("%X ", buf[i]);
323 if (!midi_event_is_valid(buf, size)) {
324 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
330 /* XXX july 2010: currently only store event ID's for notes, program changes and bank changes
333 uint8_t const c = buf[0] & 0xf0;
334 bool const store_id = (
335 c == MIDI_CMD_NOTE_ON ||
336 c == MIDI_CMD_NOTE_OFF ||
337 c == MIDI_CMD_PGM_CHANGE ||
338 (c == MIDI_CMD_CONTROL && (buf[1] == MIDI_CTL_MSB_BANK || buf[1] == MIDI_CTL_LSB_BANK))
341 if (store_id && note_id >= 0) {
347 event = smf_event_new ();
348 assert(event != NULL);
350 /* generate VLQ representation of note ID */
351 idlen = smf_format_vlq (idbuf, sizeof(idbuf), note_id);
353 /* generate VLQ representation of meta event length,
354 which is the idlen + 2 bytes (Evoral type ID plus Note ID type)
357 lenlen = smf_format_vlq (lenbuf, sizeof(lenbuf), idlen+2);
359 event->midi_buffer_length = 2 + lenlen + 2 + idlen;
360 /* this should be allocated by malloc(3) because libsmf will
363 event->midi_buffer = (uint8_t*) malloc (sizeof(uint8_t) * event->midi_buffer_length);
365 event->midi_buffer[0] = 0xff; // Meta-event
366 event->midi_buffer[1] = 0x7f; // Sequencer-specific
367 memcpy (&event->midi_buffer[2], lenbuf, lenlen);
368 event->midi_buffer[2+lenlen] = 0x99; // Evoral type ID
369 event->midi_buffer[3+lenlen] = 0x1; // Evoral type Note ID
370 memcpy (&event->midi_buffer[4+lenlen], idbuf, idlen);
373 smf_track_add_event_delta_pulses(_smf_track, event, 0);
376 event = smf_event_new_from_pointer(buf, size);
377 assert(event != NULL);
380 smf_track_add_event_delta_pulses(_smf_track, event, delta_t);
387 Glib::Threads::Mutex::Lock lm (_smf_lock);
390 smf_track_delete(_smf_track);
392 _smf_track = smf_track_new();
395 smf_add_track(_smf, _smf_track);
396 assert(_smf->number_of_tracks == 1);
400 SMF::end_write(string const & path) THROW_FILE_ERROR
402 Glib::Threads::Mutex::Lock lm (_smf_lock);
408 FILE* f = fopen (path.c_str(), "w+");
410 throw FileError (path);
413 if (smf_save(_smf, f) != 0) {
415 throw FileError (path);
422 SMF::round_to_file_precision (double val) const
426 return round (val * div) / div;
429 } // namespace Evoral