Add log window to windows menu.
[ardour.git] / libs / ardour / midi_source.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3         Written by Dave Robillard, 2006
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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <float.h>
25 #include <cerrno>
26 #include <ctime>
27 #include <cmath>
28 #include <iomanip>
29 #include <algorithm>
30
31 #include "pbd/xml++.h"
32 #include "pbd/pthread_utils.h"
33 #include "pbd/basename.h"
34
35 #include "ardour/audioengine.h"
36 #include "ardour/midi_model.h"
37 #include "ardour/midi_ring_buffer.h"
38 #include "ardour/midi_state_tracker.h"
39 #include "ardour/midi_source.h"
40 #include "ardour/session.h"
41 #include "ardour/session_directory.h"
42 #include "ardour/source_factory.h"
43 #include "ardour/tempo.h"
44
45 #include "i18n.h"
46
47 using namespace std;
48 using namespace ARDOUR;
49 using namespace PBD;
50
51 sigc::signal<void,MidiSource *> MidiSource::MidiSourceCreated;
52
53 MidiSource::MidiSource (Session& s, string name, Source::Flag flags)
54         : Source(s, DataType::MIDI, name, flags)
55         , _read_data_count(0)
56         , _write_data_count(0)
57         , _writing(false)
58         , _model_iter_valid(false)
59         , _length_beats(0.0)
60         , _last_read_end(0)
61         , _last_write_end(0)
62 {
63 }
64
65 MidiSource::MidiSource (Session& s, const XMLNode& node)
66         : Source(s, node)
67         , _read_data_count(0)
68         , _write_data_count(0)
69         , _writing(false)
70         , _model_iter_valid(false)
71         , _length_beats(0.0)
72         , _last_read_end(0)
73         , _last_write_end(0)
74 {
75         _read_data_count = 0;
76         _write_data_count = 0;
77
78         if (set_state (node, Stateful::loading_state_version)) {
79                 throw failed_constructor();
80         }
81 }
82
83 MidiSource::~MidiSource ()
84 {
85 }
86
87 XMLNode&
88 MidiSource::get_state ()
89 {
90         XMLNode& node (Source::get_state());
91
92         if (_captured_for.length()) {
93                 node.add_property ("captured-for", _captured_for);
94         }
95
96         return node;
97 }
98
99 int
100 MidiSource::set_state (const XMLNode& node, int /*version*/)
101 {
102         const XMLProperty* prop;
103
104         if ((prop = node.property ("captured-for")) != 0) {
105                 _captured_for = prop->value();
106         }
107
108         return 0;
109 }
110
111 sframes_t
112 MidiSource::length (sframes_t pos) const
113 {
114         BeatsFramesConverter converter(_session, pos);
115         return converter.to(_length_beats);
116 }
117
118 void
119 MidiSource::update_length (sframes_t /*pos*/, sframes_t /*cnt*/)
120 {
121         // You're not the boss of me!
122 }
123
124 void
125 MidiSource::invalidate ()
126 {
127         _model_iter_valid = false;
128         _model_iter.invalidate();
129 }
130
131 nframes_t
132 MidiSource::midi_read (MidiRingBuffer<nframes_t>& dst, sframes_t source_start,
133                        sframes_t start, nframes_t cnt,
134                        sframes_t stamp_offset, sframes_t negative_stamp_offset,
135                        MidiStateTracker* tracker) const
136 {
137         Glib::Mutex::Lock lm (_lock);
138
139         BeatsFramesConverter converter(_session, source_start);
140
141         if (_model) {
142                 //cerr << "READ @ " << start << " * " << cnt << " (source @ " << source_start << " {" << endl;
143 #define BEATS_TO_FRAMES(t) (converter.to(t) + stamp_offset - negative_stamp_offset)
144
145                 Evoral::Sequence<double>::const_iterator& i = _model_iter;
146
147                 // If the cached iterator is invalid, search for the first event past start
148                 if (_last_read_end == 0 || start != _last_read_end || !_model_iter_valid) {
149                         for (i = _model->begin(); i != _model->end(); ++i) {
150                                 if (BEATS_TO_FRAMES(i->time()) >= start) {
151                                         break;
152                                 }
153                         }
154                         _model_iter_valid = true;
155                 }
156
157                 _last_read_end = start + cnt;
158
159                 // Read events up to source_start + start + cnt
160                 for (; i != _model->end(); ++i) {
161                         const sframes_t time_frames = BEATS_TO_FRAMES(i->time());
162                         //cerr << "Read? " << time_frames << " < " << source_start + start + cnt << endl;
163                         if (time_frames < source_start + start + cnt) {
164                                 //cerr << "Read @ " << time_frames << endl;
165                                 dst.write(time_frames, i->event_type(), i->size(), i->buffer());
166                                 if (tracker) {
167                                         Evoral::MIDIEvent<Evoral::MusicalTime>& ev (*(Evoral::MIDIEvent<Evoral::MusicalTime>*) (&(*i)));
168                                         if (ev.is_note_on()) {
169                                                 tracker->add (ev.note(), ev.channel());
170                                         } else if (ev.is_note_off()) {
171                                                 tracker->remove (ev.note(), ev.channel());
172                                         }
173                                 }
174                         } else {
175                                 //cerr << "End" << endl;
176                                 break;
177                         }
178                 }
179                 //cerr << "}" << endl;
180                 return cnt;
181         } else {
182                 return read_unlocked (dst, source_start, start, cnt, stamp_offset, negative_stamp_offset, tracker);
183         }
184 }
185
186 nframes_t
187 MidiSource::midi_write (MidiRingBuffer<nframes_t>& source, sframes_t source_start, nframes_t duration)
188 {
189         Glib::Mutex::Lock lm (_lock);
190         const nframes_t ret = write_unlocked (source, source_start, duration);
191         _last_write_end += duration;
192         return ret;
193 }
194
195 bool
196 MidiSource::file_changed (string path)
197 {
198         struct stat stat_file;
199
200         int e1 = stat (path.c_str(), &stat_file);
201
202         return !e1;
203 }
204
205 void
206 MidiSource::mark_streaming_midi_write_started (NoteMode mode, sframes_t start_frame)
207 {
208         set_timeline_position(start_frame);
209
210         if (_model) {
211                 _model->set_note_mode(mode);
212                 _model->start_write();
213         }
214
215         _last_write_end = start_frame;
216         _writing = true;
217 }
218
219 void
220 MidiSource::mark_streaming_write_started ()
221 {
222         sframes_t start_frame = _session.transport_frame();
223
224         if (_model) {
225                 _model->start_write();
226         }
227
228         _last_write_end = start_frame;
229         _writing = true;
230 }
231
232 void
233 MidiSource::mark_streaming_write_completed ()
234 {
235         if (_model) {
236                 _model->end_write(false);
237         }
238
239         _writing = false;
240 }
241
242 void
243 MidiSource::session_saved()
244 {
245         flush_midi();
246
247         if (_model && _model->edited()) {
248                 string newname;
249                 const string basename = PBD::basename_nosuffix(_name);
250                 string::size_type last_dash = basename.find_last_of("-");
251                 if (last_dash == string::npos || last_dash == basename.find_first_of("-")) {
252                         newname = basename + "-1";
253                 } else {
254                         stringstream ss(basename.substr(last_dash+1));
255                         unsigned write_count = 0;
256                         ss >> write_count;
257                         // cerr << "WRITE COUNT: " << write_count << endl;
258                         ++write_count; // start at 1
259                         ss.clear();
260                         ss << basename.substr(0, last_dash) << "-" << write_count;
261                         newname = ss.str();
262                 }
263
264                 string newpath = _session.session_directory().midi_path().to_string() +"/"+ newname + ".mid";
265
266                 boost::shared_ptr<MidiSource> newsrc = boost::dynamic_pointer_cast<MidiSource>(
267                                 SourceFactory::createWritable(DataType::MIDI, _session,
268                                                 newpath, true, false, _session.frame_rate()));
269
270                 newsrc->set_timeline_position(_timeline_position);
271                 _model->write_to(newsrc);
272
273                 // cyclic dependency here, ugly :(
274                 newsrc->set_model(_model);
275                 _model->set_midi_source(newsrc.get());
276
277                 newsrc->flush_midi();
278
279                 Switched.emit(newsrc);
280         }
281 }
282
283 void
284 MidiSource::set_note_mode(NoteMode mode)
285 {
286         if (_model) {
287                 _model->set_note_mode(mode);
288         }
289 }
290