RAUL  0.5.1
SMFWriter.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_SMF_WRITER_HPP
19 #define RAUL_SMF_WRITER_HPP
20 
21 #include <stdexcept>
22 #include <raul/MIDISink.hpp>
23 #include <raul/TimeStamp.hpp>
24 
25 namespace Raul {
26 
27 
30 class SMFWriter : public Raul::MIDISink {
31 public:
32  SMFWriter(TimeUnit unit);
33  ~SMFWriter();
34 
35  bool start(const std::string& filename,
36  TimeStamp start_time) throw (std::logic_error);
37 
38  TimeUnit unit() const { return _unit; }
39 
40  void write_event(TimeStamp time,
41  size_t ev_size,
42  const unsigned char* ev) throw (std::logic_error);
43 
44  void flush();
45 
46  void finish() throw (std::logic_error);
47 
48 protected:
49  static const uint32_t VAR_LEN_MAX = 0x0FFFFFFF;
50 
51  void write_header();
52  void write_footer();
53 
54  void write_chunk_header(const char id[4], uint32_t length);
55  void write_chunk(const char id[4], uint32_t length, void* data);
56  size_t write_var_len(uint32_t val);
57 
58  std::string _filename;
59  FILE* _fd;
60  TimeUnit _unit;
61  Raul::TimeStamp _start_time;
63  uint32_t _track_size;
64  uint32_t _header_size;
65 };
66 
67 
68 } // namespace Raul
69 
70 #endif // RAUL_SMF_WRITER_HPP
71 
Pure virtual base for anything you can write MIDI to.
Definition: MIDISink.hpp:30
uint32_t _header_size
size of SMF header, including MTrk chunk header
Definition: SMFWriter.hpp:64
A type of time stamp.
Definition: TimeStamp.hpp:32
Definition: Array.hpp:26
Standard Midi File (Type 0) Writer.
Definition: SMFWriter.hpp:30
A real-time time stamp (possible units: frame, absolute (s), or beat).
Definition: TimeStamp.hpp:78
Raul::TimeStamp _last_ev_time
Time last event was written relative to _start_time.
Definition: SMFWriter.hpp:62