82f41ebf2d74a906d9de7f9d22f77a88a7c13cd6
[ardour.git] / libs / ardour / ardour / smf_source.h
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
21 #ifndef __ardour_smf_filesource_h__ 
22 #define __ardour_smf_filesource_h__
23
24 #include <cstdio>
25 #include <time.h>
26
27 #include <ardour/midi_source.h>
28 #include <evoral/SMF.hpp>
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 Evoral::SMF {
38   public:
39         enum Flag {
40                 Writable = 0x1,
41                 CanRename = 0x2,
42                 Broadcast = 0x4,
43                 Removable = 0x8,
44                 RemovableIfEmpty = 0x10,
45                 RemoveAtDestroy = 0x20,
46                 BuildPeaks = 0x40
47         };
48         
49         /** Constructor for existing external-to-session files */
50         SMFSource (Session& session, std::string path, Flag flags = Flag(0));
51
52         /* Constructor for existing in-session files */
53         SMFSource (Session& session, const XMLNode&);
54
55         virtual ~SMFSource ();
56
57         bool set_name (const std::string& newname) { return (set_source_name(newname, false) == 0); }
58         int set_source_name (string newname, bool destructive);
59         
60         static bool safe_file_extension (const Glib::ustring& path);
61         
62         Glib::ustring path() const { return _path; }
63
64         void set_allow_remove_if_empty (bool yn);
65         void mark_for_remove();
66
67         void append_event_unlocked(EventTimeUnit unit, const Evoral::Event<double>& ev);
68
69         int move_to_trash (const string trash_dir_name);
70
71         void mark_streaming_midi_write_started (NoteMode mode, nframes_t start_time);
72         void mark_streaming_write_completed ();
73
74         void   mark_take (string);
75         string take_id() const { return _take_id; }
76
77         static void set_search_path (string);
78         static void set_header_position_offset (nframes_t offset, bool negative);
79
80         XMLNode& get_state ();
81         int set_state (const XMLNode&);
82
83         void load_model(bool lock=true, bool force_reload=false);
84         void destroy_model();
85
86         double last_event_time() const { return _last_ev_time; }
87
88         void flush_midi();
89
90   private:
91         int init (string idstr, bool must_exist);
92
93         nframes_t read_unlocked (
94                         MidiRingBuffer<nframes_t>& dst,
95                         nframes_t start,
96                         nframes_t cn,
97                         nframes_t stamp_offset,
98                         nframes_t negative_stamp_offset) const;
99
100         nframes_t write_unlocked (
101                         MidiRingBuffer<nframes_t>& src,
102                         nframes_t cnt);
103
104         bool find (std::string path, bool must_exist, bool& is_new);
105         bool removable() const;
106         bool writable() const { return _flags & Writable; }
107         
108         void set_default_controls_interpolation();
109
110         Glib::ustring  _path;
111         Flag           _flags;
112         string         _take_id;
113         bool           _allow_remove_if_empty;
114         double         _last_ev_time;
115
116         static string _search_path;
117 };
118
119 }; /* namespace ARDOUR */
120
121 #endif /* __ardour_smf_filesource_h__ */
122