new files from sakari, missed last time
[ardour.git] / libs / ardour / ardour / smf_reader.h
1 /*
2     Copyright (C) 2008 Paul Davis 
3     Written by Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_smf_reader_h__ 
21 #define __ardour_smf_reader_h__
22
23 #include <exception>
24 #include <stdexcept>
25 #include <string>
26 #include <inttypes.h>
27
28 namespace ARDOUR {
29
30
31 /** Standard MIDI File (Type 0) Reader
32  *
33  * Currently this only reads SMF files with tempo-based timing.
34  */
35 class SMFReader {
36 public:
37         class PrematureEOF : public std::exception {
38                 const char* what() const throw() { return "Unexpected end of file"; }
39         };
40         class CorruptFile : public std::exception {
41                 const char* what() const throw() { return "Corrupted file"; }
42         };
43         class UnsupportedTime : public std::exception {
44                 const char* what() const throw() { return "Unsupported time stamp type (SMPTE)"; }
45         };
46
47         SMFReader(const std::string filename="");
48         ~SMFReader();
49
50         bool open(const std::string& filename) throw (std::logic_error, UnsupportedTime);
51
52         bool seek_to_track(unsigned track) throw (std::logic_error);
53         
54         const std::string& filename() const { return _filename; };
55
56         uint16_t type()       const { return _type; }
57         uint16_t ppqn()       const { return _ppqn; }
58         uint16_t num_tracks() const { return _num_tracks; }
59         
60         int read_event(size_t    buf_len,
61                        uint8_t*  buf,
62                        uint32_t* ev_size,
63                        uint32_t* ev_delta_time)
64                 throw (std::logic_error, PrematureEOF, CorruptFile);
65         
66         void close();
67         
68         static uint32_t read_var_len(FILE* fd) throw (PrematureEOF);
69
70 protected:
71         /** size of SMF header, including MTrk chunk header */
72         static const uint32_t HEADER_SIZE = 22;
73
74         std::string _filename;
75         FILE*       _fd;
76         uint16_t    _type;
77         uint16_t    _ppqn;
78         uint16_t    _num_tracks;
79         uint32_t    _track;
80         uint32_t    _track_size;
81 };
82
83
84 } // namespace ARDOUR
85
86 #endif /* __ardour_smf_reader_h__ */
87