Shrink exposed SMF API.
[ardour.git] / libs / evoral / evoral / SMF.hpp
1 /* This file is part of Evoral.
2  * Copyright(C) 2008 Dave Robillard <http://drobilla.net>
3  * Copyright(C) 2000-2008 Paul Davis
4  * 
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or(at your option) any later
8  * version.
9  * 
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  * 
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef EVORAL_SMF_HPP
20 #define EVORAL_SMF_HPP
21
22 #include <string>
23 #include "evoral/types.hpp"
24
25 namespace Evoral {
26         
27 template<typename T> class Event;
28 template<typename T> class EventRingBuffer;
29
30
31 /** Standard Midi File (Type 0)
32  */
33 template<typename T>
34 class SMF {
35 public:
36         SMF();
37         virtual ~SMF();
38
39         void seek_to_start() const;
40         
41         uint16_t ppqn()     const { return _ppqn; }
42         bool     is_empty() const { return _empty; }
43         bool     eof()      const { return feof(_fd); }
44         
45         T last_event_time() const { return _last_ev_time; }
46         
47         void begin_write(FrameTime start_time);
48         void append_event_unlocked(uint32_t delta_t, const Event<T>& ev);
49         void end_write();
50         
51         void flush();
52         int  flush_header();
53         int  flush_footer();
54
55 protected:
56         int  open(const std::string& path);
57         void close();
58         
59         int read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const;
60
61 private:
62         /** Used by flush_footer() to find the position to write the footer */
63         void seek_to_footer_position();
64         
65         /** Write the track footer at the current seek position */
66         void write_footer();
67
68         void     write_chunk_header(const char id[4], uint32_t length);
69         void     write_chunk(const char id[4], uint32_t length, void* data);
70         size_t   write_var_len(uint32_t val);
71         uint32_t read_var_len() const;
72
73         static const uint16_t _ppqn = 19200;
74
75         FILE*    _fd;
76         T        _last_ev_time; ///< last frame time written, relative to source start
77         uint32_t _track_size;
78         uint32_t _header_size; ///< size of SMF header, including MTrk chunk header
79         bool     _empty; ///< true iff file contains(non-empty) events
80 };
81
82 }; /* namespace Evoral */
83
84 #endif /* EVORAL_SMF_HPP */
85