Factor out sequencing related things into an independant new library: "evoral".
[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
29 namespace Evoral { class Event; }
30
31 namespace ARDOUR {
32
33 class MidiRingBuffer;
34
35 /** Standard Midi File (Type 0) Source */
36 class SMFSource : public MidiSource {
37   public:
38         enum Flag {
39                 Writable = 0x1,
40                 CanRename = 0x2,
41                 Broadcast = 0x4,
42                 Removable = 0x8,
43                 RemovableIfEmpty = 0x10,
44                 RemoveAtDestroy = 0x20,
45                 BuildPeaks = 0x40
46         };
47         
48         /** Constructor for existing external-to-session files */
49         SMFSource (Session& session, std::string path, Flag flags = Flag(0));
50
51         /* Constructor for existing in-session files */
52         SMFSource (Session& session, const XMLNode&);
53
54         virtual ~SMFSource ();
55
56         /* this block of methods do nothing for regular file sources, but are significant
57            for files used in destructive recording.
58         */
59         // FIXME and thus are useless for MIDI.. but make MidiDiskstream compile easier! :)
60
61         virtual nframes_t last_capture_start_frame() const { return 0; }
62         virtual void      mark_capture_start (nframes_t) {}
63         virtual void      mark_capture_end () {}
64         virtual void      clear_capture_marks() {}
65
66         bool set_name (const std::string& newname) { return (set_source_name(newname, false) == 0); }
67         int set_source_name (string newname, bool destructive);
68         
69         static bool safe_file_extension (const Glib::ustring& path);
70
71         Glib::ustring path() const { return _path; }
72
73         void set_allow_remove_if_empty (bool yn);
74         void mark_for_remove();
75
76         void append_event_unlocked(EventTimeUnit unit, const Evoral::Event& ev);
77
78         int flush_header ();
79         int flush_footer ();
80         
81         int move_to_trash (const string trash_dir_name);
82
83         bool is_empty () const;
84         void mark_streaming_midi_write_started (NoteMode mode, nframes_t start_time);
85         void mark_streaming_write_completed ();
86
87         void   mark_take (string);
88         string take_id() const { return _take_id; }
89
90         static void set_search_path (string);
91         static void set_header_position_offset (nframes_t offset, bool negative);
92
93         XMLNode& get_state ();
94         int set_state (const XMLNode&);
95
96         void seek_to(nframes_t time);
97         
98         void load_model(bool lock=true, bool force_reload=false);
99         void destroy_model();
100
101         uint16_t ppqn() const { return _ppqn; }
102
103   private:
104
105         int init (string idstr, bool must_exist);
106
107         nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cn, nframes_t stamp_offset, nframes_t negative_stamp_offset) const;
108         nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt);
109
110         bool find (std::string path, bool must_exist, bool& is_new);
111         bool removable() const;
112         bool writable() const { return _flags & Writable; }
113
114         int  open();
115         void close();
116         
117         /**
118          * This method is only used by flush_footer() to find the right seek position 
119          * for the footer (at the end after recording or -4 offset ro SEEK_END
120          * if a footer is already present)
121          */
122         void seek_to_footer_position();
123         
124         /**
125          * write the track footer at the current seek position
126          */
127         void write_footer();
128
129         void     write_chunk_header(const char id[4], uint32_t length);
130         void     write_chunk(const char id[4], uint32_t length, void* data);
131         size_t   write_var_len(uint32_t val);
132         uint32_t read_var_len() const;
133         int      read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const;
134
135         static const uint16_t _ppqn = 19200;
136
137         Glib::ustring  _path;
138         Flag           _flags;
139         string         _take_id;
140         bool           _allow_remove_if_empty;
141         FILE*          _fd;
142         double         _last_ev_time; ///< last frame time written, relative to source start
143         uint32_t       _track_size;
144         uint32_t       _header_size; ///< size of SMF header, including MTrk chunk header
145         bool           _empty; ///< true iff file contains (non-empty) events
146
147         static string _search_path;
148 };
149
150 }; /* namespace ARDOUR */
151
152 #endif /* __ardour_smf_filesource_h__ */
153