summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/synti/zynaddsubfx/Input/ALSAMidiIn.C
blob: 13aa14d1c9c0655773830244887f537d1d93db9d (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
/*
  ZynAddSubFX - a software synthesizer
 
  ALSAMidiIn.C - Midi input for ALSA (this creates an ALSA virtual port)
  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 "ALSAMidiIn.h"
#include <stdlib.h>
#include <stdio.h>


ALSAMidiIn::ALSAMidiIn(){
    int alsaport;
    inputok=0;
    char portname[50];
    sprintf(portname,"ZynAddSubFX");

    midi_handle=NULL;
    
    if (snd_seq_open(&midi_handle,"default",SND_SEQ_OPEN_INPUT,0)!=0) return;
    
    snd_seq_set_client_name(midi_handle,"ZynAddSubFX");//thanks to Frank Neumann

    alsaport = snd_seq_create_simple_port(midi_handle,portname
	        ,SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE
		,SND_SEQ_PORT_TYPE_SYNTH);
    if (alsaport<0) return;

    inputok=1;
};

ALSAMidiIn::~ALSAMidiIn(){
    snd_seq_close(midi_handle);
};


/*
 * Get the midi command,channel and parameters
 */
void ALSAMidiIn::getmidicmd(MidiCmdType &cmdtype,unsigned char &cmdchan,int *cmdparams){
    snd_seq_event_t *midievent=NULL;
    cmdtype=MidiNull;

    if (inputok==0){
	return;
    };
    
    snd_seq_event_input(midi_handle,&midievent);
    
    if (midievent==NULL) return;
    switch (midievent->type){
	case SND_SEQ_EVENT_NOTEON:  
		cmdtype=MidiNoteON;
		cmdchan=midievent->data.note.channel;
		cmdparams[0]=midievent->data.note.note;
		cmdparams[1]=midievent->data.note.velocity;
		break;
	case SND_SEQ_EVENT_NOTEOFF: 
		cmdtype=MidiNoteOFF;
		cmdchan=midievent->data.note.channel;
		cmdparams[0]=midievent->data.note.note;
		break;
	case SND_SEQ_EVENT_PITCHBEND:
		cmdtype=MidiController;
		cmdchan=midievent->data.control.channel;
		cmdparams[0]=C_pitchwheel;//Pitch Bend
		cmdparams[1]=midievent->data.control.value;
		break;
	case SND_SEQ_EVENT_CONTROLLER:
		cmdtype=MidiController;
		cmdchan=midievent->data.control.channel;
		cmdparams[0]=getcontroller(midievent->data.control.param);
		cmdparams[1]=midievent->data.control.value;
    	        //fprintf(stderr,"t=%d val=%d\n",midievent->data.control.param,midievent->data.control.value);
		break;
				    
    };
};