⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 option.c

📁 一个很好的c++ midi解码算法,不依赖于任何平台.
💻 C
字号:
/* *  option.cc *  Copyright (C) 1998-2000 SAKAI Katsuya * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <stdio.h> //#include <iostream.h>#include <stdlib.h>#include <string.h>#include <sys/time.h>#include <ctype.h>#include "option.h"#include "config.h"#include "defines.h"OPTION::OPTION(){  Data = 0;  tempo = 100;  NumOfData = 0L;  repeat    = 1L;  shuffle   = FALSE;  useSerial = FALSE;  PortAdevice = PORT_A;  PortBdevice = PORT_B;  PortCdevice = PORT_C;  PortDdevice = PORT_D;  PortEdevice = PORT_E;  PortFdevice = PORT_F;  SerialDevice[0] = '\0';  SequencerDevice[0] = '\0';  Output   = DEFAULTMODE;    changeBank32 = 255;    timeval tp;  gettimeofday(&tp, NULL);  srand(tp.tv_usec);}char *OPTION::filename(char *path){  char *p;  if((p=strrchr(path, '/'))!=NULL)    p++;  else    p=path;  return p;}void OPTION::shuffleData(){  if(NumOfData<=0) return;    char *temp;  u_long i, j;  for(i=0; i<NumOfData; i++)    {      j = rand() % NumOfData;      temp = Data[i];      Data[i] = Data[j];      Data[j] = temp;    }}u_long OPTION::readDec(char *chkop){  char c;  c=*chkop++;  u_long data=0L;  if(c=='\0') usage();  if(isdigit(c)){    do{      data = data * 10 + (u_long)(c-'0');      c=*chkop++;    } while(isdigit(c));    if(c!='\0') usage();  } else usage();  return data;}void OPTION::readDec(char *chkop, u_long *val1, u_long *val2){  char c;  c=*chkop++;  u_long data=0L;  if(c=='\0') usage();  if(!isdigit(c)) usage();  do{    data = data * 10 + (u_long)(c-'0');    c=*chkop++;  } while(isdigit(c));  if(c!=',') usage();  *val1 = data; data=0L;  c=*chkop++;  if(!isdigit(c)) usage();  do{    data = data * 10 + (u_long)(c-'0');    c=*chkop++;  } while(isdigit(c));  if(c!='\0') usage();  *val2 = data;}void OPTION::getOption(int argc, char *argv[]){  int i;  u_long dummy;  char *chkop;  char c;    progname = argv[0];  wait=DEFAULT_WAIT;  strcpy (SerialDevice, SEQ_SERIAL);  strcpy (SequencerDevice, SEQUENCER); #ifdef DEFAULT_RESET  strcpy (ResetDataName, DEFAULT_RESET);#else  ResetDataName[0]='\0';#endif  Data = new char* [argc-1];  #ifndef NO_OSS  u_long port, dev;#endif  for(i=1;i<argc;i++)    {      chkop=argv[i];      if(*chkop=='-')	{	  if (*++chkop == '\0')	    usage();	  c=*chkop++;	  switch (c)	    {	      #ifndef NO_OSS	    case 'd':	      readDec(chkop, &port, &dev);#ifdef SHOW_OPTION	      cout << "port:" << port << " dev:" << dev << endl;#endif	      if(port>5 || dev>15)		usage();	      else		switch(port)		  {		  case 0:		    PortAdevice = (u_char)dev; break;		  case 1:		    PortBdevice = (u_char)dev; break;		  case 2:		    PortCdevice = (u_char)dev; break;		  case 3:		    PortDdevice = (u_char)dev; break;		  case 4:		    PortEdevice = (u_char)dev; break;		  case 5:		    PortFdevice = (u_char)dev; break;		  }	      break;	      	    case 'm' :	      dummy = readDec(chkop);	      if(dummy>2)		usage();	      else		Output = dummy;	      break;#endif  // NO_OSS	      	    case 'w' :	      wait = readDec(chkop);	      break;	      	    case 'R' :	      if(*chkop=='\0')		repeat=~0;	      else		{		  repeat=readDec(chkop);		}#ifdef SHOW_OPTION	      cout << "repeat:" << repeat << endl;#endif	      break;	      	    case 'r' :	      if (chkop[0]=='\0') usage();	      else if (strcmp(chkop, "0")==0)		ResetDataName[0] = '\0';	      else if (strcmp(chkop, "1")==0)		strcpy(ResetDataName, RESET1);	      else if (strcmp(chkop, "2")==0)		strcpy(ResetDataName, RESET2);	      else if (strcmp(chkop, "3")==0)		strcpy(ResetDataName, RESET3);	      else if (strcmp(chkop, "4")==0)		strcpy(ResetDataName, RESET4);	      else		strcpy(ResetDataName, chkop);	      #ifdef SHOW_OPTION	      cout << "reset data : ";	      if(ResetDataName[0]=='\0') cout << "none";	      else cout << ResetDataName;	      cout << endl;#endif	      break;	      	    case 's' :	      if (*chkop=='\0')		{		  shuffle = 1 - shuffle;		}#ifndef NO_OSS	      else		{		  if(strcmp(chkop, "erial")==0)		    useSerial = 1 - useSerial;		  else		    usage();		}#endif	      break;	      	    case 'n':	      if (*chkop!='\0')		strcpy (SequencerDevice, chkop);	      else		usage ();#ifdef SHOW_OPTION	      cout << "sequencer : " << SequencerDevice << endl;#endif	      break;	      	    case 'o':	      if (*chkop!='\0')		strcpy (SerialDevice, chkop);	      else		usage ();	      // Output = Serial;#ifdef SHOW_OPTION	      cout << "serial : " << SerialDevice << endl;#endif	      break;	      	    case 't':	      dummy = readDec(chkop);	      if(dummy>5000 || dummy==0)		usage();	      else		tempo = (u_short)dummy;	      break;	      	    case 'b':	      if(*chkop=='\0')		changeBank32 = 128;	      else		{		  dummy = readDec(chkop);		  if (dummy>127)		    usage ();		  else		    changeBank32 = (u_char)dummy;		}#ifdef SHOW_OPTION	      cout << "change CC#32 : " << (int)changeBank32 << endl;#endif	      break;	      	      	    default:	      usage();	      break;	    }	}      else	{	  Data[NumOfData] = argv[i];	  NumOfData++;	}    }  if(NumOfData == 0)    usage();}void OPTION::usage(){#if 0  cout << endl       << "MIDI File Player for MID/RCP/R36/G18/G36" << endl       << filename(progname) << " version " << VERSION#ifdef RUNNING_ST       << '+'#endif#ifdef NO_OSS       << '.'#endif       << endl       << "Copyright (C) 1998-2000 Sakai Katsuya" << endl       << "This software comes with ABSOLUTELY NO WARRANTY." << endl       << endl        << "usage: " << filename(progname)       << " [-options...] files... [-options... or files...]"<< endl        << "   option" << endl       << "   -b[xxx]  : change BankSelectLSB(CC#32) values for xxx"       << endl       << "              xxx:0...127" << endl#ifndef NO_OSS       << "   -dxx,yyy : output port xx to midi device #yyy" << endl       << "              xx:0...5 ,  yyy:0...15" << endl        << "   -mxx     : set output mode (xx= 0:Sequencer, 1:Serial)" << endl    // << "   -mxx     : set output mode " << endl    // << "              (xx= 0:Sequencer, 1:Serial, 2:RawMIDI+Serial)"<<endl       << "   -n<dev>  : set sequencer device (default:"<<SEQUENCER<<")"<<endl#endif       << "   -o<dev>  : set serial device    (default:"<<SEQ_SERIAL<<")"<<endl    // << "   -p<dev>  : set raw midi device  (default:"<<SEQ_SERIAL<<")"<<endl       << "   -R[xxx]  : repeat xxx times" << endl       << "   -r0      : not send initialize data" << endl       << "   -r1      : send RESET1 (" << filename(RESET1) << ')' << endl       << "   -r2      : send RESET2 (" << filename(RESET2) << ')' << endl       << "   -r3      : send RESET3 (" << filename(RESET3) << ')' << endl       << "   -r4      : send RESET4 (" << filename(RESET4) << ')' << endl       << "   -s       : shuffle play" << endl#ifndef NO_OSS       << "   -serial  : use 0xF5 command to change port" << endl#endif    // << "   -wxxx  : song interval (xxx msec)" << endl    // << "   -txxx  : master tempo * xxx%" << endl  // for debug       << endl;#else       printf("\nMIDI File Player for MID/RCP/R36/G18/G36\n");       printf("%s version %s", filename(progname), VERSION);#ifdef RUNNING_ST       printf("+");#endif#ifdef NO_OSS       printf(".");#endif	   printf("\n");	   printf("Copyright (C) 1998-2000 Sakai Katsuya\n");       printf("This software comes with ABSOLUTELY NO WARRANTY.\n\n");       printf("usage: %s",filename(progname));       printf(" [-options...] files... [-options... or files...]\n");        printf("   option\n");       printf("   -b[xxx]  : change BankSelectLSB(CC#32) values for xxx\n");       printf("              xxx:0...127\n");#ifndef NO_OSS       printf("   -dxx,yyy : output port xx to midi device #yyy\n");       printf("              xx:0...5 ,  yyy:0...15\n");       printf("   -mxx     : set output mode (xx= 0:Sequencer, 1:Serial)\n");       printf("   -n<dev>  : set sequencer device (default:%s)\n",SEQUENCER);#endif       printf("   -o<dev>  : set serial device    (default:%s)\n",SEQ_SERIAL);       printf("   -R[xxx]  : repeat xxx times\n");       printf("   -r0      : not send initialize data\n");//       printf("   -r1      : send RESET1 (%s)\n",filename(RESET1));//       printf("   -r2      : send RESET2 (%s)\n",filename(RESET2));//       printf("   -r3      : send RESET3 (%s)\n",filename(RESET3));//       printf("   -r4      : send RESET4 (%s)\n",filename(RESET4));//       printf("   -s       : shuffle play\n")#ifndef NO_OSS       printf("   -serial  : use 0xF5 command to change port\n\n");#endif	#endif  exit(1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -