RAUL  0.8.0
SMFWriter.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David 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 <string>
23 
24 #include "raul/MIDISink.hpp"
25 #include "raul/TimeStamp.hpp"
26 
27 namespace Raul {
28 
29 
33 class SMFWriter : public Raul::MIDISink {
34 public:
35  explicit SMFWriter(TimeUnit unit);
36  ~SMFWriter();
37 
38  bool start(const std::string& filename,
39  TimeStamp start_time) throw (std::logic_error);
40 
41  TimeUnit unit() const { return _unit; }
42 
43  void write_event(TimeStamp time,
44  size_t ev_size,
45  const unsigned char* ev) throw (std::logic_error);
46 
47  void flush();
48 
49  void finish() throw (std::logic_error);
50 
51 protected:
52  static const uint32_t VAR_LEN_MAX = 0x0FFFFFFF;
53 
54  void write_header();
55  void write_footer();
56 
57  void write_chunk_header(const char id[4], uint32_t length);
58  void write_chunk(const char id[4], uint32_t length, void* data);
59  size_t write_var_len(uint32_t val);
60 
61  std::string _filename;
62  FILE* _fd;
63  TimeUnit _unit;
64  Raul::TimeStamp _start_time;
66  uint32_t _track_size;
67  uint32_t _header_size;
68 };
69 
70 
71 } // namespace Raul
72 
73 #endif // RAUL_SMF_WRITER_HPP
74