Update comments & icon of rubberband example Lua script
[ardour.git] / gtk2_ardour / note_player.cc
1 /*
2  * Copyright (C) 2010-2012 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2011-2012 David Robillard <d@drobilla.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <sigc++/bind.h>
21 #include <glibmm/main.h>
22
23 #include "ardour/midi_track.h"
24
25 #include "note_player.h"
26
27 using namespace ARDOUR;
28 using namespace std;
29
30 NotePlayer::NotePlayer (boost::shared_ptr<MidiTrack> mt)
31         : track (mt)
32 {
33 }
34
35 NotePlayer::~NotePlayer ()
36 {
37         clear ();
38 }
39
40 void
41 NotePlayer::add (boost::shared_ptr<NoteType> note)
42 {
43         notes.push_back (note);
44 }
45
46 void
47 NotePlayer::clear ()
48 {
49         off ();
50         notes.clear ();
51 }
52
53 void
54 NotePlayer::on ()
55 {
56         for (Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
57                 track->write_immediate_event ((*n)->on_event().size(), (*n)->on_event().buffer());
58         }
59 }
60
61 void
62 NotePlayer::play ()
63 {
64         on ();
65
66         /* note: if there is more than 1 note, we will silence them all at the same time
67          */
68
69         const uint32_t note_length_ms = 100;
70
71         Glib::signal_timeout().connect (sigc::bind (sigc::ptr_fun (&NotePlayer::_off), this),
72                                         note_length_ms, G_PRIORITY_DEFAULT);
73 }
74
75 bool
76 NotePlayer::_off (NotePlayer* np)
77 {
78         np->off ();
79         delete np;
80         return false;
81 }
82
83 void
84 NotePlayer::off ()
85 {
86         for (Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
87                 track->write_immediate_event ((*n)->off_event().size(), (*n)->off_event().buffer());
88         }
89 }