TimidityMidiDriver.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003  The Pentagram Team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 #include "TimidityMidiDriver.h"
00021 
00022 #ifdef USE_TIMIDITY_MIDI
00023 
00024 #include "timidity/timidity.h"
00025 
00026 #ifndef PENTAGRAM_IN_EXULT
00027 #include "GameData.h"
00028 #include "MusicFlex.h"
00029 #include "XMidiFile.h"
00030 #include "XMidiEvent.h"
00031 #include "XMidiEventList.h"
00032 #endif
00033 
00034 const MidiDriver::MidiDriverDesc TimidityMidiDriver::desc = 
00035                 MidiDriver::MidiDriverDesc ("Timidity", createInstance);
00036 
00037 TimidityMidiDriver::TimidityMidiDriver() :
00038         LowLevelMidiDriver()
00039 {
00040 }
00041 
00042 int TimidityMidiDriver::open()
00043 {
00044         sint32 encoding = PE_16BIT|PE_SIGNED;
00045         if (!stereo) encoding |= PE_MONO;
00046         if (NS_TIMIDITY::Timidity_Init_Simple(sample_rate,65536,encoding)) 
00047         {
00048                 perr << NS_TIMIDITY::Timidity_Error() << std::endl;
00049                 return 1;
00050         }
00051 
00052 
00053 #if defined(DEBUG) || defined(PENTAGRAM_IN_EXULT)
00054         // Going through all the XMidi files just takes far too long 
00055         // in DEBUG Builds
00056         memset (used_inst, true, sizeof(bool)*128);
00057         memset (used_drums, true, sizeof(bool)*128);
00058 #else
00059         MusicFlex *music = GameData::get_instance()->getMusic();
00060 
00061         memset (used_inst, false, sizeof(bool)*128);
00062         memset (used_drums, false, sizeof(bool)*128);
00063 
00064         // For all the XMIDIs
00065         for (int i = 0; i < 128; i++)
00066         {
00067                 XMidiFile *x = music->getXMidi(i);
00068                 if (!x) continue;
00069 
00070                 // For every track in the XMIDI
00071                 for (int t = 0; t < x->number_of_tracks(); t++)
00072                 {
00073                         // Get the event list
00074                         XMidiEventList *list = x->GetEventList(t);
00075 
00076                         // No list
00077                         if (!list) continue;
00078 
00079                         // For each event in the track
00080                         for (XMidiEvent *event = list->events; event; event = event->next)
00081                         {
00082                                 // Normal channel patch change
00083                                 if ((event->status&0xF0) == 0xC0 && (event->status&0x0F) != 9)
00084                                 {
00085                                         used_inst[event->data[0]] = true;
00086                                 }
00087                                 // Drum channel note on
00088                                 else if (event->status == 0x99)
00089                                 {
00090                                         if (event->data[1]) 
00091                                                 used_drums[event->data[0]] = true;
00092                                 }
00093                         }
00094                 }
00095         }
00096 #endif
00097 
00098         NS_TIMIDITY::Timidity_FinalInit(used_inst,used_drums);
00099         return 0;
00100 }
00101 
00102 void TimidityMidiDriver::close()
00103 {
00104         NS_TIMIDITY::Timidity_DeInit();
00105 }
00106 
00107 void TimidityMidiDriver::send(uint32 b)
00108 {
00109         NS_TIMIDITY::Timidity_PlayEvent(b&0xFF, (b>>8)&0x7F, (b>>16)&0x7F);
00110 }
00111 
00112 void TimidityMidiDriver::lowLevelProduceSamples(sint16 *samples, uint32 num_samples)
00113 {
00114         NS_TIMIDITY::Timidity_GenerateSamples(samples,num_samples);
00115 }
00116 
00117 #endif //USE_TIMIDITY_MIDI

Generated on Fri Jul 27 22:27:45 2007 for pentagram by  doxygen 1.4.7