00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00055
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
00065 for (int i = 0; i < 128; i++)
00066 {
00067 XMidiFile *x = music->getXMidi(i);
00068 if (!x) continue;
00069
00070
00071 for (int t = 0; t < x->number_of_tracks(); t++)
00072 {
00073
00074 XMidiEventList *list = x->GetEventList(t);
00075
00076
00077 if (!list) continue;
00078
00079
00080 for (XMidiEvent *event = list->events; event; event = event->next)
00081 {
00082
00083 if ((event->status&0xF0) == 0xC0 && (event->status&0x0F) != 9)
00084 {
00085 used_inst[event->data[0]] = true;
00086 }
00087
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