The compiler now uses stage 1 events.
[ahxm.git] / midiin.c
blob1d162a52a3db7cd31e799b3fe988db07191b48c6
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2004 Angel Ortega <angel@triptico.com>
6 midiin.c - Code for testing the libraries
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <math.h>
37 #include "core.h"
38 #include "output.h"
39 #include "ss_gen.h"
40 #include "effect.h"
41 #include "ss_ins.h"
42 #include "input.h"
44 /*******************
45 Data
46 ********************/
48 #define MIDI_NOTE_OFF 0x80
49 #define MIDI_NOTE_ON 0x90
50 #define MIDI_AFTERTOUCH 0xA0
51 #define MIDI_PARAMETER 0xB0
52 #define MIDI_PROGRAM 0xC0
53 #define MIDI_PRESSURE 0xD0
54 #define MIDI_PITCHWHEEL 0xE0
55 #define MIDI_SYSEX 0xF0
56 #define MIDI_EOX 0xf7
58 /* MIDI device */
59 char _midi_dev[150]="/dev/midi00";
60 int _midi_fd=-1;
62 /* PAT instrument */
63 char _pat_instrument[150]="samples/acpiano.pat";
65 /* the instrument */
66 struct ss_ins i;
68 /* the MIDI message */
69 unsigned char midimsg[128];
70 int i_msg=0;
72 /*******************
73 Code
74 ********************/
76 void process_MIDI(void)
78 /* strip MIDI channel */
79 midimsg[0] &= 0xf0;
81 if(midimsg[0] == MIDI_NOTE_OFF)
82 ss_ins_note_off(midimsg[1]);
83 else
84 if(midimsg[0] == MIDI_NOTE_ON)
85 ss_ins_note_on(&i, midimsg[1],
86 ((float)midimsg[2]) / 127.0, midimsg[1]);
88 i_msg=0;
92 void midiin(void)
94 unsigned char midibuf[128];
95 int c, n;
96 float sample[2];
98 for(;;)
100 /* read from MIDI device */
101 c=read(_midi_fd, midibuf, sizeof(midibuf));
103 if(c > 0)
105 for(n=0;n < c;n++)
107 /* if 7bit of input is set,
108 it's a start of MIDI message */
109 if((midibuf[n] & 0x80) && i_msg)
110 process_MIDI();
112 midimsg[i_msg++]=midibuf[n];
116 output_init_frame(sample);
117 ss_ins_frame(0, sample);
118 output_write(sample);
123 int main(int argc, char * argv[])
125 _interpolation=1;
126 _frequency=44100;
127 _master_volume=1;
128 _n_channels=2;
130 if(argc == 0)
132 printf("Usage:\n");
133 printf("midiin [instrument.pat] [midi device]\n");
134 return(1);
137 if(argc > 1)
138 strcpy(_pat_instrument, argv[1]);
140 if(argc > 2)
141 strcpy(_midi_dev, argv[2]);
143 /* loads the instrument or bangs */
144 ss_ins_init(&i, 0);
145 if(load_pat_file(&i, _pat_instrument) < 0)
147 printf("Error loading '%s'\n", _pat_instrument);
148 return(2);
151 /* opens the MIDI channel */
152 if((_midi_fd=open(_midi_dev, O_RDONLY|O_NONBLOCK))==-1)
154 printf("Error opening midi device '%s'\n", _midi_dev);
155 return(3);
158 /* opens the ahxm output driver */
159 if(output_open("default", NULL) < 0)
161 printf("Error opening ahxm output driver\n");
162 return(4);
165 midiin();
167 output_close();
169 return(0);