Separate low level details of SMF reading/writing from concept of 'midi source in...
[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 class Event;
28 class EventRingBuffer;
29
30
31 /** Standard Midi File (Type 0)
32  */
33 class SMF {
34 public:
35         SMF();
36         virtual ~SMF();
37
38         void seek_to_start() const;
39         
40         uint16_t ppqn() const { return _ppqn; }
41         bool     is_empty() const { return _empty; }
42         bool     eof() const { return feof(_fd); }
43         
44         EventTime last_event_time() const { return _last_ev_time; }
45         
46         void begin_write(nframes_t start_time);
47         void append_event_unlocked(uint32_t delta_t, const Evoral::Event& ev);
48         void end_write();
49         
50         void flush();
51         int  flush_header();
52         int  flush_footer();
53
54 protected:
55         int  open(const std::string& path);
56         void close();
57         
58         /** Used by flush_footer() to find the position to write the footer */
59         void seek_to_footer_position();
60         
61         /** Write the track footer at the current seek position */
62         void write_footer();
63
64         void     write_chunk_header(const char id[4], uint32_t length);
65         void     write_chunk(const char id[4], uint32_t length, void* data);
66         size_t   write_var_len(uint32_t val);
67         uint32_t read_var_len() const;
68         int      read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const;
69
70 private:
71         static const uint16_t _ppqn = 19200;
72
73         FILE*          _fd;
74         EventTime      _last_ev_time; ///< last frame time written, relative to source start
75         uint32_t       _track_size;
76         uint32_t       _header_size; ///< size of SMF header, including MTrk chunk header
77         bool           _empty; ///< true iff file contains(non-empty) events
78 };
79
80 }; /* namespace Evoral */
81
82 #endif /* EVORAL_SMF_HPP */
83