optimize some performance bottlenecks; remove jack_nframes_t that crept back into...
[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 ARDOUR {
30
31 class MidiRingBuffer;
32
33 /** Standard Midi File (Type 0) Source */
34 class SMFSource : public MidiSource {
35   public:
36         enum Flag {
37                 Writable = 0x1,
38                 CanRename = 0x2,
39                 Broadcast = 0x4,
40                 Removable = 0x8,
41                 RemovableIfEmpty = 0x10,
42                 RemoveAtDestroy = 0x20,
43                 BuildPeaks = 0x40
44         };
45         
46         /** Constructor for existing external-to-session files */
47         SMFSource (Session& session, std::string path, Flag flags = Flag(0));
48
49         /* Constructor for existing in-session files */
50         SMFSource (Session& session, const XMLNode&);
51
52         virtual ~SMFSource ();
53
54         /* this block of methods do nothing for regular file sources, but are significant
55            for files used in destructive recording.
56         */
57         // FIXME and thus are useless for MIDI.. but make MidiDiskstream compile easier! :)
58
59         virtual nframes_t last_capture_start_frame() const { return 0; }
60         virtual void           mark_capture_start (nframes_t) {}
61         virtual void           mark_capture_end () {}
62         virtual void           clear_capture_marks() {}
63
64         int set_name (string newname, bool destructive);
65
66         string path() const { return _path; }
67
68         void set_allow_remove_if_empty (bool yn);
69         void mark_for_remove();
70
71         int update_header (nframes_t when, struct tm&, time_t);
72         int flush_header ();
73         int flush_footer ();
74
75         int move_to_trash (const string trash_dir_name);
76
77         static bool is_empty (string path);
78         void mark_streaming_write_completed ();
79
80         void   mark_take (string);
81         string take_id() const { return _take_id; }
82
83         static void set_search_path (string);
84         static void set_header_position_offset (nframes_t offset, bool negative);
85
86         XMLNode& get_state ();
87         int set_state (const XMLNode&);
88
89   private:
90
91         int init (string idstr, bool must_exist);
92
93         nframes_t read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cn, nframes_t stamp_offset) const;
94         nframes_t write_unlocked (MidiRingBuffer& dst, nframes_t cnt);
95
96         bool find (std::string path, bool must_exist, bool& is_new);
97         bool removable() const;
98         bool writable() const { return _flags & Writable; }
99         
100         int open();
101         
102         void     write_chunk_header(char id[4], uint32_t length);
103         void     write_chunk(char id[4], uint32_t length, void* data);
104         size_t   write_var_len(uint32_t val);
105         uint32_t read_var_len() const;
106         int      read_event(MidiEvent& ev) const;
107
108         uint16_t       _channel;
109         string         _path;
110         Flag           _flags;
111         string         _take_id;
112         bool           _allow_remove_if_empty;
113         uint64_t       _timeline_position;
114         FILE*          _fd;
115         nframes_t _last_ev_time; // last frame time written, relative to source start
116         uint32_t       _track_size;
117         uint32_t       _header_size; // size of SMF header, including MTrk chunk header
118
119         static string _search_path;
120 };
121
122 }; /* namespace ARDOUR */
123
124 #endif /* __ardour_smf_filesource_h__ */
125