MIDI2TXT.CPP
资源名称:midi2txt.rar [点击查看]
上传用户:sun1865
上传日期:2008-12-23
资源大小:59k
文件大小:28k
源码类别:
midi
开发平台:
Visual C++
- static char* version = "midi2txt v1.14 by Guter Nagler (" __DATE__ ")";
- #include "midiio.hpp"
- #include "miditime.hpp"
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #ifdef __MSDOS__
- #include <dos.h>
- #else
- #endif
- #include "gnadvert.h"
- // values for info_
- #define ONLY_ERRORS -2
- #define ONLY_LYRICS -1
- #define ONLY_CHUNK 0
- #define ONLY_SHORT 1
- #define ONLY_INFO 2
- #define ALL_EVENTS 3
- static FILE* outputf = NULL;
- static const char* versioninfo(int version)
- {
- switch(version)
- {
- case 0: return "single multichanneltrack";
- case 1: return "several tracks with seperated channels to play all at once";
- case 2: return "several multichanneltracks to play one by one";
- default: return "unknown version";
- }
- }
- class MidiPrint : public MidiRead
- {
- public:
- MidiPrint(char* name);
- virtual ~MidiPrint();
- virtual void head(unsigned version, unsigned tracks, unsigned unitperbeat);
- void garbage(FILE* f, long datapos, long datalen);
- virtual void track(int trackno, long length, int channel);
- virtual void endtrack(int trackno);
- virtual void seqnumber(unsigned int seqno);
- virtual void text(int what, unsigned len, char* whattext, unsigned char* txt);
- virtual void meta(int what, unsigned len, unsigned char* data);
- virtual void meta(int what, FILE* f, long datapos, long datalen);
- virtual void end();
- virtual void prefixchannel(unsigned char channel);
- virtual void prefixport(unsigned char port);
- virtual void smpteofs(int mode, int hour, int min, int sec, int frame, int fracframe);
- virtual void tact(int nom, int denom, int unitsperbeat, int notes32perbeat);
- virtual void tempo(unsigned long ticks);
- virtual void key(int signature, int isminor);
- virtual void program(int prg, int channel);
- virtual void control(int channel, int what, int value);
- virtual void highbank(int channel, int val);
- virtual void wheel(int channel, int val);
- virtual void breath(int channel, int val);
- virtual void foot(int channel, int val);
- virtual void portamentotime(int channel, int val);
- virtual void data(int channel, int val);
- virtual void volume(int channel, int val);
- virtual void balance(int channel, int val);
- virtual void expression(int channel, int val);
- virtual void lowbank(int channel, int val);
- virtual void hold(int channel, int val);
- virtual void reverb(int channel, int val);
- virtual void chorus(int channel, int val);
- virtual void datainc(int channel, int val);
- virtual void datadec(int channel, int val);
- virtual void lowrpn(int channel, int val);
- virtual void highrpn(int channel, int val);
- virtual void pitchbendrange(int channel, int val);
- virtual void noteon(int channel, int note, int vel);
- virtual void noteoff(int channel, int note, int vel);
- virtual void time(unsigned long ticks);
- virtual void pitchbend(int channel, int val);
- virtual void polyaftertouch(int channel, int note, int val);
- virtual void aftertouch(int channel, int val);
- virtual void songpos(unsigned pos);
- virtual void songselect(unsigned char song);
- virtual void tunerequest();
- virtual void timingclock();
- virtual void start();
- virtual void cont();
- virtual void stop();
- virtual void activesense();
- virtual void sysex(unsigned syslen, unsigned char* sysdata);
- virtual void sysex(FILE* f, long sysdatapos, long sysdatalen);
- virtual void gmreset();
- virtual void gsreset();
- virtual void gsexit();
- virtual void xgreset();
- virtual void endmidi();
- virtual void error(const char* msg);
- virtual void percent(int perc);
- void printchannel(int channel);
- void printcurpos(unsigned long units);
- int info_;
- int printunits_;
- int printms_;
- int marktact_;
- long errorcnt_;
- int printdecimal_; // no program, notes names etc.
- int printchannel_;
- private:
- int indent_;
- MidiTimeTable* tactinfo_;
- char hastempochanges_; // can't print milliseconds in tracks 2,3... if tempo changes
- int nl_;
- int endmark_; // each track should contain FF 2F 00 as last command
- void indent();
- void hex(FILE* f, long pos, long len, FILE* outputf);
- void printxf();
- int runxftrack(int trackno);
- };
- MidiPrint::MidiPrint(char* name) : MidiRead(name, NULL, MIDI_PRELOAD)
- {
- indent_ = 0;
- info_ = ALL_EVENTS;
- marktact_ = printms_ = 0;
- nl_ = 1;
- errorcnt_ = 0;
- hastempochanges_ = 0;
- printdecimal_ = 0;
- printchannel_ = 0;
- tactinfo_ = 0;
- }
- MidiPrint::~MidiPrint()
- {
- delete tactinfo_;
- tactinfo_ = 0;
- }
- void MidiPrint::indent()
- {
- for (; indent_ > 0; indent_--)
- fputc(' ', outputf);
- }
- void MidiPrint::head(unsigned version, unsigned tracks, unsigned unitperbeat)
- {
- if (marktact_)
- {
- tactinfo_ = new MidiTimeTable(midiname(), getf());
- if (tactinfo_)
- {
- if (!tactinfo_->run())
- {
- delete tactinfo_;
- tactinfo_ = 0;
- }
- }
- }
- if (midiname_)
- fprintf(outputf, "// %sn", midiname_);
- if (getcurpos() != 14)
- fprintf(outputf, " // Warning: %ld bytes garbage at beginning of filen", getcurpos() - 14);
- if (tracks_ == 0 && info_ == ONLY_ERRORS)
- error("no track found");
- if (info_ == ONLY_SHORT || info_ == ONLY_LYRICS || info_ == ONLY_ERRORS)
- return;
- fprintf(outputf, "mthdn");
- fprintf(outputf, " version %d // %sn", version, versioninfo(version));
- fprintf(outputf, " // %d track%sn", tracks, tracks != 1 ? "s" : "");
- fprintf(outputf, " unit %d // is 1/4n", unitperbeat);
- if (tracklen_ > 6)
- {
- fprintf(outputf, "// Warning: %ld bytes garbage in header:n", tracklen_ - 6);
- hex(getf(), getcurpos(), tracklen_ - 6, outputf);
- }
- fprintf(outputf, "end mthdn");
- }
- void MidiPrint::track(int trackno, long /*length*/, int channel)
- {
- endmark_ = 0;
- if (info_ == ONLY_SHORT || info_ == ONLY_LYRICS || info_ == ONLY_ERRORS)
- return;
- fprintf(outputf, "nmtrk");
- if (channel >= 0)
- fprintf(outputf, "(%d)", channel+1);
- else if (channel == MULTICHANNEL)
- fprintf(outputf, " // multichannel track");
- fprintf(outputf, " // track %d", trackno);
- fprintf(outputf, "n");
- }
- void MidiPrint::endtrack(int /*trackno*/)
- {
- if (!endmark_ && info_ != ONLY_CHUNK)
- fprintf(outputf, " // Warning: end of track meta event missingn");
- if (info_ == ONLY_SHORT || info_ == ONLY_ERRORS)
- return;
- if (info_ == ONLY_LYRICS)
- {
- if (!nl_)
- {
- nl_ = 1;
- fprintf(outputf, "n");
- }
- return;
- }
- if (info_ >= ALL_EVENTS)
- fprintf(outputf, "n");
- fprintf(outputf, "end mtrkn");
- }
- void printchar(int c)
- {
- if (c < 0)
- return;
- if (c == '\')
- fprintf(outputf, "\\");
- else if (c == '"')
- fprintf(outputf, "\"");
- else if (c != 0 && (isprint(c) || strchr("剶亷櫄