|
| 1 | +// |
| 2 | +// Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu> |
| 3 | +// Creation Date: Mon Feb 16 12:26:32 PST 2015 Adapted from binasc program. |
| 4 | +// Last Modified: Wed Feb 18 14:48:21 PST 2015 |
| 5 | +// Filename: midifile/include/Binasc.cpp |
| 6 | +// Syntax: C++11 |
| 7 | +// vim: ts=3 expandtab |
| 8 | +// |
| 9 | +// description: Interface to convert bytes between binary and ASCII forms. |
| 10 | +// |
| 11 | + |
| 12 | +#ifndef _BINASC_H_INCLUDED |
| 13 | +#define _BINASC_H_INCLUDED |
| 14 | + |
| 15 | +#include <iostream> |
| 16 | +#include <fstream> |
| 17 | + |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +typedef unsigned char uchar; |
| 21 | +typedef unsigned short ushort; |
| 22 | +typedef unsigned long ulong; |
| 23 | + |
| 24 | +class Binasc { |
| 25 | + public: |
| 26 | + Binasc (void); |
| 27 | + ~Binasc (); |
| 28 | + |
| 29 | + // functions for setting options: |
| 30 | + int setLineLength (int length); |
| 31 | + int getLineLength (void); |
| 32 | + int setLineBytes (int length); |
| 33 | + int getLineBytes (void); |
| 34 | + void setComments (int state); |
| 35 | + void setCommentsOn (void); |
| 36 | + void setCommentsOff (void); |
| 37 | + void setBytes (int state); |
| 38 | + void setBytesOn (void); |
| 39 | + void setBytesOff (void); |
| 40 | + |
| 41 | + // functions for converting into a binary file: |
| 42 | + void writeToBinary (const string& outfile, const string& infile); |
| 43 | + void writeToBinary (const string& outfile, istream& input); |
| 44 | + ostream& writeToBinary (ostream& out, const string& infile); |
| 45 | + ostream& writeToBinary (ostream& out, istream& input); |
| 46 | + |
| 47 | + // functions for converting into an ASCII file with hex bytes: |
| 48 | + void readFromBinary (const string& outfile, const string& infile); |
| 49 | + void readFromBinary (const string& outfile, istream& input); |
| 50 | + ostream& readFromBinary (ostream& out, const string& infile); |
| 51 | + ostream& readFromBinary (ostream& out, istream& input); |
| 52 | + |
| 53 | + ostream& outputStyleMidiFile(ostream& out, istream& input); |
| 54 | + int readMidiEvent (ostream& out, istream& infile, int& trackbytes, |
| 55 | + int& command); |
| 56 | + int getVLV (istream& infile, int& trackbytes); |
| 57 | + |
| 58 | + // static functions for writing ordered bytes: |
| 59 | + static ostream& writeLittleEndianUShort (ostream& out, ushort value); |
| 60 | + static ostream& writeBigEndianUShort (ostream& out, ushort value); |
| 61 | + static ostream& writeLittleEndianShort (ostream& out, short value); |
| 62 | + static ostream& writeBigEndianShort (ostream& out, short value); |
| 63 | + static ostream& writeLittleEndianULong (ostream& out, ulong value); |
| 64 | + static ostream& writeBigEndianULong (ostream& out, ulong value); |
| 65 | + static ostream& writeLittleEndianLong (ostream& out, long value); |
| 66 | + static ostream& writeBigEndianLong (ostream& out, long value); |
| 67 | + static ostream& writeLittleEndianFloat (ostream& out, float value); |
| 68 | + static ostream& writeBigEndianFloat (ostream& out, float value); |
| 69 | + static ostream& writeLittleEndianDouble (ostream& out, double value); |
| 70 | + static ostream& writeBigEndianDouble (ostream& out, double value); |
| 71 | + |
| 72 | + protected: |
| 73 | + // helper functions for reading ASCII content to conver to binary: |
| 74 | + ostream& processLine (ostream& out, char* word, int lineNum); |
| 75 | + ostream& processAsciiWord (ostream& out, const char* word, int lineNum); |
| 76 | + ostream& processBinaryWord (ostream& out, const char* word, int lineNum); |
| 77 | + ostream& processDecimalWord (ostream& out, const char* word, int lineNum); |
| 78 | + ostream& processHexWord (ostream& out, const char* word, int lineNum); |
| 79 | + ostream& processVlvWord (ostream& out, const char* word, int lineNum); |
| 80 | + ostream& processMidiPitchBendWord(ostream& out, const char* word, |
| 81 | + int lineNum); |
| 82 | + |
| 83 | + // helper functions for reading binary content to convert to ASCII: |
| 84 | + ostream& outputStyleAscii (ostream& out, istream& input); |
| 85 | + ostream& outputStyleBinary (ostream& out, istream& input); |
| 86 | + ostream& outputStyleBoth (ostream& out, istream& input); |
| 87 | + |
| 88 | + private: |
| 89 | + int bytesQ; // option for printing hex bytes in ASCII output. |
| 90 | + int commentsQ; // option for printing comments in ASCII output. |
| 91 | + int maxLineLength; // number of character in ASCII output on a line. |
| 92 | + int maxLineBytes; // number of hex bytes in ASCII output on a line. |
| 93 | +}; |
| 94 | + |
| 95 | + |
| 96 | +#endif /* _BINASC_H_INCLUDED */ |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments