1 /*
2 Copyright (c) 2003-2006 yuno
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice,
8    this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright notice,
10    this list of conditions and the following disclaimer in the documentation
11    and/or other materials provided with the distribution.
12 3. Neither the name of copyright holders nor the names of its contributors
13    may be used to endorse or promote products derived from this software
14    without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef midisequencer_h
29 #define midisequencer_h
30 
31 #include <stdint.h>
32 #include <cstdio>
33 #include <string>
34 #include <vector>
35 #include <chrono>
36 
37 #define META_EVENT_ALL_NOTE_OFF 0x8888
38 
39 namespace midisequencer{
40     /*
41     typedef unsigned long uint_least32_t;
42     */
43     struct midi_message{
44         std::chrono::microseconds time;
45         uint_least32_t message;
46         int port;
47         int track;
48         uint_least32_t tempo;
49     };
50 
51     class uncopyable{
52     public:
uncopyable()53         uncopyable(){}
54     private:
55         uncopyable(const uncopyable&);
56         void operator=(const uncopyable&);
57     };
58 
59     class output:uncopyable{
60     public:
61         virtual void midi_message(int port, uint_least32_t message) = 0;
62         virtual void sysex_message(int port, const void* data, std::size_t size) = 0;
63         virtual void meta_event(int type, const void* data, std::size_t size) = 0;
64         virtual void reset() = 0;
65     protected:
~output()66         ~output(){}
67     };
68 
69     class sequencer:uncopyable{
70     public:
71         sequencer();
72         void clear();
73         void rewind();
74         std::vector<midi_message>::iterator rewind_to_loop();
75         bool is_at_end();
76         bool load(void* fp, int(*fgetc)(void*));
77         bool load(std::FILE* fp);
78         int get_num_ports()const;
79         std::chrono::microseconds get_total_time()const;
80         std::string get_title()const;
81         std::string get_copyright()const;
82         std::string get_song()const;
83         uint32_t get_division()const;
84         void play(std::chrono::microseconds time, output* out);
85         void set_time(std::chrono::microseconds time, output* out);
86         std::chrono::microseconds get_start_skipping_silence();
87     private:
88         std::vector<midi_message> messages;
89         std::vector<midi_message>::iterator position;
90         std::vector<midi_message>::iterator loop_position;
91         std::vector<std::string> long_messages;
92         uint32_t division = 0;
93         void load_smf(void* fp, int(*fgetc)(void*));
94     };
95 }
96 
97 #endif
98