Added a new text file to document the extended commands. By now it's just
[ahxm.git] / midiin.c
blob057e11c3c551944dcf955183b7c19eaedbbd0e39
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 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 "ss_core.h"
38 #include "ss_output.h"
39 #include "ss_gen.h"
40 #include "ss_ins.h"
41 #include "ss_input.h"
43 /*******************
44 Data
45 ********************/
47 #define MIDI_NOTE_OFF 0x80
48 #define MIDI_NOTE_ON 0x90
49 #define MIDI_AFTERTOUCH 0xA0
50 #define MIDI_PARAMETER 0xB0
51 #define MIDI_PROGRAM 0xC0
52 #define MIDI_PRESSURE 0xD0
53 #define MIDI_PITCHWHEEL 0xE0
54 #define MIDI_SYSEX 0xF0
55 #define MIDI_EOX 0xf7
57 /* MIDI device */
58 char midi_dev[150]="/dev/midi";
59 int midi_fd=-1;
61 /* PAT instrument */
62 char pat_instrument[150]="samples/acpiano.pat";
64 /* the instrument */
65 struct ss_ins i;
67 /* the MIDI message */
68 unsigned char midimsg[128];
69 int i_msg=0;
71 /*******************
72 Code
73 ********************/
75 void print_note(int note)
77 static int octave = -1;
78 int o, d, n;
79 char * notestr[] = {
80 "c", "c#", "d", "d#", "e", "f",
81 "f#", "g", "g#", "a", "a#", "b"
84 o=note / 12;
85 n=note % 12;
87 if(octave == -1 || (d=abs(octave - o)) > 1)
89 /* first time or octave too far; set it */
90 octave=o;
91 printf("o%d ", o);
92 d=0;
95 printf("%s%s ", notestr[n], (d == 1 ? "'" : (d == -1 ? "," : "")));
97 fflush(stdout);
101 void process_MIDI(void)
103 /* strip MIDI channel */
104 midimsg[0] &= 0xf0;
106 if(midimsg[0] == MIDI_NOTE_OFF)
107 ss_ins_note_off(&i, midimsg[1]);
108 else
109 if(midimsg[0] == MIDI_NOTE_ON)
111 ss_ins_note_on(&i, midimsg[1],
112 ((float)midimsg[2]) / 127.0, midimsg[1]);
114 print_note(midimsg[1]);
117 i_msg=0;
121 void midiin(void)
123 unsigned char midibuf[128];
124 int c, n;
125 float sample[2];
127 for(;;)
129 /* read from MIDI device */
130 c=read(midi_fd, midibuf, sizeof(midibuf));
132 if(c > 0)
134 for(n=0;n < c;n++)
136 /* if 7bit of input is set,
137 it's a start of MIDI message */
138 if((midibuf[n] & 0x80) && i_msg)
139 process_MIDI();
141 midimsg[i_msg++]=midibuf[n];
145 ss_output_init_frame(sample);
146 ss_ins_frame(&i, sample);
147 ss_output_write(sample);
152 int main(int argc, char * argv[])
154 ss_interpolation=1;
155 ss_frequency=44100;
156 ss_master_volume=1;
157 ss_nchannels=2;
159 if(argc == 0)
161 printf("Usage:\n");
162 printf("midiin [instrument.pat] [midi device]\n");
163 return(1);
166 if(argc > 1)
167 strcpy(pat_instrument, argv[1]);
169 if(argc > 2)
170 strcpy(midi_dev, argv[2]);
172 /* loads the instrument or bangs */
173 ss_ins_init(&i);
174 if(ss_load_pat_file(&i, pat_instrument) < 0)
176 printf("Error loading '%s'\n", pat_instrument);
177 return(2);
180 /* opens the ahxm output driver */
181 if(ss_output_open("default", NULL) < 0)
183 printf("Error opening ahxm output driver\n");
184 return(4);
187 /* opens the MIDI channel */
188 if((midi_fd=open(midi_dev, O_RDONLY|O_NONBLOCK))==-1)
190 printf("Error opening midi device '%s'\n", midi_dev);
191 return(3);
194 midiin();
196 ss_output_close();
198 return(0);