-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binasc.h
159 lines (137 loc) · 7.64 KB
/
Binasc.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Feb 16 12:26:32 PST 2015 Adapted from binasc program.
// Last Modified: Sat Apr 21 10:52:19 PDT 2018 Removed using namespace std;
// Filename: midifile/include/Binasc.h
// Website: http://midifile.sapp.org
// Syntax: C++11
// vim: ts=3 noexpandtab
//
// description: Interface to convert bytes between binary and ASCII forms.
//
#ifndef _BINASC_H_INCLUDED
#define _BINASC_H_INCLUDED
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h> /* needed for MinGW */
namespace smf {
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned long ulong;
class Binasc {
public:
Binasc (void);
~Binasc ();
// functions for setting options:
int setLineLength (int length);
int getLineLength (void);
int setLineBytes (int length);
int getLineBytes (void);
void setComments (int state);
void setCommentsOn (void);
void setCommentsOff (void);
int getComments (void);
void setBytes (int state);
void setBytesOn (void);
void setBytesOff (void);
int getBytes (void);
void setMidi (int state);
void setMidiOn (void);
void setMidiOff (void);
int getMidi (void);
// functions for converting into a binary file:
int writeToBinary (const std::string& outfile,
const std::string& infile);
int writeToBinary (const std::string& outfile,
std::istream& input);
int writeToBinary (std::ostream& out,
const std::string& infile);
int writeToBinary (std::ostream& out,
std::istream& input);
// functions for converting into an ASCII file with hex bytes:
int readFromBinary (const std::string&
outfile,
const std::string& infile);
int readFromBinary (const std::string& outfile,
std::istream& input);
int readFromBinary (std::ostream& out,
const std::string& infile);
int readFromBinary (std::ostream& out,
std::istream& input);
// static functions for writing ordered bytes:
static std::ostream& writeLittleEndianUShort (std::ostream& out,
ushort value);
static std::ostream& writeBigEndianUShort (std::ostream& out,
ushort value);
static std::ostream& writeLittleEndianShort (std::ostream& out,
short value);
static std::ostream& writeBigEndianShort (std::ostream& out,
short value);
static std::ostream& writeLittleEndianULong (std::ostream& out,
ulong value);
static std::ostream& writeBigEndianULong (std::ostream& out,
ulong value);
static std::ostream& writeLittleEndianLong (std::ostream& out,
long value);
static std::ostream& writeBigEndianLong (std::ostream& out,
long value);
static std::ostream& writeLittleEndianFloat (std::ostream& out,
float value);
static std::ostream& writeBigEndianFloat (std::ostream& out,
float value);
static std::ostream& writeLittleEndianDouble (std::ostream& out,
double value);
static std::ostream& writeBigEndianDouble (std::ostream& out,
double value);
static std::string keyToPitchName (int key);
protected:
int m_bytesQ; // option for printing hex bytes in ASCII output.
int m_commentsQ; // option for printing comments in ASCII output.
int m_midiQ; // output ASCII data as parsed MIDI file.
int m_maxLineLength;// number of character in ASCII output on a line.
int m_maxLineBytes; // number of hex bytes in ASCII output on a line.
private:
// helper functions for reading ASCII content to conver to binary:
int processLine (std::ostream& out,
const std::string& input,
int lineNum);
int processAsciiWord (std::ostream& out,
const std::string& input,
int lineNum);
int processStringWord (std::ostream& out,
const std::string& input,
int lineNum);
int processBinaryWord (std::ostream& out,
const std::string& input,
int lineNum);
int processDecimalWord (std::ostream& out,
const std::string& input,
int lineNum);
int processHexWord (std::ostream& out,
const std::string& input,
int lineNum);
int processVlvWord (std::ostream& out,
const std::string& input,
int lineNum);
int processMidiPitchBendWord(std::ostream& out,
const std::string& input,
int lineNum);
int processMidiTempoWord (std::ostream& out,
const std::string& input,
int lineNum);
// helper functions for reading binary content to convert to ASCII:
int outputStyleAscii (std::ostream& out, std::istream& input);
int outputStyleBinary (std::ostream& out, std::istream& input);
int outputStyleBoth (std::ostream& out, std::istream& input);
int outputStyleMidi (std::ostream& out, std::istream& input);
// MIDI parsing helper functions:
int readMidiEvent (std::ostream& out, std::istream& infile,
int& trackbytes, int& command);
int getVLV (std::istream& infile, int& trackbytes);
int getWord (std::string& word, const std::string& input,
const std::string& terminators, int index);
};
} // end of namespace smf
#endif /* _BINASC_H_INCLUDED */