Add missing copyright headers.
[ardour.git] / gtk2_ardour / note_player.cc
1 /*
2     Copyright (C) 2011 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <sigc++/bind.h>
20 #include <glibmm/main.h>
21
22 #include "ardour/midi_track.h"
23 #include "ardour/session.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 void
36 NotePlayer::add (boost::shared_ptr<NoteType> note)
37 {
38         notes.push_back (note);
39 }
40
41 void
42 NotePlayer::play ()
43 {
44         Evoral::MusicalTime longest_duration_beats = 0;
45
46         /* note: if there is more than 1 note, we will silence them all at the same time
47          */
48
49         for (Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
50                 track->write_immediate_event ((*n)->on_event().size(), (*n)->on_event().buffer());
51                 if ((*n)->length() > longest_duration_beats) {
52                         longest_duration_beats = (*n)->length();
53                 }
54         }
55
56         uint32_t note_length_ms = 350;
57         /* beats_to_frames (longest_duration_beats)
58          * (1000 / (double)track->session().nominal_frame_rate()); */
59
60         Glib::signal_timeout().connect(sigc::bind (sigc::ptr_fun (&NotePlayer::_off), this),
61                                        note_length_ms, G_PRIORITY_DEFAULT);
62 }
63
64 bool
65 NotePlayer::_off (NotePlayer* np)
66 {
67         np->off ();
68         delete np;
69         return false;
70 }
71
72 void
73 NotePlayer::off ()
74 {
75         for (Notes::iterator n = notes.begin(); n != notes.end(); ++n) {
76                 track->write_immediate_event((*n)->off_event().size(), (*n)->off_event().buffer());
77         }
78 }