2008-06-02 21:41:35 +00:00
|
|
|
/*
|
|
|
|
|
Copyright (C) 2006 Paul Davis
|
|
|
|
|
Written by Dave Robillard, 2006
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <pbd/mountpoint.h>
|
|
|
|
|
#include <pbd/pathscanner.h>
|
|
|
|
|
#include <pbd/stl_delete.h>
|
|
|
|
|
#include <pbd/strsplit.h>
|
|
|
|
|
|
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
|
|
2008-09-30 00:45:26 +00:00
|
|
|
#include <evoral/SMFReader.hpp>
|
2009-01-30 20:18:31 +00:00
|
|
|
#include <evoral/Control.hpp>
|
2008-09-30 00:45:26 +00:00
|
|
|
|
2008-06-02 21:41:35 +00:00
|
|
|
#include <ardour/audioengine.h>
|
2008-09-22 16:28:02 +00:00
|
|
|
#include <ardour/event_type_map.h>
|
2009-02-15 19:44:27 +00:00
|
|
|
#include <ardour/midi_model.h>
|
|
|
|
|
#include <ardour/midi_ring_buffer.h>
|
|
|
|
|
#include <ardour/session.h>
|
|
|
|
|
#include <ardour/smf_source.h>
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
using namespace ARDOUR;
|
2009-02-17 02:11:49 +00:00
|
|
|
using namespace Glib;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 17:30:42 +00:00
|
|
|
/** Constructor used for new internal-to-session files. File cannot exist. */
|
2009-02-17 02:11:49 +00:00
|
|
|
SMFSource::SMFSource (Session& s, const ustring& path, bool embedded, Source::Flag flags)
|
|
|
|
|
: Source(s, DataType::MIDI, path, flags)
|
|
|
|
|
, MidiSource(s, path)
|
|
|
|
|
, FileSource(s, DataType::MIDI, path, embedded, flags)
|
2009-02-15 01:53:06 +00:00
|
|
|
, Evoral::SMF()
|
2009-02-15 17:30:42 +00:00
|
|
|
, _last_ev_time_beats(0.0)
|
|
|
|
|
, _last_ev_time_frames(0)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-02-17 02:11:49 +00:00
|
|
|
if (init(_name, false)) {
|
2008-06-02 21:41:35 +00:00
|
|
|
throw failed_constructor ();
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-14 20:52:15 +00:00
|
|
|
if (create(path)) {
|
2008-06-02 21:41:35 +00:00
|
|
|
throw failed_constructor ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-17 02:11:49 +00:00
|
|
|
/** Constructor used for existing internal-to-session files. */
|
|
|
|
|
SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
|
|
|
|
|
: Source(s, node)
|
|
|
|
|
, MidiSource(s, node)
|
|
|
|
|
, FileSource(s, node, must_exist)
|
2009-02-15 17:30:42 +00:00
|
|
|
, _last_ev_time_beats(0.0)
|
|
|
|
|
, _last_ev_time_frames(0)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-02-15 01:53:06 +00:00
|
|
|
if (set_state(node)) {
|
2008-06-02 21:41:35 +00:00
|
|
|
throw failed_constructor ();
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 01:53:06 +00:00
|
|
|
if (init(_name, true)) {
|
2008-06-02 21:41:35 +00:00
|
|
|
throw failed_constructor ();
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-30 00:45:26 +00:00
|
|
|
if (open(_path)) {
|
2008-06-02 21:41:35 +00:00
|
|
|
throw failed_constructor ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SMFSource::~SMFSource ()
|
|
|
|
|
{
|
|
|
|
|
if (removable()) {
|
|
|
|
|
unlink (_path.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** All stamps in audio frames */
|
|
|
|
|
nframes_t
|
2009-02-19 05:45:11 +00:00
|
|
|
SMFSource::read_unlocked (MidiRingBuffer<nframes_t>& dst, sframes_t position,
|
|
|
|
|
sframes_t start, nframes_t dur,
|
|
|
|
|
sframes_t stamp_offset, sframes_t negative_stamp_offset) const
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-02-17 02:11:49 +00:00
|
|
|
int ret = 0;
|
2008-06-02 21:41:35 +00:00
|
|
|
uint64_t time = 0; // in SMF ticks, 1 tick per _ppqn
|
|
|
|
|
|
|
|
|
|
_read_data_count = 0;
|
|
|
|
|
|
|
|
|
|
// Output parameters for read_event (which will allocate scratch in buffer as needed)
|
|
|
|
|
uint32_t ev_delta_t = 0;
|
2009-02-15 01:53:06 +00:00
|
|
|
uint32_t ev_type = 0;
|
|
|
|
|
uint32_t ev_size = 0;
|
|
|
|
|
uint8_t* ev_buffer = 0;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
size_t scratch_size = 0; // keep track of scratch to minimize reallocs
|
2009-02-19 04:12:54 +00:00
|
|
|
|
|
|
|
|
BeatsFramesConverter converter(_session, position);
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-19 04:12:54 +00:00
|
|
|
const uint64_t start_ticks = (uint64_t)(converter.from(start) * ppqn());
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 02:09:58 +00:00
|
|
|
if (_last_read_end == 0 || start != _last_read_end) {
|
|
|
|
|
cerr << "SMFSource::read_unlocked seeking to " << start << endl;
|
|
|
|
|
Evoral::SMF::seek_to_start();
|
|
|
|
|
while (time < start_ticks) {
|
|
|
|
|
ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
|
|
|
|
|
if (ret == -1) { // EOF
|
|
|
|
|
_last_read_end = start + dur;
|
|
|
|
|
return dur;
|
|
|
|
|
}
|
|
|
|
|
time += ev_delta_t; // accumulate delta time
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_last_read_end = start + dur;
|
|
|
|
|
|
2009-02-15 19:44:27 +00:00
|
|
|
while (true) {
|
2009-02-15 02:09:58 +00:00
|
|
|
ret = read_event(&ev_delta_t, &ev_size, &ev_buffer);
|
2008-06-02 21:41:35 +00:00
|
|
|
if (ret == -1) { // EOF
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time += ev_delta_t; // accumulate delta time
|
|
|
|
|
|
|
|
|
|
if (ret == 0) { // meta-event (skipped, just accumulate time)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2009-02-15 20:50:20 +00:00
|
|
|
|
|
|
|
|
ev_type = EventTypeMap::instance().midi_event_type(ev_buffer[0]);
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 02:09:58 +00:00
|
|
|
assert(time >= start_ticks);
|
2009-02-19 05:45:11 +00:00
|
|
|
const sframes_t ev_frame_time = converter.to(time / (double)ppqn()) + stamp_offset;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 02:09:58 +00:00
|
|
|
if (ev_frame_time < start + dur) {
|
|
|
|
|
dst.write(ev_frame_time - negative_stamp_offset, ev_type, ev_size, ev_buffer);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_read_data_count += ev_size;
|
|
|
|
|
|
2009-02-15 01:53:06 +00:00
|
|
|
if (ev_size > scratch_size) {
|
2008-06-02 21:41:35 +00:00
|
|
|
scratch_size = ev_size;
|
2009-02-15 01:53:06 +00:00
|
|
|
}
|
2009-02-15 03:51:49 +00:00
|
|
|
ev_size = scratch_size; // ensure read_event only allocates if necessary
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-15 02:09:58 +00:00
|
|
|
return dur;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** All stamps in audio frames */
|
|
|
|
|
nframes_t
|
2009-02-19 05:45:11 +00:00
|
|
|
SMFSource::write_unlocked (MidiRingBuffer<nframes_t>& src, sframes_t position, nframes_t dur)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
|
|
|
|
_write_data_count = 0;
|
|
|
|
|
|
2009-02-15 01:24:26 +00:00
|
|
|
nframes_t time;
|
2008-10-13 02:43:14 +00:00
|
|
|
Evoral::EventType type;
|
|
|
|
|
uint32_t size;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 01:53:06 +00:00
|
|
|
size_t buf_capacity = 4;
|
|
|
|
|
uint8_t* buf = (uint8_t*)malloc(buf_capacity);
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 01:53:06 +00:00
|
|
|
if (_model && ! _model->writing()) {
|
2008-06-02 21:41:35 +00:00
|
|
|
_model->start_write();
|
2009-02-15 01:53:06 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-20 00:30:42 +00:00
|
|
|
Evoral::MIDIEvent<nframes_t> ev;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
while (true) {
|
2008-09-22 16:28:02 +00:00
|
|
|
bool ret = src.peek_time(&time);
|
2009-02-20 00:30:42 +00:00
|
|
|
if (!ret || time > _last_write_end + dur) {
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
2009-02-15 01:53:06 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2008-09-22 16:28:02 +00:00
|
|
|
ret = src.read_prefix(&time, &type, &size);
|
2009-02-15 01:53:06 +00:00
|
|
|
if (!ret) {
|
2009-02-20 00:30:42 +00:00
|
|
|
cerr << "ERROR: Unable to read event prefix, corrupt MIDI ring buffer" << endl;
|
2008-06-02 21:41:35 +00:00
|
|
|
break;
|
2009-02-15 01:53:06 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
if (size > buf_capacity) {
|
|
|
|
|
buf_capacity = size;
|
|
|
|
|
buf = (uint8_t*)realloc(buf, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = src.read_contents(size, buf);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
cerr << "ERROR: Read time/size but not buffer, corrupt MIDI ring buffer" << endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-19 04:12:54 +00:00
|
|
|
assert(time >= position);
|
|
|
|
|
time -= position;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
ev.set(buf, size, time);
|
2008-09-22 16:28:02 +00:00
|
|
|
ev.set_event_type(EventTypeMap::instance().midi_event_type(ev.buffer()[0]));
|
2009-02-15 01:53:06 +00:00
|
|
|
if (!(ev.is_channel_event() || ev.is_smf_meta_event() || ev.is_sysex())) {
|
2009-02-15 01:24:26 +00:00
|
|
|
cerr << "SMFSource: WARNING: caller tried to write non SMF-Event of type "
|
|
|
|
|
<< std::hex << int(ev.buffer()[0]) << endl;
|
2008-06-02 21:41:35 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-19 04:12:54 +00:00
|
|
|
append_event_unlocked_frames(ev, position);
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-29 00:18:26 +00:00
|
|
|
if (_model) {
|
2009-02-14 17:59:58 +00:00
|
|
|
set_default_controls_interpolation();
|
2009-01-29 00:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-15 01:32:41 +00:00
|
|
|
Evoral::SMF::flush();
|
2008-06-02 21:41:35 +00:00
|
|
|
free(buf);
|
|
|
|
|
|
2009-02-20 00:30:42 +00:00
|
|
|
ViewDataRangeReady(position + _last_write_end, dur); /* EMIT SIGNAL */
|
2009-02-15 17:30:42 +00:00
|
|
|
|
|
|
|
|
return dur;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-02-15 17:30:42 +00:00
|
|
|
/** Append an event with a timestamp in beats (double) */
|
2008-06-02 21:41:35 +00:00
|
|
|
void
|
2009-02-17 02:11:49 +00:00
|
|
|
SMFSource::append_event_unlocked_beats (const Evoral::Event<double>& ev)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-02-01 21:04:12 +00:00
|
|
|
if (ev.size() == 0) {
|
2008-06-02 21:41:35 +00:00
|
|
|
return;
|
2009-02-01 21:04:12 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 17:30:42 +00:00
|
|
|
/*printf("SMFSource: %s - append_event_unlocked_beats time = %lf, size = %u, data = ",
|
2009-02-01 21:04:12 +00:00
|
|
|
name().c_str(), ev.time(), ev.size());
|
2009-02-15 17:30:42 +00:00
|
|
|
for (size_t i = 0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");*/
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
assert(ev.time() >= 0);
|
2009-02-15 17:30:42 +00:00
|
|
|
if (ev.time() < _last_ev_time_beats) {
|
|
|
|
|
cerr << "SMFSource: Warning: Skipping event with non-monotonic time" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-20 00:30:42 +00:00
|
|
|
_length_beats = max(_length_beats, ev.time());
|
|
|
|
|
|
2009-02-15 17:30:42 +00:00
|
|
|
const double delta_time_beats = ev.time() - _last_ev_time_beats;
|
|
|
|
|
const uint32_t delta_time_ticks = (uint32_t)lrint(delta_time_beats * (double)ppqn());
|
|
|
|
|
|
|
|
|
|
Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer());
|
|
|
|
|
_last_ev_time_beats = ev.time();
|
|
|
|
|
|
|
|
|
|
_write_data_count += ev.size();
|
|
|
|
|
|
|
|
|
|
if (_model) {
|
|
|
|
|
_model->append(ev);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Append an event with a timestamp in frames (nframes_t) */
|
|
|
|
|
void
|
2009-02-19 05:45:11 +00:00
|
|
|
SMFSource::append_event_unlocked_frames (const Evoral::Event<nframes_t>& ev, sframes_t position)
|
2009-02-15 17:30:42 +00:00
|
|
|
{
|
|
|
|
|
if (ev.size() == 0) {
|
2008-06-02 21:41:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2009-02-15 17:30:42 +00:00
|
|
|
|
|
|
|
|
/*printf("SMFSource: %s - append_event_unlocked_frames time = %u, size = %u, data = ",
|
|
|
|
|
name().c_str(), ev.time(), ev.size());
|
|
|
|
|
for (size_t i=0; i < ev.size(); ++i) printf("%X ", ev.buffer()[i]); printf("\n");*/
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 17:30:42 +00:00
|
|
|
assert(ev.time() >= 0);
|
|
|
|
|
if (ev.time() < _last_ev_time_frames) {
|
|
|
|
|
cerr << "SMFSource: Warning: Skipping event with non-monotonic time" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-19 04:12:54 +00:00
|
|
|
BeatsFramesConverter converter(_session, position);
|
|
|
|
|
|
2009-02-20 00:30:42 +00:00
|
|
|
_length_beats = max(_length_beats, converter.from(ev.time()));
|
|
|
|
|
|
2009-02-19 05:45:11 +00:00
|
|
|
const sframes_t delta_time_frames = ev.time() - _last_ev_time_frames;
|
2009-02-19 04:12:54 +00:00
|
|
|
const double delta_time_beats = converter.from(delta_time_frames);
|
2009-02-16 02:51:16 +00:00
|
|
|
const uint32_t delta_time_ticks = (uint32_t)(lrint(delta_time_beats * (double)ppqn()));
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-16 02:51:16 +00:00
|
|
|
Evoral::SMF::append_event_delta(delta_time_ticks, ev.size(), ev.buffer());
|
2009-02-15 17:30:42 +00:00
|
|
|
_last_ev_time_frames = ev.time();
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
_write_data_count += ev.size();
|
2009-02-15 17:30:42 +00:00
|
|
|
|
|
|
|
|
if (_model) {
|
2009-02-19 04:12:54 +00:00
|
|
|
const double ev_time_beats = converter.from(ev.time());
|
2009-02-15 17:30:42 +00:00
|
|
|
const Evoral::Event<double> beat_ev(
|
2009-02-16 02:51:16 +00:00
|
|
|
ev.event_type(), ev_time_beats, ev.size(), (uint8_t*)ev.buffer());
|
2009-02-15 17:30:42 +00:00
|
|
|
_model->append(beat_ev);
|
|
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XMLNode&
|
|
|
|
|
SMFSource::get_state ()
|
|
|
|
|
{
|
2009-02-17 02:11:49 +00:00
|
|
|
return MidiSource::get_state();
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
SMFSource::set_state (const XMLNode& node)
|
|
|
|
|
{
|
2009-02-17 02:11:49 +00:00
|
|
|
if (Source::set_state (node)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
if (MidiSource::set_state (node)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2009-02-17 02:11:49 +00:00
|
|
|
|
|
|
|
|
if (FileSource::set_state (node)) {
|
|
|
|
|
return -1;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-02-19 05:45:11 +00:00
|
|
|
SMFSource::mark_streaming_midi_write_started (NoteMode mode, sframes_t start_frame)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
|
|
|
|
MidiSource::mark_streaming_midi_write_started (mode, start_frame);
|
2009-02-15 01:32:41 +00:00
|
|
|
Evoral::SMF::begin_write ();
|
2009-02-15 17:30:42 +00:00
|
|
|
_last_ev_time_beats = 0.0;
|
|
|
|
|
_last_ev_time_frames = 0;
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
SMFSource::mark_streaming_write_completed ()
|
|
|
|
|
{
|
|
|
|
|
MidiSource::mark_streaming_write_completed();
|
|
|
|
|
|
|
|
|
|
if (!writable()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_model->set_edited(false);
|
2009-02-15 01:32:41 +00:00
|
|
|
Evoral::SMF::end_write ();
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-02-17 02:11:49 +00:00
|
|
|
SMFSource::safe_midi_file_extension (const Glib::ustring& file)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
|
|
|
|
return (file.rfind(".mid") != Glib::ustring::npos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2009-02-17 02:11:49 +00:00
|
|
|
SMFSource::load_model (bool lock, bool force_reload)
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
2009-01-28 04:55:31 +00:00
|
|
|
if (_writing) {
|
2008-06-02 21:41:35 +00:00
|
|
|
return;
|
2009-01-28 04:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lock) {
|
2008-06-02 21:41:35 +00:00
|
|
|
Glib::Mutex::Lock lm (_lock);
|
2009-01-28 04:55:31 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
2009-02-15 19:44:27 +00:00
|
|
|
if (_model && !force_reload) {
|
2008-06-02 21:41:35 +00:00
|
|
|
return;
|
2009-01-28 04:55:31 +00:00
|
|
|
}
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
if (! _model) {
|
|
|
|
|
_model = boost::shared_ptr<MidiModel>(new MidiModel(this));
|
|
|
|
|
cerr << _name << " loaded new model " << _model.get() << endl;
|
|
|
|
|
} else {
|
|
|
|
|
cerr << _name << " reloading model " << _model.get()
|
|
|
|
|
<< " (" << _model->n_notes() << " notes)" <<endl;
|
|
|
|
|
_model->clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_model->start_write();
|
2009-02-15 01:32:41 +00:00
|
|
|
Evoral::SMF::seek_to_start();
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
uint64_t time = 0; /* in SMF ticks */
|
2009-02-02 02:36:05 +00:00
|
|
|
Evoral::Event<double> ev;
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
size_t scratch_size = 0; // keep track of scratch and minimize reallocs
|
|
|
|
|
|
|
|
|
|
uint32_t delta_t = 0;
|
|
|
|
|
uint32_t size = 0;
|
|
|
|
|
uint8_t* buf = NULL;
|
|
|
|
|
int ret;
|
|
|
|
|
while ((ret = read_event(&delta_t, &size, &buf)) >= 0) {
|
|
|
|
|
time += delta_t;
|
2009-02-15 17:30:42 +00:00
|
|
|
ev.set(buf, size, time / (double)ppqn());
|
2008-06-02 21:41:35 +00:00
|
|
|
|
|
|
|
|
if (ret > 0) { // didn't skip (meta) event
|
2008-09-22 16:59:32 +00:00
|
|
|
ev.set_event_type(EventTypeMap::instance().midi_event_type(buf[0]));
|
2008-06-02 21:41:35 +00:00
|
|
|
_model->append(ev);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-28 04:55:31 +00:00
|
|
|
if (ev.size() > scratch_size) {
|
2008-06-02 21:41:35 +00:00
|
|
|
scratch_size = ev.size();
|
2009-01-28 04:55:31 +00:00
|
|
|
}
|
2009-02-15 03:51:49 +00:00
|
|
|
ev.size() = scratch_size; // ensure read_event only allocates if necessary
|
2009-02-20 00:30:42 +00:00
|
|
|
|
|
|
|
|
_length_beats = max(_length_beats, ev.time());
|
2009-01-28 04:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-14 17:59:58 +00:00
|
|
|
set_default_controls_interpolation();
|
2009-01-29 00:18:26 +00:00
|
|
|
|
|
|
|
|
_model->end_write(false);
|
|
|
|
|
_model->set_edited(false);
|
|
|
|
|
|
2009-02-15 22:10:32 +00:00
|
|
|
_model_iter = _model->begin();
|
|
|
|
|
|
2009-01-29 00:18:26 +00:00
|
|
|
free(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define LINEAR_INTERPOLATION_MODE_WORKS_PROPERLY 0
|
|
|
|
|
|
|
|
|
|
void
|
2009-02-17 02:11:49 +00:00
|
|
|
SMFSource::set_default_controls_interpolation ()
|
2009-01-29 00:18:26 +00:00
|
|
|
{
|
2009-01-28 04:55:31 +00:00
|
|
|
// set interpolation style to defaults, can be changed by the GUI later
|
|
|
|
|
Evoral::ControlSet::Controls controls = _model->controls();
|
|
|
|
|
for (Evoral::ControlSet::Controls::iterator c = controls.begin(); c != controls.end(); ++c) {
|
|
|
|
|
(*c).second->list()->set_interpolation(
|
2009-01-28 09:49:42 +00:00
|
|
|
// to be enabled when ControlList::rt_safe_earliest_event_linear_unlocked works properly
|
2009-01-29 00:18:26 +00:00
|
|
|
#if LINEAR_INTERPOLATION_MODE_WORKS_PROPERLY
|
2009-01-28 04:55:31 +00:00
|
|
|
EventTypeMap::instance().interpolation_of((*c).first));
|
2009-01-28 09:49:42 +00:00
|
|
|
#else
|
|
|
|
|
Evoral::ControlList::Discrete);
|
|
|
|
|
#endif
|
2008-06-02 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-02-17 02:11:49 +00:00
|
|
|
SMFSource::destroy_model ()
|
2008-06-02 21:41:35 +00:00
|
|
|
{
|
|
|
|
|
//cerr << _name << " destroying model " << _model.get() << endl;
|
|
|
|
|
_model.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-30 00:45:26 +00:00
|
|
|
void
|
2009-02-17 02:11:49 +00:00
|
|
|
SMFSource::flush_midi ()
|
2008-09-30 00:45:26 +00:00
|
|
|
{
|
2009-02-15 01:32:41 +00:00
|
|
|
Evoral::SMF::end_write();
|
2008-09-30 00:45:26 +00:00
|
|
|
}
|
|
|
|
|
|