(FULL commit) start of a nominal debug tracing system, with 64 bits available for flags; track notes by region in MidiPlaylist, and resolve them if they are left hanging at region boundaries. note: MIDI playback is still not working 100% though its better now.

git-svn-id: svn://localhost/ardour2/branches/3.0@5903 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Paul Davis 2009-10-24 13:26:56 +00:00
parent dbc75e0749
commit f8fe64a91e
12 changed files with 325 additions and 46 deletions

View File

@ -0,0 +1,52 @@
/*
Copyright (C) 2009 Paul Davis
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.
*/
#ifndef __ardour_debug_h__
#define __ardour_debug_h__
#include <sstream>
namespace ARDOUR {
extern uint64_t debug_bits;
void debug_print (std::string str);
void set_debug_bits (uint64_t bits);
namespace DEBUG {
/* this namespace is so that we can write DEBUG::bit_name */
enum DebugBits {
MidiSourceIO = 0x1
};
}
}
#ifndef NDEBUG
#define DEBUG_TRACE(bits,str) if ((bits) & ARDOUR::debug_bits) { ARDOUR::debug_print (str); }
#define DEBUG_STR_SET(id,s) std::stringstream __debug_str ## id; __debug_str ## id << s;
#define DEBUG_STR(id) __debug_str ## id
#else
#define DEBUG_TRACE(bits,fmt,...) /*empty*/
#define DEBUG_STR_SET(a,b) /* empty */
#define DEBUG_STR(a) /* empty */
#endif
#endif /* __ardour_debug_h__ */

View File

@ -56,7 +56,7 @@ class MidiRegion : public Region
virtual nframes_t read (Sample*, sframes_t /*pos*/, nframes_t /*cnt*/, int /*channel*/) const { return 0; }
virtual sframes_t readable_length() const { return length(); }
nframes_t read_at (MidiRingBuffer<nframes_t>& dst,
nframes_t read_at (Evoral::EventSink<nframes_t>& dst,
sframes_t position,
nframes_t dur,
uint32_t chan_n = 0,
@ -105,7 +105,7 @@ class MidiRegion : public Region
MidiRegion (const SourceList &, const XMLNode&);
private:
nframes_t _read_at (const SourceList&, MidiRingBuffer<nframes_t>& dst,
nframes_t _read_at (const SourceList&, Evoral::EventSink<nframes_t>& dst,
sframes_t position,
nframes_t dur,
uint32_t chan_n = 0,

View File

@ -58,14 +58,14 @@ class MidiSource : virtual public Source
* \param negative_stamp_offset Offset to subtract from event times written to dst
* \param tracker an optional pointer to MidiStateTracker object, for note on/off tracking
*/
virtual nframes_t midi_read (MidiRingBuffer<nframes_t>& dst,
virtual nframes_t midi_read (Evoral::EventSink<nframes_t>& dst,
sframes_t source_start,
sframes_t start, nframes_t cnt,
sframes_t stamp_offset, sframes_t negative_stamp_offset, MidiStateTracker*) const;
virtual nframes_t midi_write (MidiRingBuffer<nframes_t>& src,
sframes_t source_start,
nframes_t cnt);
sframes_t source_start,
nframes_t cnt);
virtual void append_event_unlocked_beats(const Evoral::Event<Evoral::MusicalTime>& ev) = 0;
@ -114,7 +114,7 @@ class MidiSource : virtual public Source
protected:
virtual void flush_midi() = 0;
virtual nframes_t read_unlocked (MidiRingBuffer<nframes_t>& dst,
virtual nframes_t read_unlocked (Evoral::EventSink<nframes_t>& dst,
sframes_t position,
sframes_t start, nframes_t cnt,
sframes_t stamp_offset, sframes_t negative_stamp_offset,

View File

@ -23,9 +23,12 @@
#include <bitset>
#include "ardour/midi_buffer.h"
namespace Evoral {
template <typename T> class EventSink;
}
namespace ARDOUR {
template <typename T> class MidiRingBuffer;
/** Tracks played notes, so they can be resolved in potential stuck note
* situations (e.g. looping, transport stop, etc).
@ -39,14 +42,17 @@ public:
void add (uint8_t note, uint8_t chn);
void remove (uint8_t note, uint8_t chn);
void resolve_notes (MidiBuffer& buffer, nframes64_t time);
void resolve_notes (MidiRingBuffer<nframes_t>& buffer, nframes64_t time);
void resolve_notes (Evoral::EventSink<nframes_t>& buffer, nframes64_t time);
void dump (std::ostream&);
void reset ();
bool empty() const { return _on == 0; }
uint16_t on() const { return _on; }
private:
void track_note_onoffs(const Evoral::MIDIEvent<MidiBuffer::TimeType>& event);
uint8_t _active_notes[128*16];
uint8_t _active_notes[128*16];
uint16_t _on;
};

View File

@ -70,7 +70,7 @@ public:
static bool safe_midi_file_extension (const Glib::ustring& path);
private:
nframes_t read_unlocked (MidiRingBuffer<nframes_t>& dst,
nframes_t read_unlocked (Evoral::EventSink<nframes_t>& dst,
sframes_t position,
sframes_t start,
nframes_t cnt,

View File

@ -59,6 +59,7 @@
#include "midi++/mmc.h"
#include "ardour/ardour.h"
#include "ardour/debug.h"
#include "ardour/analyser.h"
#include "ardour/audio_library.h"
#include "ardour/rc_configuration.h"
@ -89,6 +90,8 @@ using namespace ARDOUR;
using namespace std;
using namespace PBD;
uint64_t ARDOUR::debug_bits = 0x0;
MIDI::Port *ARDOUR::default_mmc_port = 0;
MIDI::Port *ARDOUR::default_mtc_port = 0;
MIDI::Port *ARDOUR::default_midi_port = 0;
@ -108,6 +111,18 @@ mix_buffers_no_gain_t ARDOUR::mix_buffers_no_gain = 0;
sigc::signal<void,std::string> ARDOUR::BootMessage;
void
ARDOUR::debug_print (std::string str)
{
cerr << str;
}
void
ARDOUR::set_debug_bits (uint64_t bits)
{
debug_bits = bits;
}
int
ARDOUR::setup_midi ()
{

View File

@ -20,11 +20,13 @@
#include <cassert>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <sigc++/bind.h>
#include "evoral/EventList.hpp"
#include "ardour/types.h"
#include "ardour/configuration.h"
#include "ardour/midi_playlist.h"
@ -122,6 +124,13 @@ struct RegionSortByLayer {
}
};
template<typename Time>
struct EventsSortByTime {
bool operator() (Evoral::Event<Time>* a, Evoral::Event<Time>* b) {
return a->time() < b->time();
}
};
/** Returns the number of frames in time duration read (eg could be large when 0 events are read) */
nframes_t
MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t dur, unsigned chan_n)
@ -131,6 +140,7 @@ MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t d
*/
Glib::RecMutex::Lock rm (region_lock);
cerr << "++++++" << start << " .. " << start + dur << " channel " << chan_n << " +++++++++++++++++++++++++++++++++++++++++++++++\n";
nframes_t end = start + dur - 1;
@ -138,47 +148,167 @@ MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t d
// relevent regions overlapping start <--> end
vector< boost::shared_ptr<Region> > regs;
typedef pair<MidiStateTracker*,nframes64_t> TrackerInfo;
vector<TrackerInfo> tracker_info;
uint32_t note_cnt = 0;
for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
if ((*i)->coverage (start, end) != OverlapNone) {
regs.push_back(*i);
} else {
/* region does not cover the current read boundaries, so make
sure that we silence any notes that it had turned on
*/
NoteTrackers::iterator t = _note_trackers.find ((*i).get());
if (t != _note_trackers.end()) {
t->second->resolve_notes (dst, (*i)->last_frame());
delete t->second;
/* add it the set of trackers we will do note resolution
on, and remove it from the list we are keeping
around, because we don't need it anymore.
*/
tracker_info.push_back (TrackerInfo (t->second, (*i)->last_frame()));
cerr << "time to resolve & remove tracker for " << (*i)->name() << endl;
note_cnt += (t->second->on());
_note_trackers.erase (t);
}
}
}
RegionSortByLayer layer_cmp;
sort(regs.begin(), regs.end(), layer_cmp);
if (note_cnt == 0 && !tracker_info.empty()) {
/* trackers to dispose of, but they have no notes in them */
cerr << "Clearing " << tracker_info.size() << " empty trackers\n";
for (vector<TrackerInfo>::iterator t = tracker_info.begin(); t != tracker_info.end(); ++t) {
delete (*t).first;
}
tracker_info.clear ();
}
if (regs.size() == 1 && tracker_info.empty()) {
/* just a single region - read directly into dst */
cerr << "Single region (" << regs.front()->name() << ") read, no out-of-bound region tracking info\n";
boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(regs.front());
for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
if (mr) {
NoteTrackers::iterator t = _note_trackers.find ((*i).get());
NoteTrackers::iterator t = _note_trackers.find (mr.get());
MidiStateTracker* tracker;
bool new_tracker = false;
if (t == _note_trackers.end()) {
pair<Region*,MidiStateTracker*> newpair;
newpair.first = (*i).get();
tracker = newpair.second = new MidiStateTracker;
_note_trackers.insert (newpair);
tracker = new MidiStateTracker;
new_tracker = true;
cerr << "\tBEFORE: new tracker\n";
} else {
tracker = t->second;
cerr << "\tBEFORE: tracker says there are " << tracker->on() << " on notes\n";
}
mr->read_at (dst, start, dur, chan_n, _note_mode, tracker);
cerr << "\tAFTER: tracker says there are " << tracker->on() << " on notes\n";
if (new_tracker) {
pair<Region*,MidiStateTracker*> newpair;
newpair.first = mr.get();
newpair.second = tracker;
_note_trackers.insert (newpair);
cerr << "\tadded tracker to trackers\n";
}
mr->read_at (dst, start, dur, chan_n, _note_mode, tracker);
_read_data_count += mr->read_data_count();
}
} else {
/* multiple regions and/or note resolution: sort by layer, read into a temporary non-monotonically
sorted EventSink, sort and then insert into dst.
*/
cerr << regs.size() << " regions to read, plus " << tracker_info.size() << " trackers\n";
Evoral::EventList<nframes_t> evlist;
for (vector<TrackerInfo>::iterator t = tracker_info.begin(); t != tracker_info.end(); ++t) {
cerr << "Resolve " << (*t).first->on() << " notes\n";
(*t).first->resolve_notes (evlist, (*t).second);
delete (*t).first;
}
cerr << "After resolution we now have " << evlist.size() << " events\n";
for (Evoral::EventList<nframes_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
cerr << '\t' << **x << endl;
}
RegionSortByLayer layer_cmp;
sort(regs.begin(), regs.end(), layer_cmp);
cerr << "for " << start << " .. " << start+dur-1 << " We have " << regs.size() << " regions to consider\n";
for (vector<boost::shared_ptr<Region> >::iterator i = regs.begin(); i != regs.end(); ++i) {
boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(*i);
if (!mr) {
continue;
}
NoteTrackers::iterator t = _note_trackers.find (mr.get());
MidiStateTracker* tracker;
bool new_tracker = false;
cerr << "Before " << mr->name() << " (" << mr->position() << " .. " << mr->last_frame() << ") we now have " << evlist.size() << " events\n";
if (t == _note_trackers.end()) {
tracker = new MidiStateTracker;
new_tracker = true;
cerr << "\tBEFORE: tracker says there are " << tracker->on() << " on notes\n";
} else {
tracker = t->second;
cerr << "\tBEFORE: tracker says there are " << tracker->on() << " on notes\n";
}
mr->read_at (evlist, start, dur, chan_n, _note_mode, tracker);
_read_data_count += mr->read_data_count();
cerr << "After " << mr->name() << " (" << mr->position() << " .. " << mr->last_frame() << ") we now have " << evlist.size() << " events\n";
for (Evoral::EventList<nframes_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
cerr << '\t' << **x << endl;
}
cerr << "\tAFTER: tracker says there are " << tracker->on() << " on notes\n";
if (new_tracker) {
pair<Region*,MidiStateTracker*> newpair;
newpair.first = mr.get();
newpair.second = tracker;
_note_trackers.insert (newpair);
cerr << "\tadded tracker to trackers\n";
}
}
cerr << "Final we now have " << evlist.size() << " events\n";
for (Evoral::EventList<nframes_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
cerr << '\t' << **x << endl;
}
if (!evlist.empty()) {
/* sort the event list */
EventsSortByTime<nframes_t> time_cmp;
evlist.sort (time_cmp);
cerr << "Post sort: we now have " << evlist.size() << " events\n";
for (Evoral::EventList<nframes_t>::iterator x = evlist.begin(); x != evlist.end(); ++x) {
cerr << '\t' << **x << endl;
}
/* write into dst */
for (Evoral::EventList<nframes_t>::iterator e = evlist.begin(); e != evlist.end(); ++e) {
Evoral::Event<nframes_t>* ev (*e);
dst.write (ev->time(), ev->event_type(), ev->size(), ev->buffer());
delete ev;
}
}
}
cerr << "-------------------------------------------------------------\n";
return dur;
}

View File

@ -130,7 +130,7 @@ MidiRegion::set_position_internal (nframes_t pos, bool allow_bbt_recompute)
}
nframes_t
MidiRegion::read_at (MidiRingBuffer<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
MidiRegion::read_at (Evoral::EventSink<nframes_t>& out, sframes_t position, nframes_t dur, uint32_t chan_n, NoteMode mode, MidiStateTracker* tracker) const
{
return _read_at (_sources, out, position, dur, chan_n, mode, tracker);
}
@ -142,9 +142,8 @@ MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, sframes_t position,
}
nframes_t
MidiRegion::_read_at (const SourceList& /*srcs*/,
MidiRingBuffer<nframes_t>& dst, sframes_t position, nframes_t dur, uint32_t chan_n,
NoteMode mode, MidiStateTracker* tracker) const
MidiRegion::_read_at (const SourceList& /*srcs*/, Evoral::EventSink<nframes_t>& dst, sframes_t position, nframes_t dur, uint32_t chan_n,
NoteMode mode, MidiStateTracker* tracker) const
{
nframes_t internal_offset = 0;
nframes_t src_offset = 0;

View File

@ -33,6 +33,7 @@
#include "pbd/basename.h"
#include "ardour/audioengine.h"
#include "ardour/debug.h"
#include "ardour/midi_model.h"
#include "ardour/midi_ring_buffer.h"
#include "ardour/midi_state_tracker.h"
@ -129,7 +130,7 @@ MidiSource::invalidate ()
}
nframes_t
MidiSource::midi_read (MidiRingBuffer<nframes_t>& dst, sframes_t source_start,
MidiSource::midi_read (Evoral::EventSink<nframes_t>& dst, sframes_t source_start,
sframes_t start, nframes_t cnt,
sframes_t stamp_offset, sframes_t negative_stamp_offset,
MidiStateTracker* tracker) const
@ -139,18 +140,19 @@ MidiSource::midi_read (MidiRingBuffer<nframes_t>& dst, sframes_t source_start,
BeatsFramesConverter converter(_session, source_start);
if (_model) {
#define BEATS_TO_FRAMES(t) (converter.to(t) + stamp_offset - negative_stamp_offset)
Evoral::Sequence<double>::const_iterator& i = _model_iter;
// If the cached iterator is invalid, search for the first event past start
if (_last_read_end == 0 || start != _last_read_end || !_model_iter_valid) {
DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("****!!!!**** search for relevant iterator for %1 / %2\n", source_start, start));
for (i = _model->begin(); i != _model->end(); ++i) {
if (converter.to(i->time()) >= start) {
break;
}
}
_model_iter_valid = true;
} else {
DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("============ use cached iterator for %1 / %2\n", source_start, start));
}
_last_read_end = start + cnt;
@ -164,8 +166,10 @@ MidiSource::midi_read (MidiRingBuffer<nframes_t>& dst, sframes_t source_start,
if (tracker) {
Evoral::MIDIEvent<Evoral::MusicalTime>& ev (*(Evoral::MIDIEvent<Evoral::MusicalTime>*) (&(*i)));
if (ev.is_note_on()) {
DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("\t%1 Added note on %2 @ %3\n", _name, ev.note(), time_frames));
tracker->add (ev.note(), ev.channel());
} else if (ev.is_note_off()) {
DEBUG_TRACE (DEBUG::MidiSourceIO, string_compose ("\t%1 Added note OFF %2 @ %3\n", _name, ev.note(), time_frames));
tracker->remove (ev.note(), ev.channel());
}
}

View File

@ -35,17 +35,16 @@ void
MidiStateTracker::reset ()
{
memset (_active_notes, 0, sizeof (_active_notes));
_on = 0;
}
void
MidiStateTracker::track_note_onoffs (const Evoral::MIDIEvent<MidiBuffer::TimeType>& event)
{
if (event.is_note_on()) {
_active_notes [event.note() + 128 * event.channel()]++;
add (event.note(), event.channel());
} else if (event.is_note_off()){
if (_active_notes[event.note() + 128 * event.channel()]) {
_active_notes [event.note() + 128 * event.channel()]--;
}
remove (event.note(), event.channel());
}
}
@ -53,13 +52,23 @@ void
MidiStateTracker::add (uint8_t note, uint8_t chn)
{
++_active_notes[note + 128 * chn];
++_on;
}
void
MidiStateTracker::remove (uint8_t note, uint8_t chn)
{
if (_active_notes[note + 128 * chn]) {
--_active_notes[note + 128 * chn];
switch (_active_notes[note + 128 * chn]) {
case 0:
break;
case 1:
--_on;
_active_notes [note + 128 * chn] = 0;
break;
default:
--_active_notes [note + 128 * chn];
break;
}
}
@ -82,35 +91,45 @@ MidiStateTracker::track (const MidiBuffer::iterator &from, const MidiBuffer::ite
void
MidiStateTracker::resolve_notes (MidiBuffer &dst, nframes64_t time)
{
if (!_on) {
return;
}
for (int channel = 0; channel < 16; ++channel) {
for (int note = 0; note < 128; ++note) {
while (_active_notes[channel * 128 + note]) {
while (_active_notes[note + 128 * channel]) {
uint8_t buffer[3] = { MIDI_CMD_NOTE_OFF | channel, note, 0 };
Evoral::MIDIEvent<MidiBuffer::TimeType> noteoff
(time, MIDI_CMD_NOTE_OFF, 3, buffer, false);
dst.push_back (noteoff);
_active_notes[channel * 128 + note]--;
_active_notes[note + 128 * channel]--;
}
}
}
_on = 0;
}
void
MidiStateTracker::resolve_notes (MidiRingBuffer<nframes_t> &dst, nframes64_t time)
MidiStateTracker::resolve_notes (Evoral::EventSink<nframes_t> &dst, nframes64_t time)
{
uint8_t buf[3];
if (!_on) {
return;
}
for (int channel = 0; channel < 16; ++channel) {
for (int note = 0; note < 128; ++note) {
while (_active_notes[channel * 128 + note]) {
while (_active_notes[note + 128 * channel]) {
buf[0] = MIDI_CMD_NOTE_OFF|channel;
buf[1] = note;
buf[2] = 0;
dst.write (time, EventTypeMap::instance().midi_event_type (buf[0]), 3, buf);
_active_notes[channel * 128 + note]--;
_active_notes[note + 128 * channel]--;
}
}
}
_on = 0;
}
void

View File

@ -102,7 +102,7 @@ SMFSource::~SMFSource ()
/** All stamps in audio frames */
nframes_t
SMFSource::read_unlocked (MidiRingBuffer<nframes_t>& destination, sframes_t source_start,
SMFSource::read_unlocked (Evoral::EventSink<nframes_t>& destination, sframes_t source_start,
sframes_t start, nframes_t duration,
sframes_t stamp_offset, sframes_t negative_stamp_offset,
MidiStateTracker* tracker) const

View File

@ -0,0 +1,54 @@
/* This file is part of Evoral.
* Copyright (C) 2008 Dave Robillard <http://drobilla.net>
*
* Evoral 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.
*
* Evoral 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 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.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef EVORAL_EVENT_LIST_HPP
#define EVORAL_EVENT_LIST_HPP
#include <list>
#include "evoral/EventSink.hpp"
#include "evoral/types.hpp"
#include "evoral/Event.hpp"
namespace Evoral {
/** A list of events (generic time-stamped binary "blobs").
*
* Used when we need an unsorted list of Events that is also an EventSink. Absolutely nothing more.
*/
template<typename Time>
class EventList : public std::list<Evoral::Event<Time> *>, public Evoral::EventSink<Time> {
public:
EventList() {}
uint32_t write(Time time, EventType type, uint32_t size, const uint8_t* buf);
};
template<typename Time>
inline uint32_t
EventList<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
{
Evoral::Event<Time>* e = new Evoral::Event<Time> (type, time, size, const_cast<uint8_t*> (buf), true); // event makes copy of buffer
push_back (e);
return size;
}
} // namespace Evoral
#endif // EVORAL_EVENT_LIST_HPP