RAUL  0.8.0
SMFReader.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_READER_HPP
19 #define RAUL_SMF_READER_HPP
20 
21 #include <stdexcept>
22 #include <string>
23 #include <inttypes.h>
24 #include "raul/TimeStamp.hpp"
25 
26 namespace Raul {
27 
28 
34 class SMFReader {
35 public:
36  class PrematureEOF : public std::exception {
37  const char* what() const throw() { return "Unexpected end of file"; }
38  };
39  class CorruptFile : public std::exception {
40  const char* what() const throw() { return "Corrupted file"; }
41  };
42  class UnsupportedTime : public std::exception {
43  const char* what() const throw() { return "Unsupported time stamp type (SMPTE)"; }
44  };
45 
46  explicit SMFReader(const std::string filename="");
47  ~SMFReader();
48 
49  bool open(const std::string& filename) throw (std::logic_error, UnsupportedTime);
50 
51  bool seek_to_track(unsigned track) throw (std::logic_error);
52 
53  uint16_t type() const { return _type; }
54  uint16_t ppqn() const { return _ppqn; }
55  size_t num_tracks() { return _num_tracks; }
56 
57  int read_event(size_t buf_len,
58  uint8_t* buf,
59  uint32_t* ev_size,
60  uint32_t* ev_delta_time)
61  throw (std::logic_error, PrematureEOF, CorruptFile);
62 
63  void close();
64 
65  static uint32_t read_var_len(FILE* fd) throw (PrematureEOF);
66 
67 protected:
69  static const uint32_t HEADER_SIZE = 22;
70 
71  std::string _filename;
72  FILE* _fd;
73  uint16_t _type;
74  uint16_t _ppqn;
75  uint16_t _num_tracks;
76  uint32_t _track;
77  uint32_t _track_size;
78 };
79 
80 
81 } // namespace Raul
82 
83 #endif // RAUL_SMF_READER_HPP
84 
bool seek_to_track(unsigned track)
Seek to the start of a given track, starting from 1.
Definition: SMFReader.cpp:151
int read_event(size_t buf_len, uint8_t *buf, uint32_t *ev_size, uint32_t *ev_delta_time)
Read an event from the current position in file.
Definition: SMFReader.cpp:209
Standard Midi File (Type 0) Reader.
Definition: SMFReader.hpp:34
Definition: Array.hpp:26
static const uint32_t HEADER_SIZE
size of SMF header, including MTrk chunk header
Definition: SMFReader.hpp:69