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
26 #include <glib/gstdio.h>
28 #include "libsmf/smf.h"
30 #include "evoral/Event.hpp"
31 #include "evoral/SMF.hpp"
32 #include "evoral/midi_util.h"
35 extern double round(double x);
55 SMF::num_tracks() const
57 Glib::Threads::Mutex::Lock lm (_smf_lock);
58 return _smf ? _smf->number_of_tracks : 0;
64 Glib::Threads::Mutex::Lock lm (_smf_lock);
68 /** Seek to the specified track (1-based indexing)
69 * \return 0 on success
72 SMF::seek_to_track(int track)
74 Glib::Threads::Mutex::Lock lm (_smf_lock);
75 _smf_track = smf_get_track_by_number(_smf, track);
76 if (_smf_track != NULL) {
77 _smf_track->next_event_number = (_smf_track->number_of_events == 0) ? 0 : 1;
84 /** Attempt to open the SMF file just to see if it is valid.
86 * \return true on success
90 SMF::test(const std::string& path)
92 FILE* f = g_fopen(path.c_str(), "r");
97 smf_t* test_smf = smf_load(f);
100 const bool success = (test_smf != NULL);
101 smf_delete(test_smf);
106 /** Attempt to open the SMF file for reading and/or writing.
108 * \return 0 on success
109 * -1 if the file can not be opened or created
110 * -2 if the file exists but specified track does not exist
113 SMF::open(const std::string& path, int track) THROW_FILE_ERROR
115 Glib::Threads::Mutex::Lock lm (_smf_lock);
118 _type0channels.clear ();
125 FILE* f = g_fopen(path.c_str(), "r");
128 } else if ((_smf = smf_load(f)) == 0) {
131 } else if ((_smf_track = smf_get_track_by_number(_smf, track)) == 0) {
136 //cerr << "Track " << track << " # events: " << _smf_track->number_of_events << endl;
137 if (_smf_track->number_of_events == 0) {
138 _smf_track->next_event_number = 0;
141 _smf_track->next_event_number = 1;
148 if (_smf->format == 0 && _smf->number_of_tracks == 1 && !_empty) {
149 // type-0 file: scan file for # of used channels.
151 uint32_t delta_t = 0;
154 event_id_t event_id = 0;
156 while ((ret = read_event (&delta_t, &size, &buf, &event_id)) >= 0) {
163 uint8_t type = buf[0] & 0xf0;
164 uint8_t chan = buf[0] & 0x0f;
165 if (type < 0x80 || type > 0xE0) {
168 _type0channels.insert(chan);
177 /** Attempt to create a new SMF file for reading and/or writing.
179 * \return 0 on success
180 * -1 if the file can not be created
181 * -2 if the track can not be created
184 SMF::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
186 Glib::Threads::Mutex::Lock lm (_smf_lock);
199 if (smf_set_ppqn(_smf, ppqn) != 0) {
203 for (int i = 0; i < track; ++i) {
204 _smf_track = smf_track_new();
208 smf_add_track(_smf, _smf_track);
211 _smf_track = smf_get_track_by_number(_smf, track);
215 _smf_track->next_event_number = 0;
218 /* put a stub file on disk */
220 FILE* f = g_fopen (path.c_str(), "w+");
225 if (smf_save (_smf, f)) {
234 _type0channels.clear ();
240 SMF::close() THROW_FILE_ERROR
242 Glib::Threads::Mutex::Lock lm (_smf_lock);
249 _type0channels.clear ();
254 SMF::seek_to_start() const
256 Glib::Threads::Mutex::Lock lm (_smf_lock);
258 _smf_track->next_event_number = std::min(_smf_track->number_of_events, (size_t)1);
260 cerr << "WARNING: SMF seek_to_start() with no track" << endl;
264 /** Read an event from the current position in file.
266 * File position MUST be at the beginning of a delta time, or this will die very messily.
267 * ev.buffer must be of size ev.size, and large enough for the event. The returned event
268 * will have it's time field set to it's delta time, in SMF tempo-based ticks, using the
269 * rate given by ppqn() (it is the caller's responsibility to calculate a real time).
271 * \a buf must be a pointer to a buffer allocated with malloc, or a pointer to NULL.
272 * \a size must be the capacity of \a buf. If it is not large enough, \a buf will
273 * be reallocated and *size will be set to the new size of buf.
275 * if the event is a meta-event and is an Evoral Note ID, then \a note_id will be set
276 * to the value of the NoteID; otherwise, meta-events will set \a note_id to -1.
278 * \return event length (including status byte) on success, 0 if event was
279 * a meta event, or -1 on EOF (or end of track).
282 SMF::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf, event_id_t* note_id) const
284 Glib::Threads::Mutex::Lock lm (_smf_lock);
293 if ((event = smf_track_get_next_event(_smf_track)) != NULL) {
295 *delta_t = event->delta_time_pulses;
297 if (smf_event_is_metadata(event)) {
298 *note_id = -1; // "no note id in this meta-event */
300 if (event->midi_buffer[1] == 0x7f) { // Sequencer-specific
305 if (smf_extract_vlq (&event->midi_buffer[2], event->midi_buffer_length-2, &evsize, &lenlen) == 0) {
307 if (event->midi_buffer[2+lenlen] == 0x99 && // Evoral
308 event->midi_buffer[3+lenlen] == 0x1) { // Evoral Note ID
313 if (smf_extract_vlq (&event->midi_buffer[4+lenlen], event->midi_buffer_length-(4+lenlen), &id, &idlen) == 0) {
319 return 0; /* this is a meta-event */
322 int event_size = event->midi_buffer_length;
323 assert(event_size > 0);
325 // Make sure we have enough scratch buffer
326 if (*size < (unsigned)event_size) {
327 *buf = (uint8_t*)realloc(*buf, event_size);
329 memcpy(*buf, event->midi_buffer, size_t(event_size));
331 if (((*buf)[0] & 0xF0) == 0x90 && (*buf)[2] == 0) {
332 /* normalize note on with velocity 0 to proper note off */
333 (*buf)[0] = 0x80 | ((*buf)[0] & 0x0F); /* note off */
334 (*buf)[2] = 0x40; /* default velocity */
337 if (!midi_event_is_valid(*buf, *size)) {
338 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
343 /* printf("SMF::read_event @ %u: ", *delta_t);
344 for (size_t i = 0; i < *size; ++i) {
345 printf("%X ", (*buf)[i]);
355 SMF::append_event_delta(uint32_t delta_t, uint32_t size, const uint8_t* buf, event_id_t note_id)
357 Glib::Threads::Mutex::Lock lm (_smf_lock);
363 /* printf("SMF::append_event_delta @ %u:", delta_t);
364 for (size_t i = 0; i < size; ++i) {
365 printf("%X ", buf[i]);
368 if (!midi_event_is_valid(buf, size)) {
369 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
375 /* XXX july 2010: currently only store event ID's for notes, program changes and bank changes
378 uint8_t const c = buf[0] & 0xf0;
379 bool const store_id = (
380 c == MIDI_CMD_NOTE_ON ||
381 c == MIDI_CMD_NOTE_OFF ||
382 c == MIDI_CMD_NOTE_PRESSURE ||
383 c == MIDI_CMD_PGM_CHANGE ||
384 (c == MIDI_CMD_CONTROL && (buf[1] == MIDI_CTL_MSB_BANK || buf[1] == MIDI_CTL_LSB_BANK))
387 if (store_id && note_id >= 0) {
393 event = smf_event_new ();
394 assert(event != NULL);
396 /* generate VLQ representation of note ID */
397 idlen = smf_format_vlq (idbuf, sizeof(idbuf), note_id);
399 /* generate VLQ representation of meta event length,
400 which is the idlen + 2 bytes (Evoral type ID plus Note ID type)
403 lenlen = smf_format_vlq (lenbuf, sizeof(lenbuf), idlen+2);
405 event->midi_buffer_length = 2 + lenlen + 2 + idlen;
406 /* this should be allocated by malloc(3) because libsmf will
409 event->midi_buffer = (uint8_t*) malloc (sizeof(uint8_t) * event->midi_buffer_length);
411 event->midi_buffer[0] = 0xff; // Meta-event
412 event->midi_buffer[1] = 0x7f; // Sequencer-specific
413 memcpy (&event->midi_buffer[2], lenbuf, lenlen);
414 event->midi_buffer[2+lenlen] = 0x99; // Evoral type ID
415 event->midi_buffer[3+lenlen] = 0x1; // Evoral type Note ID
416 memcpy (&event->midi_buffer[4+lenlen], idbuf, idlen);
419 smf_track_add_event_delta_pulses(_smf_track, event, 0);
422 event = smf_event_new_from_pointer(buf, size);
423 assert(event != NULL);
426 smf_track_add_event_delta_pulses(_smf_track, event, delta_t);
433 Glib::Threads::Mutex::Lock lm (_smf_lock);
436 smf_track_delete(_smf_track);
438 _smf_track = smf_track_new();
441 smf_add_track(_smf, _smf_track);
442 assert(_smf->number_of_tracks == 1);
446 SMF::end_write(string const & path) THROW_FILE_ERROR
448 Glib::Threads::Mutex::Lock lm (_smf_lock);
454 FILE* f = g_fopen (path.c_str(), "w+");
456 throw FileError (path);
459 if (smf_save(_smf, f) != 0) {
461 throw FileError (path);
468 SMF::round_to_file_precision (double val) const
472 return round (val * div) / div;
476 SMF::track_names(vector<string>& names) const
484 Glib::Threads::Mutex::Lock lm (_smf_lock);
486 for (uint16_t n = 0; n < _smf->number_of_tracks; ++n) {
487 smf_track_t* trk = smf_get_track_by_number (_smf, n+1);
489 names.push_back (string());
492 names.push_back (trk->name);
494 names.push_back (string());
501 SMF::instrument_names(vector<string>& names) const
509 Glib::Threads::Mutex::Lock lm (_smf_lock);
511 for (uint16_t n = 0; n < _smf->number_of_tracks; ++n) {
512 smf_track_t* trk = smf_get_track_by_number (_smf, n+1);
514 names.push_back (string());
516 if (trk->instrument) {
517 names.push_back (trk->instrument);
519 names.push_back (string());
525 SMF::Tempo::Tempo (smf_tempo_t* smft)
526 : time_pulses (smft->time_pulses)
527 , time_seconds (smft->time_seconds)
528 , microseconds_per_quarter_note (smft->microseconds_per_quarter_note)
529 , numerator (smft->numerator)
530 , denominator (smft->denominator)
531 , clocks_per_click (smft->clocks_per_click)
532 , notes_per_note (smft->notes_per_note)
537 SMF::num_tempos () const
540 return smf_get_tempo_count (_smf);
544 SMF::tempo_at_smf_pulse (size_t smf_pulse) const
546 smf_tempo_t* t = smf_get_tempo_by_seconds (_smf, smf_pulse);
550 return new Tempo (t);
554 SMF::tempo_at_seconds (double seconds) const
556 smf_tempo_t* t = smf_get_tempo_by_seconds (_smf, seconds);
560 return new Tempo (t);
564 SMF::nth_tempo (size_t n) const
568 smf_tempo_t* t = smf_get_tempo_by_number (_smf, n);
573 return new Tempo (t);
576 } // namespace Evoral