RAUL 0.7.0

SMFWriter.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
00003  *
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  *
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_SMF_WRITER_HPP
00019 #define RAUL_SMF_WRITER_HPP
00020 
00021 #include <stdexcept>
00022 #include "raul/MIDISink.hpp"
00023 #include "raul/TimeStamp.hpp"
00024 
00025 namespace Raul {
00026 
00027 
00030 class SMFWriter : public Raul::MIDISink {
00031 public:
00032     SMFWriter(TimeUnit unit);
00033     ~SMFWriter();
00034 
00035     bool start(const std::string& filename,
00036                TimeStamp          start_time) throw (std::logic_error);
00037 
00038     TimeUnit unit() const { return _unit; }
00039 
00040     void write_event(TimeStamp            time,
00041                      size_t               ev_size,
00042                      const unsigned char* ev) throw (std::logic_error);
00043 
00044     void flush();
00045 
00046     void finish() throw (std::logic_error);
00047 
00048 protected:
00049     static const uint32_t VAR_LEN_MAX = 0x0FFFFFFF;
00050 
00051     void write_header();
00052     void write_footer();
00053 
00054     void     write_chunk_header(const char id[4], uint32_t length);
00055     void     write_chunk(const char id[4], uint32_t length, void* data);
00056     size_t   write_var_len(uint32_t val);
00057 
00058     std::string     _filename;
00059     FILE*           _fd;
00060     TimeUnit        _unit;
00061     Raul::TimeStamp _start_time;
00062     Raul::TimeStamp _last_ev_time; 
00063     uint32_t        _track_size;
00064     uint32_t        _header_size; 
00065 };
00066 
00067 
00068 } // namespace Raul
00069 
00070 #endif // RAUL_SMF_WRITER_HPP
00071