summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/synti/zynaddsubfx/Input/OSSMidiIn.C
blob: 5a6ae5091da7bd829067c9bb16b2af3c2e240aef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
  ZynAddSubFX - a software synthesizer
 
  OSSMidiIn.C - Midi input for Open Sound System
  Copyright (C) 2002-2005 Nasca Octavian Paul
  Author: Nasca Octavian Paul

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License 
  as published by the Free Software Foundation.

  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 (version 2) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/soundcard.h>

#include "OSSMidiIn.h"
#include "../Misc/Util.h"

OSSMidiIn::OSSMidiIn(){
    inputok=0;
    midi_handle=open(config.cfg.LinuxOSSSeqInDev,O_RDONLY,0);
    if (midi_handle!=-1) inputok=1;
    
    lastmidicmd=0;
    cmdtype=0;
    cmdchan=0;

};

OSSMidiIn::~OSSMidiIn(){
    close(midi_handle);
};

unsigned char OSSMidiIn::readbyte(){
    unsigned char tmp[4];
    read(midi_handle,&tmp[0],1);
    while (tmp[0]!=SEQ_MIDIPUTC){
	read(midi_handle,&tmp[0],4);
    };
    return(tmp[1]);
};

unsigned char OSSMidiIn::getmidibyte(){
    unsigned char b;
    do {
	b=readbyte();
    } while (b==0xfe);//drops the Active Sense Messages
    return(b);
};

/*
 * Get the midi command,channel and parameters
 */
void OSSMidiIn::getmidicmd(MidiCmdType &cmdtype,unsigned char &cmdchan,int *cmdparams){
    unsigned char tmp,i;
    if (inputok==0) {
	cmdtype=MidiNull;
	return;
    };
    i=0;
    if (lastmidicmd==0){//asteapta prima data pana cand vine prima comanda midi
	while (tmp<0x80) tmp=getmidibyte();
	lastmidicmd=tmp;
	};

    tmp=getmidibyte();

    if (tmp>=0x80) {
	lastmidicmd=tmp;
	tmp=getmidibyte();
	};

    if ((lastmidicmd>=0x80)&&(lastmidicmd<=0x8f)){//Note OFF
	cmdtype=MidiNoteOFF;
	cmdchan=lastmidicmd%16;
	cmdparams[0]=tmp;//note number
    };

    if ((lastmidicmd>=0x90)&&(lastmidicmd<=0x9f)){//Note ON
	cmdtype=MidiNoteON;
	cmdchan=lastmidicmd%16;
	cmdparams[0]=tmp;//note number	
	cmdparams[1]=getmidibyte();//velocity
	if (cmdparams[1]==0) cmdtype=MidiNoteOFF;//if velocity==0 then is note off
    };
    if ((lastmidicmd>=0xB0)&&(lastmidicmd<=0xBF)){//Controllers
	cmdtype=MidiController;
	cmdchan=lastmidicmd%16;
	cmdparams[0]=getcontroller(tmp);
	cmdparams[1]=getmidibyte();
    };
    if ((lastmidicmd>=0xE0)&&(lastmidicmd<=0xEF)){//Pitch Wheel
	cmdtype=MidiController;
	cmdchan=lastmidicmd%16;
	cmdparams[0]=C_pitchwheel;
	cmdparams[1]=(tmp+getmidibyte()*(int) 128)-8192;//hope this is correct
    };
};