📄 seq_oss_sio.c
字号:
/* * seq_oss_sio.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.ho>#include <stdlib.h>#include <string.h>#include <sys/ioctl.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <termios.h>#include "defines.h"#include "config.h"#include "seq_oss_sio.h"#include "extern.h"#ifndef NO_OSS#include SOUNDCARD_HEADER#else#include "midievdefs.h"#endifint Seq_OSS_Serial::open(char *seqdev, int buflen){ if(flagOpen == TRUE) return -1; // device open checkMidiDevice(); char middevname[20] = MIDIDEVICE; int len = strlen(middevname); int num; for (num=0;num<numberOfDevice-1;num++) { middevname[len] = num+'0'; middevname[len+1] = '\0'; if((midifd[num] = ::open(middevname, O_WRONLY | O_NDELAY))<0) { printf("cannot open device %s", middevname); //cerr << "cannot open device "; perror(middevname); return -2; } } if((midifd[num] = ::open(seqdev, O_RDWR | O_NDELAY))<0) { perror("cannot open device"); return -2; } struct termios tio; seqfd = midifd[num]; if (tcgetattr (seqfd, &tio) < 0) { return -1; } tio_orig = tio; tio.c_iflag = 0; tio.c_oflag = 0; tio.c_cflag = CS8 | CREAD | CLOCAL; tio.c_lflag = 0; cfsetispeed(&tio, B38400); cfsetospeed(&tio, B38400); if (tcsetattr (seqfd, TCSANOW, &tio) < 0) { perror(" "); return -3; } // buffer _buf = new u_char *[numberOfDevice]; for (num=0;num<numberOfDevice;num++) _buf[num] = new u_char [buflen]; _seqbuflen = buflen; _bufptr = new int [numberOfDevice]; if(!_buf || !_bufptr) { printf("allocation error\n"); // cerr << "allocation error" << endl; return -3; } for (num=0;num<numberOfDevice;num++) { _bufptr[num] = 0; } flagOpen = TRUE; u_char port=0; for (num=0;num<16;num++) { if (port2dev[num]==numberOfDevice-1) { SIOPORT[num]=port; port++; } } return 0;}int Seq_OSS_Serial::close(){ if(flagOpen != TRUE) return -1; stopTimer(); flagOpen = FALSE; if (tcsetattr (seqfd, TCSANOW, &tio_orig) < 0) perror(" "); for (int num=0;num<numberOfDevice;num++) { ::close (midifd[num]); } delete [] _buf; delete [] _bufptr; return 0;}int Seq_OSS_Serial::checkMidiDevice(){#ifndef NO_OSS int nrmidis, fd; static int flag=0; if(flag==0) { if ((fd = ::open(SEQUENCER, O_RDWR | O_NDELAY))<0) { printf("can't open %s\n", SEQUENCER); // cerr << "can't open " << SEQUENCER << endl; return -1; } if (ioctl(fd, SNDCTL_SEQ_NRMIDIS, &nrmidis) == -1) { printf("can't get info about midi ports\n"); // cerr << "can't get info about midi ports\n"; return -1; } ::close (fd);# ifdef SHOW_INFO cout << "detect " << nrmidis << " midis." << endl;# endif numberOfDevice = nrmidis+1; flag=1; }#endif return numberOfDevice;}void Seq_OSS_Serial::reset(){ seqbuf_dump(); for (int num=0;num<numberOfDevice;num++) _bufptr[num] = 0;}void Seq_OSS_Serial::startTimer(){#ifdef RUNNING_ST for(int n=0;n<16;n++) clearStatus(n);#endif LastOutputPort = 0; gettimeofday(&starttime, NULL); ctrlrate = 1000000; LastTick = 0; CurrentTime = 0.; }void Seq_OSS_Serial::stopTimer(){ changePort (numberOfDevice-1, 0); LastOutputPort = 0; seqbuf_dump();}void Seq_OSS_Serial::MidiOut(u_char port, char ch){ u_char dev; dev = port2dev[port]; if (dev==numberOfDevice-1) // $B%7%j%"%k%G%P%$%9$X$N=PNO$@$C$?$i(B { if (port!=LastOutputPort) { changePort (dev, SIOPORT[port]); LastOutputPort = port; } } seq_midiout (dev, ch);}void Seq_OSS_Serial::midiEvent1(u_char port, u_char ev, u_char para1){ u_char dev; dev = port2dev[port]; if (dev==numberOfDevice-1) { if (port!=LastOutputPort) { changePort (dev, SIOPORT[port]); LastOutputPort = port; } }#ifdef RUNNING_ST if(LastStatus[dev]!=ev) {#endif seq_midiout (dev, ev);#ifdef RUNNING_ST LastStatus[dev]=ev; }#endif seq_midiout (dev, para1);}void Seq_OSS_Serial::midiEvent2(u_char port, u_char ev, u_char para1, u_char para2){ u_char dev; dev = port2dev[port]; if (dev==numberOfDevice-1) { if (port!=LastOutputPort) { changePort (dev, SIOPORT[port]); LastOutputPort = port; } }#ifdef RUNNING_ST if(LastStatus[dev]!=ev) {#endif seq_midiout (dev, ev);#ifdef RUNNING_ST LastStatus[dev]=ev; }#endif seq_midiout (dev, para1); seq_midiout (dev, para2);}void Seq_OSS_Serial::seq_midiout(u_char dev, u_char dat){ if (dev&0x80) return; if (_bufptr[dev]>=_seqbuflen) seqbuf_dump(); _buf[dev][_bufptr[dev]] = dat; _bufptr[dev]++;}void Seq_OSS_Serial::seqbuf_dump(){ for (int num=0;num<numberOfDevice;num++) { if(_bufptr[num]) if(write(midifd[num], _buf[num], _bufptr[num]) < 0) { perror("write error"); exit(-1); } _bufptr[num]=0; }}void Seq_OSS_Serial::send_exclusive(u_char port, u_char *ex, int len){ u_char dev; dev = port2dev[port]; if (dev==numberOfDevice-1) // $B%7%j%"%k%G%P%$%9$X$N=PNO$@$C$?$i(B { if (port!=LastOutputPort) { changePort (dev, SIOPORT[port]); LastOutputPort = port; } } for (int i=0;i<len;i++) seq_midiout (dev, ex[i]);}void Seq_OSS_Serial::seq_waittime(u_long tick){ static long waittime; seqbuf_dump(); gettimeofday (&now, NULL); waittime = (long)tick - (now.tv_usec-starttime.tv_usec) - (now.tv_sec-starttime.tv_sec)*1000000; if(waittime>999) usleep (waittime);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -