back port 6576105 from cairocanvas as fix for data loss/file deletion
[ardour.git] / libs / ardour / ardour / smf_source.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: David Robillard
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
21 #ifndef __ardour_smf_source_h__
22 #define __ardour_smf_source_h__
23
24 #include <cstdio>
25 #include <time.h>
26 #include "evoral/SMF.hpp"
27 #include "ardour/midi_source.h"
28 #include "ardour/file_source.h"
29
30 namespace Evoral { template<typename T> class Event; }
31
32 namespace ARDOUR {
33
34 template<typename T> class MidiRingBuffer;
35
36 /** Standard Midi File (Type 0) Source */
37 class SMFSource : public MidiSource, public FileSource, public Evoral::SMF {
38 public:
39         /** Constructor for new internal-to-session files */
40         SMFSource (Session& session, const std::string& path, Source::Flag flags);
41
42         /** Constructor for existing external-to-session files */
43         SMFSource (Session& session, const std::string& path);
44
45         /** Constructor for existing in-session files */
46         SMFSource (Session& session, const XMLNode&, bool must_exist = false);
47
48         virtual ~SMFSource ();
49
50         /** Rename the file on disk referenced by this source to \param newname
51          *
52          * This method exists only for MIDI file sources, not for audio, which 
53          * can never be renamed. It exists for MIDI so that we can get
54          * consistent and sane region/source numbering when regions are added
55          * manually (which never happens with audio).
56          */
57         int rename (const std::string& name);
58
59         bool safe_file_extension (const std::string& path) const {
60                 return safe_midi_file_extension(path);
61         }
62
63         void append_event_unlocked_beats (const Evoral::Event<Evoral::MusicalTime>& ev);
64         void append_event_unlocked_frames (const Evoral::Event<framepos_t>& ev, framepos_t source_start);
65
66         void mark_streaming_midi_write_started (NoteMode mode);
67         void mark_streaming_write_completed ();
68         void mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::MusicalTime>::StuckNoteOption, Evoral::MusicalTime when = 0);
69
70         XMLNode& get_state ();
71         int set_state (const XMLNode&, int version);
72
73         void load_model (bool lock=true, bool force_reload=false);
74         void destroy_model ();
75
76         void flush_midi ();
77         void ensure_disk_file ();
78
79         static bool safe_midi_file_extension (const std::string& path);
80         static bool valid_midi_file (const std::string& path);
81
82         void prevent_deletion ();
83
84   protected:
85         void set_path (const std::string& newpath);
86
87   private:
88         int open_for_write ();
89
90         framecnt_t read_unlocked (Evoral::EventSink<framepos_t>& dst,
91                                   framepos_t                     position,
92                                   framepos_t                     start,
93                                   framecnt_t                     cnt,
94                                   MidiStateTracker*              tracker) const;
95
96         framecnt_t write_unlocked (MidiRingBuffer<framepos_t>& src,
97                                    framepos_t                  position,
98                                    framecnt_t                  cnt);
99
100         double    _last_ev_time_beats;
101         framepos_t _last_ev_time_frames;
102         /** end time (start + duration) of last call to read_unlocked */
103         mutable framepos_t _smf_last_read_end;
104         /** time (in SMF ticks, 1 tick per _ppqn) of the last event read by read_unlocked */
105         mutable framepos_t _smf_last_read_time;
106 };
107
108 }; /* namespace ARDOUR */
109
110 #endif /* __ardour_smf_source_h__ */
111