MIDIFileWriter.cxx

Go to the documentation of this file.
00001 #include "MIDITrack.hxx"
00002 #include "MIDISong.hxx"
00003 #include "MIDIWriter.hxx"
00004 #include "MIDIFileWriter.hxx"
00005 
00006 namespace CLAM
00007 {
00008         MIDIFileWriter::MIDIFileWriter()
00009                 : mInput("MIDISong Input",this)
00010         {
00011                 MIDIFileIOConfig cfg;
00012                 Configure(cfg);
00013         }
00014 
00015         MIDIFileWriter::MIDIFileWriter(const MIDIFileIOConfig& cfg)
00016                 : mInput("MIDISong Input",this)
00017         {
00018                 Configure(cfg);
00019         }
00020 
00021         MIDIFileWriter::~MIDIFileWriter()
00022         {
00023         }
00024 
00025         bool MIDIFileWriter::Do()
00026         {
00027                 bool result = Do(mInput.GetData());
00028                 mInput.Consume();
00029                 return result;
00030         }
00031 
00032         bool MIDIFileWriter::Do(const MIDISong& in)
00033         {
00034                 MIDI::Song s;
00035                 s.SetTicksPerQ(96);
00036                 // add tempo 
00037                 MIDI::Track* tempo_track = new MIDI::Track;
00038                 s.AddTrack(tempo_track);
00039                 MIDI::MetaEvent* m_ev = new MIDI::MetaEvent(MIDI::Message(0xFF,0x51,0x03),0,3);
00040                 m_ev->mData[0]=0x07; m_ev->mData[1]=0xA1; m_ev->mData[2]=0x20;
00041                 tempo_track->Add(m_ev);
00042                 
00043                 int nTracks = in.GetNumberOfTracks();
00044                 for(int i=0; i < nTracks; i++)
00045                 {
00046                         MIDI::Track* track = new MIDI::Track;
00047                         s.AddTrack(track);
00048                         BuildEventList(in.GetTracks()[i]);
00049                         std::list<MIDIEvent>::const_iterator it = mEventList.begin();
00050                         for(; it != mEventList.end(); it++)
00051                         {
00052                                 const MIDIEvent &ev = *it;
00053                                 // add event to current track
00054                                 if(ev.Length()==2)
00055                                 {
00056                                         track->Add(new MIDI::Event(MIDI::Message(ev.GetMessage()[0], ev.GetMessage()[1]), ev.Ticks()));
00057                                 }
00058                                 else
00059                                 {
00060                                         track->Add(new MIDI::Event(MIDI::Message(ev.GetMessage()[0],ev.GetMessage()[1],ev.GetMessage()[2]), ev.Ticks()));
00061                                 }
00062                         }
00063                 }
00064                 // write midi file
00065                 MIDI::Writer w(mConfig.GetFileName().c_str());
00066                 w.Write(s);
00067                 return true;
00068         }
00069 
00070         bool MIDIFileWriter::ConcreteConfigure(const ProcessingConfig& c)
00071         {
00072                 CopyAsConcreteConfig(mConfig, c);
00073                 return true;
00074         }
00075 
00076         void MIDIFileWriter::BuildEventList(const MIDITrack& trk)
00077         {
00078                 mEventList.clear();
00079                 int nEvents = trk.GetEventInfo().Size();
00080                 for(int i=0; i < nEvents; i++)
00081                 {
00082                         mEventList.push_back(trk.GetEventInfo()[i]);
00083                 }
00084                 TData t;
00085                 int channel=0;
00086                 MIDIEvent ev;
00087                 ev.GetMessage().Resize(3);
00088                 ev.GetMessage().SetSize(3);
00089                 int nNotes = trk.GetTrackMelody().GetNumberOfNotes();
00090                 for(int i=0; i < nNotes; i++)
00091                 {
00092                         if(i < trk.GetChannels().Size()) channel = trk.GetChannels()[i];
00093                         ev.GetMessage()[0] = (0x90 | channel);
00094                         t = trk.GetTrackMelody().GetNoteArray()[i].GetTime().GetBegin();
00095                         int key = trk.GetTrackMelody().GetNoteArray()[i].GetKey();
00096                         int vel = trk.GetTrackMelody().GetNoteArray()[i].GetVelocity();
00097                         ev.SetTime(t);
00098                         ev.GetMessage()[1]=key;
00099                         ev.GetMessage()[2]=vel;
00100                         mEventList.push_back(ev);
00101                         t = trk.GetTrackMelody().GetNoteArray()[i].GetTime().GetEnd();
00102                         ev.SetTime(t);
00103                         ev.GetMessage()[2]=0;
00104                         mEventList.push_back(ev);
00105                 }
00106                 mEventList.sort();
00107         }
00108 }
00109 
00110 // END
00111 
Generated by  doxygen 1.6.3