forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMFReader.hpp
More file actions
95 lines (80 loc) · 2.79 KB
/
SMFReader.hpp
File metadata and controls
95 lines (80 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* SMFReader.hpp
* Copyright © 2012 kbinani
*
* This file is part of libvsq.
*
* libvsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* libvsq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef __SMFReader_hpp__
#define __SMFReader_hpp__
#include "vsqglobal.hpp"
#include "InputStream.hpp"
#include "MidiEvent.hpp"
#include "BitConverter.hpp"
#include <vector>
VSQ_BEGIN_NAMESPACE
using namespace std;
using namespace VSQ_NS;
/**
* @brief SMF(Standard MIDI File)の読み込みを行うクラス
*/
class SMFReader{
public:
class ParseException : public std::exception{
};
/**
* @brief ストリームから、SMF を読み込む
* @param [in] stream 読み込むストリーム
* @param [out] dest 読み込んだ MIDI イベントのリスト。MIDI イベントは、トラックごとに格納される
* @param [out] format SMF のフォーマット
* @param [out] timeFormat 時間分解能
*/
void read( InputStream *stream, vector<vector<MidiEvent> > &dest, int &format, int &timeFormat ){
// ヘッダ
char byte4[4] = { 0 };
stream->read( byte4, 0, 4 );
if( BitConverter::makeUInt32BE( byte4 ) != 0x4d546864 ){
throw ParseException();
}
// データ長
stream->read( byte4, 0, 4 );
// フォーマット
stream->read( byte4, 0, 2 );
format = BitConverter::makeUInt16BE( byte4 );
// トラック数
stream->read( byte4, 0, 2 );
int tracks = (int)BitConverter::makeUInt16BE( byte4 );
// 時間分解能
stream->read( byte4, 0, 2 );
timeFormat = BitConverter::makeUInt16BE( byte4 );
// 各トラックを読込み
dest.clear();
for( int track = 0; track < tracks; track++ ){
dest.push_back( vector<MidiEvent>() );
// ヘッダー
stream->read( byte4, 0, 4 );
if( BitConverter::makeUInt32BE( byte4 ) != 0x4d54726b ){
throw ParseException();// "header error; MTrk" );
}
// チャンクサイズ
stream->read( byte4, 0, 4 );
long size = (long)BitConverter::makeUInt32BE( byte4 );
int64_t startpos = stream->getPointer();
// チャンクの終わりまで読込み
tick_t clock = 0;
uint8_t last_status_byte = 0x00;
while( stream->getPointer() < startpos + size ){
MidiEvent mi = MidiEvent::read( stream, clock, last_status_byte );
dest[track].push_back( mi );
}
}
}
};
VSQ_END_NAMESPACE
#endif