MIDIReader.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  * UNIVERSITAT POMPEU FABRA
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018  *
00019  * MIDIFileReader C++ classes
00020  * This code is part of the CLAM library, but also usable stand-alone.
00021  * Maarten de Boer <mdeboer@iua.upf.es>
00022  *
00023  */
00024 #ifndef __MIDIReader__
00025 #define __MIDIReader__
00026 
00027 #include <stdio.h>
00028 #include <CLAM/MIDIDataTypes.hxx>
00029 #include "MIDIChunkType.hxx"
00030 
00031 namespace MIDI
00032 {
00033         class Song;
00034 
00035         class Reader
00036         /* class to read midi data from a file, parse it and store it in a song;
00037         */
00038         {
00039         private:
00040                 FILE* mFile;
00041                 int mCnt;
00042         public:
00043                 Reader(const char* filename)
00044                 {
00045                         mFile = fopen(filename,"rb");
00046                 }
00047                 bool Ok(void)
00048                 {
00049                         return mFile!=NULL;
00050                 }
00051                 Byte GetByte(void)
00052                 {
00053                         mCnt++;
00054                         return fgetc(mFile);
00055                 }
00056                 unsigned int GetInt(void)
00057                 {
00058                         unsigned int val;
00059 
00060                         val = GetByte();
00061                         val <<=8;
00062                         val |= GetByte();
00063                         val <<=8;
00064                         val |= GetByte();
00065                         val <<=8;
00066                         val |= GetByte();
00067                         
00068                         return val;
00069                 }
00070                 unsigned short GetShort(void)
00071                 {
00072                         unsigned short val;
00073 
00074                         val = GetByte();
00075                         val <<= 8;
00076                         val |= GetByte();
00077 
00078                         return val;
00079                 }
00080 
00081                 ChunkType GetChunkType(void)
00082                 {
00083                         Byte bytes[4];
00084                         for (int i=0;i<4;i++) bytes[i] = GetByte();
00085                         return ChunkType(bytes);
00086                 }
00087 
00088                 unsigned int GetVarLength(void)
00089                 {
00090                         /* read a variable lenght value. see midi file spec */
00091                         unsigned int ret = 0;
00092                         Byte tmp;
00093 
00094                         tmp = GetByte();
00095                         ret = tmp&0x7F;
00096                         while (tmp&0x80)
00097                         {
00098                                 tmp = GetByte();
00099                                 ret <<= 7;
00100                                 ret |= (tmp&0x7F);
00101                         }
00102                         return ret;
00103                 }
00104 
00105                 class Error
00106                 {
00107                 public:
00108                         const char* mStr;
00109                         Error(const char* str):mStr(str) { printf(str); }
00110                 };
00111 
00112                 void Read(Song& s);
00113         };
00114 
00115 }
00116 
00117 #endif
00118 
Generated by  doxygen 1.6.3