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);
104 smf_delete(test_smf);
109 /** Attempt to open the SMF file for reading and/or writing.
111 * \return 0 on success
112 * -1 if the file can not be opened or created
113 * -2 if the file exists but specified track does not exist
116 SMF::open(const std::string& path, int track) THROW_FILE_ERROR
118 Glib::Threads::Mutex::Lock lm (_smf_lock);
121 _type0channels.clear ();
128 FILE* f = g_fopen(path.c_str(), "r");
131 } else if ((_smf = smf_load(f)) == 0) {
134 } else if ((_smf_track = smf_get_track_by_number(_smf, track)) == 0) {
139 //cerr << "Track " << track << " # events: " << _smf_track->number_of_events << endl;
140 if (_smf_track->number_of_events == 0) {
141 _smf_track->next_event_number = 0;
144 _smf_track->next_event_number = 1;
151 if (_smf->format == 0 && _smf->number_of_tracks == 1 && !_empty) {
152 // type-0 file: scan file for # of used channels.
154 uint32_t delta_t = 0;
157 event_id_t event_id = 0;
159 while ((ret = read_event (&delta_t, &size, &buf, &event_id)) >= 0) {
166 uint8_t type = buf[0] & 0xf0;
167 uint8_t chan = buf[0] & 0x0f;
168 if (type < 0x80 || type > 0xE0) {
171 _type0channels.insert(chan);
180 /** Attempt to create a new SMF file for reading and/or writing.
182 * \return 0 on success
183 * -1 if the file can not be created
184 * -2 if the track can not be created
187 SMF::create(const std::string& path, int track, uint16_t ppqn) THROW_FILE_ERROR
189 Glib::Threads::Mutex::Lock lm (_smf_lock);
202 if (smf_set_ppqn(_smf, ppqn) != 0) {
206 for (int i = 0; i < track; ++i) {
207 _smf_track = smf_track_new();
211 smf_add_track(_smf, _smf_track);
214 _smf_track = smf_get_track_by_number(_smf, track);
218 _smf_track->next_event_number = 0;
221 /* put a stub file on disk */
223 FILE* f = g_fopen (path.c_str(), "w+");
228 if (smf_save (_smf, f)) {
237 _type0channels.clear ();
243 SMF::close() THROW_FILE_ERROR
245 Glib::Threads::Mutex::Lock lm (_smf_lock);
252 _type0channels.clear ();
257 SMF::seek_to_start() const
259 Glib::Threads::Mutex::Lock lm (_smf_lock);
261 _smf_track->next_event_number = std::min(_smf_track->number_of_events, (size_t)1);
263 cerr << "WARNING: SMF seek_to_start() with no track" << endl;
267 /** Read an event from the current position in file.
269 * File position MUST be at the beginning of a delta time, or this will die very messily.
270 * ev.buffer must be of size ev.size, and large enough for the event. The returned event
271 * will have it's time field set to it's delta time, in SMF tempo-based ticks, using the
272 * rate given by ppqn() (it is the caller's responsibility to calculate a real time).
274 * \a buf must be a pointer to a buffer allocated with malloc, or a pointer to NULL.
275 * \a size must be the capacity of \a buf. If it is not large enough, \a buf will
276 * be reallocated and *size will be set to the new size of buf.
278 * if the event is a meta-event and is an Evoral Note ID, then \a note_id will be set
279 * to the value of the NoteID; otherwise, meta-events will set \a note_id to -1.
281 * \return event length (including status byte) on success, 0 if event was
282 * a meta event, or -1 on EOF (or end of track).
285 SMF::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf, event_id_t* note_id) const
287 Glib::Threads::Mutex::Lock lm (_smf_lock);
296 if ((event = smf_track_get_next_event(_smf_track)) != NULL) {
298 *delta_t = event->delta_time_pulses;
300 if (smf_event_is_metadata(event)) {
301 *note_id = -1; // "no note id in this meta-event */
303 if (event->midi_buffer[1] == 0x7f) { // Sequencer-specific
308 if (smf_extract_vlq (&event->midi_buffer[2], event->midi_buffer_length-2, &evsize, &lenlen) == 0) {
310 if (event->midi_buffer[2+lenlen] == 0x99 && // Evoral
311 event->midi_buffer[3+lenlen] == 0x1) { // Evoral Note ID
316 if (smf_extract_vlq (&event->midi_buffer[4+lenlen], event->midi_buffer_length-(4+lenlen), &id, &idlen) == 0) {
322 return 0; /* this is a meta-event */
325 int event_size = event->midi_buffer_length;
326 assert(event_size > 0);
328 // Make sure we have enough scratch buffer
329 if (*size < (unsigned)event_size) {
330 *buf = (uint8_t*)realloc(*buf, event_size);
333 memcpy(*buf, event->midi_buffer, size_t(event_size));
335 if (((*buf)[0] & 0xF0) == 0x90 && (*buf)[2] == 0) {
336 /* normalize note on with velocity 0 to proper note off */
337 (*buf)[0] = 0x80 | ((*buf)[0] & 0x0F); /* note off */
338 (*buf)[2] = 0x40; /* default velocity */
341 if (!midi_event_is_valid(*buf, *size)) {
342 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
347 /* printf("SMF::read_event @ %u: ", *delta_t);
348 for (size_t i = 0; i < *size; ++i) {
349 printf("%X ", (*buf)[i]);
359 SMF::append_event_delta(uint32_t delta_t, uint32_t size, const uint8_t* buf, event_id_t note_id)
361 Glib::Threads::Mutex::Lock lm (_smf_lock);
367 /* printf("SMF::append_event_delta @ %u:", delta_t);
368 for (size_t i = 0; i < size; ++i) {
369 printf("%X ", buf[i]);
387 /* System Real Time or System Common event: not valid in SMF
392 if (!midi_event_is_valid(buf, size)) {
393 cerr << "WARNING: SMF ignoring illegal MIDI event" << endl;
399 /* XXX july 2010: currently only store event ID's for notes, program changes and bank changes
402 uint8_t const c = buf[0] & 0xf0;
403 bool const store_id = (
404 c == MIDI_CMD_NOTE_ON ||
405 c == MIDI_CMD_NOTE_OFF ||
406 c == MIDI_CMD_NOTE_PRESSURE ||
407 c == MIDI_CMD_PGM_CHANGE ||
408 (c == MIDI_CMD_CONTROL && (buf[1] == MIDI_CTL_MSB_BANK || buf[1] == MIDI_CTL_LSB_BANK))
411 if (store_id && note_id >= 0) {
417 event = smf_event_new ();
418 assert(event != NULL);
420 /* generate VLQ representation of note ID */
421 idlen = smf_format_vlq (idbuf, sizeof(idbuf), note_id);
423 /* generate VLQ representation of meta event length,
424 which is the idlen + 2 bytes (Evoral type ID plus Note ID type)
427 lenlen = smf_format_vlq (lenbuf, sizeof(lenbuf), idlen+2);
429 event->midi_buffer_length = 2 + lenlen + 2 + idlen;
430 /* this should be allocated by malloc(3) because libsmf will
433 event->midi_buffer = (uint8_t*) malloc (sizeof(uint8_t) * event->midi_buffer_length);
435 event->midi_buffer[0] = 0xff; // Meta-event
436 event->midi_buffer[1] = 0x7f; // Sequencer-specific
437 memcpy (&event->midi_buffer[2], lenbuf, lenlen);
438 event->midi_buffer[2+lenlen] = 0x99; // Evoral type ID
439 event->midi_buffer[3+lenlen] = 0x1; // Evoral type Note ID
440 memcpy (&event->midi_buffer[4+lenlen], idbuf, idlen);
443 smf_track_add_event_delta_pulses(_smf_track, event, 0);
446 event = smf_event_new_from_pointer(buf, size);
447 assert(event != NULL);
450 smf_track_add_event_delta_pulses(_smf_track, event, delta_t);
457 Glib::Threads::Mutex::Lock lm (_smf_lock);
460 smf_track_delete(_smf_track);
462 _smf_track = smf_track_new();
465 smf_add_track(_smf, _smf_track);
466 assert(_smf->number_of_tracks == 1);
470 SMF::end_write(string const & path) THROW_FILE_ERROR
472 Glib::Threads::Mutex::Lock lm (_smf_lock);
478 FILE* f = g_fopen (path.c_str(), "w+");
480 throw FileError (path);
483 if (smf_save(_smf, f) != 0) {
485 throw FileError (path);
492 SMF::round_to_file_precision (double val) const
496 return round (val * div) / div;
500 SMF::track_names(vector<string>& names) const
508 Glib::Threads::Mutex::Lock lm (_smf_lock);
510 for (uint16_t n = 0; n < _smf->number_of_tracks; ++n) {
511 smf_track_t* trk = smf_get_track_by_number (_smf, n+1);
513 names.push_back (string());
516 names.push_back (trk->name);
518 names.push_back (string());
525 SMF::instrument_names(vector<string>& names) const
533 Glib::Threads::Mutex::Lock lm (_smf_lock);
535 for (uint16_t n = 0; n < _smf->number_of_tracks; ++n) {
536 smf_track_t* trk = smf_get_track_by_number (_smf, n+1);
538 names.push_back (string());
540 if (trk->instrument) {
541 names.push_back (trk->instrument);
543 names.push_back (string());
549 SMF::Tempo::Tempo (smf_tempo_t* smft)
550 : time_pulses (smft->time_pulses)
551 , time_seconds (smft->time_seconds)
552 , microseconds_per_quarter_note (smft->microseconds_per_quarter_note)
553 , numerator (smft->numerator)
554 , denominator (smft->denominator)
555 , clocks_per_click (smft->clocks_per_click)
556 , notes_per_note (smft->notes_per_note)
561 SMF::num_tempos () const
564 return smf_get_tempo_count (_smf);
568 SMF::tempo_at_smf_pulse (size_t smf_pulse) const
570 smf_tempo_t* t = smf_get_tempo_by_seconds (_smf, smf_pulse);
574 return new Tempo (t);
578 SMF::tempo_at_seconds (double seconds) const
580 smf_tempo_t* t = smf_get_tempo_by_seconds (_smf, seconds);
584 return new Tempo (t);
588 SMF::nth_tempo (size_t n) const
592 smf_tempo_t* t = smf_get_tempo_by_number (_smf, n);
597 return new Tempo (t);
600 } // namespace Evoral