summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/note_fixer.h
blob: 69abc39df47663ebdb229a4cc44f48f50683f9ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
    Copyright (C) 2015 Paul Davis
    Author: David Robillard

    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_note_fixer_h__
#define __ardour_note_fixer_h__

#include <list>

#include <boost/utility.hpp>

#include "temporal/beats.h"

#include "evoral/Note.hpp"

#include "ardour/midi_model.h"
#include "ardour/types.h"

namespace Evoral { template<typename Time> class EventSink; }

namespace ARDOUR {

class BeatsSamplesConverter;
class MidiStateTracker;
class TempoMap;

/** A tracker and compensator for note edit operations.
 *
 * This monitors edit operations sent to a model that affect active notes
 * during a read, and maintains a queue of synthetic events that should be sent
 * at the start of the next read to maintain coherent MIDI state.
 */
class NoteFixer : public boost::noncopyable
{
public:
	typedef Evoral::Note<Temporal::Beats> Note;

	~NoteFixer();

	/** Clear all internal state. */
	void clear();

	/** Handle a region edit during read.
	 *
	 * This must be called before the command is applied to the model.  Events
	 * are enqueued to compensate for edits which should be later sent with
	 * emit() at the start of the next read.
	 *
	 * @param cmd Command to compensate for.
	 * @param origin Timeline position of edited source.
	 * @param pos Current read position (last read end).
	 */
	void prepare(TempoMap&                          tempo_map,
	             const MidiModel::NoteDiffCommand*  cmd,
	             samplepos_t                         origin,
	             samplepos_t                         pos,
	             std::set< boost::weak_ptr<Note> >& active_notes);

	/** Emit any pending edit compensation events.
	 *
	 * @param dst Destination for events.
	 * @param pos Timestamp to be used for every event, should be the start of
	 * the read block immediately following any calls to prepare().
	 * @param tracker Tracker to update with emitted events.
	 */
	void emit(Evoral::EventSink<samplepos_t>& dst,
	          samplepos_t                     pos,
	          MidiStateTracker&              tracker);

private:
	typedef Evoral::Event<samplepos_t> Event;
	typedef std::list<Event*>         Events;

	/** Copy a beats event to a samples event with the given time stamp. */
	Event* copy_event(samplepos_t time, const Evoral::Event<Temporal::Beats>& ev);

	/** Return true iff `note` is active at `pos`. */
	bool note_is_active(const BeatsSamplesConverter& converter,
	                    boost::shared_ptr<Note>     note,
	                    samplepos_t                  pos);

	Events _events;
};

} /* namespace ARDOUR */

#endif	/* __ardour_note_fixer_h__ */