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

📄 seq_smf0.c

📁 一个很好的c++ midi解码算法,不依赖于任何平台.
💻 C
字号:
/* *  seq_smf0.cc *  Copyright (C) 1998,1999 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 <string.h>#include <sys/ioctl.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include "defines.h"#include "config.h"#include "seq_smf0.h"#include "extern.h"int Sequencer_SMF0::open(char *, int buflen){  if (flagOpen == TRUE) return -1;  seqfd = 1;  /*  if ((seqfd = ::open("test.mid", O_WRONLY|O_CREAT|O_TRUNC, 0644))<0)	{	  perror("cannot open device");	  return -2;	}	*/  _seqbuf = new u_char [buflen];  if (!_seqbuf)	{	  printf("allocation error \n"); // cerr << "allocation error" << endl;	  return -3;	}  _seqbuflen = buflen;  _seqbufptr = 0;  flagOpen = TRUE;  seqstart=FALSE;  tempoflag=FALSE;  return 0;}int Sequencer_SMF0::close(){  if (flagOpen != TRUE) return -1;  if (seqstart==TRUE)	stopTimer();  delete [] _seqbuf;  flagOpen = FALSE;  //  ::close(seqfd);  return 0;}int Sequencer_SMF0::checkMidiDevice(){  numberOfDevice = 1;  return numberOfDevice;}void Sequencer_SMF0::reset(){  seqbuf_dump();  _seqbufptr = 0;}void Sequencer_SMF0::sync(){  //  seqbuf_dump();}void Sequencer_SMF0::startTimer(){  if (seqstart==TRUE)	{	  printf("warning:startTimer:already started\n"); // cout << "warning:startTimer:already started" << endl;	  return;	}#ifdef RUNNING_ST  for(int n=0;n<16;n++) clearStatus(n);#endif  LastOutputPort = 0;  Tick=LastTick=0;  seqstart = TRUE;  /*** MThd, MTrk $B$r=q$-=P$9(B ***/  write_string ((char *)"MThd");  write_conlen (6, 4);  write_conlen (0, 2);  write_conlen (1, 2);  write_conlen (timebase, 2);  write_string ((char *)"MTrk");  write_conlen (0, 4); // dummy  if (tempoflag==FALSE)	set_tempo (tempo);}void Sequencer_SMF0::stopTimer(){  if (seqstart==FALSE)	{	  printf("warning:stopTimer:already stopped\n"); // cout << "warning:stopTimer:already stopped" << endl;	  return;	}  changePort (0, 0);  LastOutputPort = 0;  write_tick();  seq_midiout (0xFF);  seq_midiout (0x2F);  seq_midiout (0x00);  seqbuf_dump();  seqstart=FALSE;  tempoflag=FALSE;  /*** $B%U%!%$%kD9$r<hF@$7$F!"(BMTrk$B$N(BLength$B$r(Bfile$B$K=q$/(B  ***/  u_long len;  len = lseek (seqfd, 0, SEEK_END);  lseek (seqfd, 18L, SEEK_SET);  write_conlen (len-22, 4);  seqbuf_dump();}void Sequencer_SMF0::waitTime(u_long tick){  Tick = tick;}void Sequencer_SMF0::midiEvent1(u_char port, u_char ev, u_char para1){  if(port!=LastOutputPort)	{	  changePort(0, port);	  LastOutputPort = port;	}  write_tick();  if (_seqbufptr+2 >= _seqbuflen) seqbuf_dump();#ifdef RUNNING_ST  if(LastStatus[0]!=ev)	{#endif	  seq_midiout (ev);#ifdef RUNNING_ST	  LastStatus[0]=ev;	}#endif  seq_midiout (para1);}void Sequencer_SMF0::midiEvent2(u_char port, u_char ev, u_char para1, u_char para2){  if(port!=LastOutputPort)	{	  changePort(0, port);	  LastOutputPort = port;	}  write_tick();  if (_seqbufptr+3 >= _seqbuflen) seqbuf_dump();  #ifdef RUNNING_ST  if(LastStatus[0]!=ev)	{#endif	  seq_midiout (ev);#ifdef RUNNING_ST	  LastStatus[0]=ev;	}#endif  seq_midiout (para1);  seq_midiout (para2);}#ifdef RUNNING_STvoid Sequencer_SMF0::clearStatus(u_char){  LastStatus[0]=0;}#endifvoid Sequencer_SMF0::changePort(u_char, u_char port){  // 00 FF 21 01 port $B$r=q$/(B  write_tick();  seq_midiout (0xff);  seq_midiout (0x21);  seq_midiout (0x01);  seq_midiout (port);#ifdef SHOW_PORTCHANGE  cout << "change port:" << port+1 << endl;#endif#ifdef RUNNING_ST  clearStatus(0);#endif}void Sequencer_SMF0::send_exclusive(u_char port, u_char *ex, int len){  if (_seqbufptr+len >= _seqbuflen) seqbuf_dump();  if(port!=LastOutputPort)	{	  changePort(0, port);	  LastOutputPort = port;	}#ifdef RUNNING_ST  clearStatus(0);#endif  write_tick();  seq_midiout (ex[0]);  write_varlen (len-1);  for (int i=1;i<len;i++)	seq_midiout (ex[i]);}void Sequencer_SMF0::seq_midiout (u_char ch){  if (_seqbufptr>=_seqbuflen)	seqbuf_dump();  _seqbuf[_seqbufptr++]=ch;}void Sequencer_SMF0::write_tick(){  write_varlen (Tick-LastTick);  LastTick=Tick;}void Sequencer_SMF0::set_tempo (u_long val){  tempo = val;  if (seqstart==TRUE)	{	  tempoflag=TRUE;	  write_tick();	  seq_midiout (0xff);	  seq_midiout (0x51);	  seq_midiout (0x3 );	  write_conlen (tempo, 3);	}}void Sequencer_SMF0::AllNoteOff(u_char ){}void Sequencer_SMF0::write_string(char *str){  int len;  len = strlen(str);  if (_seqbufptr+len>=_seqbuflen)	seqbuf_dump();  for (int i=0;i<len;i++)	_seqbuf[_seqbufptr++]=str[i];}void Sequencer_SMF0::write_varlen(long value)  /* $B2DJQD9CM=q$-9~$_(B */ {  long buffer;  if (value<0) value = 0; // error  if (_seqbufptr+4>=_seqbuflen)	seqbuf_dump();  buffer = value & 0x7F;  while ((value>>=7)>0)	{	  buffer <<=8;	  buffer |= 0x80;	  buffer += (value & 0x7F);	}  while (1)	{	  _seqbuf[_seqbufptr++] = buffer; // fputc (buffer, fp);	  if (buffer&0x80)		buffer >>= 8;	  else		break;	}}void Sequencer_SMF0::write_conlen(long value, unsigned char len)     /* $B8GDjD9CM=q$-9~$_(B */{  int i;  u_int c;  if (_seqbufptr+len>=_seqbuflen)	seqbuf_dump();  for(i=len-1;i>=0;i--){	c = ((u_long)value>>(8*i))&0xff;	_seqbuf[_seqbufptr++]=c;  // fputc(c, fp);;  }}void Sequencer_SMF0::metaEvent(u_char type, int len, char *data){  if (_seqbufptr+len >= _seqbuflen) seqbuf_dump();  write_tick();  seq_midiout (0xFF);  seq_midiout (type);  write_varlen (len);  for (int i=0;i<len;i++)	seq_midiout (data[i]);}

⌨️ 快捷键说明

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