CLAM-Development  1.4.0
MIDIReader.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * MIDIFileReader C++ classes
20  * This code is part of the CLAM library, but also usable stand-alone.
21  * Maarten de Boer <mdeboer@iua.upf.es>
22  *
23  */
24 #ifndef __MIDIReader__
25 #define __MIDIReader__
26 
27 #include <stdio.h>
28 #include <CLAM/MIDIDataTypes.hxx>
29 #include "MIDIChunkType.hxx"
30 
31 namespace MIDI
32 {
33  class Song;
34 
35  class Reader
36  /* class to read midi data from a file, parse it and store it in a song;
37  */
38  {
39  private:
40  FILE* mFile;
41  int mCnt;
42  public:
43  Reader(const char* filename)
44  {
45  mFile = fopen(filename,"rb");
46  }
47  bool Ok(void)
48  {
49  return mFile!=NULL;
50  }
51  Byte GetByte(void)
52  {
53  mCnt++;
54  return fgetc(mFile);
55  }
56  unsigned int GetInt(void)
57  {
58  unsigned int val;
59 
60  val = GetByte();
61  val <<=8;
62  val |= GetByte();
63  val <<=8;
64  val |= GetByte();
65  val <<=8;
66  val |= GetByte();
67 
68  return val;
69  }
70  unsigned short GetShort(void)
71  {
72  unsigned short val;
73 
74  val = GetByte();
75  val <<= 8;
76  val |= GetByte();
77 
78  return val;
79  }
80 
82  {
83  Byte bytes[4];
84  for (int i=0;i<4;i++) bytes[i] = GetByte();
85  return ChunkType(bytes);
86  }
87 
88  unsigned int GetVarLength(void)
89  {
90  /* read a variable lenght value. see midi file spec */
91  unsigned int ret = 0;
92  Byte tmp;
93 
94  tmp = GetByte();
95  ret = tmp&0x7F;
96  while (tmp&0x80)
97  {
98  tmp = GetByte();
99  ret <<= 7;
100  ret |= (tmp&0x7F);
101  }
102  return ret;
103  }
104 
105  class Error
106  {
107  public:
108  const char* mStr;
109  Error(const char* str):mStr(str) { printf(str); }
110  };
111 
112  void Read(Song& s);
113  };
114 
115 }
116 
117 #endif
118