Finally, delete wav.c, as it's no longer needed. It has served us well
[ahxm.git] / midiin.c
blob290d1a7b0ef8e07e4304b40d7b0b92508f4f3e60
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 "annhell.h"
39 /*******************
40 Data
41 ********************/
43 #define MIDI_NOTE_OFF 0x80
44 #define MIDI_NOTE_ON 0x90
45 #define MIDI_AFTERTOUCH 0xA0
46 #define MIDI_PARAMETER 0xB0
47 #define MIDI_PROGRAM 0xC0
48 #define MIDI_PRESSURE 0xD0
49 #define MIDI_PITCHWHEEL 0xE0
50 #define MIDI_SYSEX 0xF0
51 #define MIDI_EOX 0xf7
53 /* MIDI device */
54 char midi_dev[150]="/dev/midi";
55 int midi_fd=-1;
57 /* instrument */
58 char instrument[150]="samples/acpiano.pat";
60 /* the MIDI message */
61 unsigned char midimsg[128];
62 int i_msg=0;
64 /* use the instruments in ss_ins */
65 extern struct ss_ins song_ins[256];
67 /*******************
68 Code
69 ********************/
71 void print_note(int note)
73 static int octave = -1;
74 int o, d, n;
75 char * notestr[] = {
76 "c", "c#", "d", "d#", "e", "f",
77 "f#", "g", "g#", "a", "a#", "b"
80 o=note / 12;
81 n=note % 12;
83 if(octave == -1 || (d=(octave - o)) > 1 || d < -1)
85 /* first time or octave too far; set it */
86 octave=o;
87 printf("o%d ", o);
88 d=0;
91 printf("%s%s ", notestr[n], (d == -1 ? "'" : (d == 1 ? "," : "")));
93 fflush(stdout);
97 void process_MIDI(void)
99 /* strip MIDI channel */
100 midimsg[0] &= 0xf0;
102 if(midimsg[0] == MIDI_NOTE_OFF)
103 ss_ins_note_off(&song_ins[0], midimsg[1]);
104 else
105 if(midimsg[0] == MIDI_NOTE_ON)
107 ss_ins_note_on(&song_ins[0], midimsg[1],
108 ((float)midimsg[2]) / 127.0, midimsg[1]);
110 print_note(midimsg[1]);
113 i_msg=0;
117 void midiin(void)
119 unsigned char midibuf[128];
120 int c, n;
121 float sample[2];
123 for(;;)
125 /* read from MIDI device */
126 c=read(midi_fd, midibuf, sizeof(midibuf));
128 if(c > 0)
130 for(n=0;n < c;n++)
132 /* if 7bit of input is set,
133 it's a start of MIDI message */
134 if((midibuf[n] & 0x80) && i_msg)
135 process_MIDI();
137 midimsg[i_msg++]=midibuf[n];
141 ss_output_init_frame(sample);
142 ss_ins_frame(&song_ins[0], sample);
143 ss_output_write(sample);
148 int main(int argc, char * argv[])
150 char * ext;
152 ss_interpolation=1;
153 ss_frequency=44100;
154 ss_master_volume=1;
155 ss_nchannels=2;
157 add_to_library_path("~/annhell", 0);
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(instrument, argv[1]);
169 if(argc > 2)
170 strcpy(midi_dev, argv[2]);
172 /* opens the ahxm output driver */
173 if(ss_output_open("default", NULL) < 0)
175 printf("Error opening ahxm output driver\n");
176 return(4);
179 /* loads the instrument or bangs */
180 ss_ins_init(&song_ins[0]);
182 if((ext=strchr(instrument, '.')) == NULL)
184 printf("The instrument '%s' doesn't have an extension\n", instrument);
185 return(2);
188 if(strcmp(ext, ".pat") == 0 || strcmp(ext, ".PAT") == 0)
190 if(ss_load_pat_file(&song_ins[0], instrument) < 0)
192 printf("Error loading '%s'\n", instrument);
193 return(2);
196 else
198 if(compile_file(instrument))
199 return(2);
201 ss_song_render(0);
204 /* opens the MIDI channel */
205 if((midi_fd=open(midi_dev, O_RDONLY|O_NONBLOCK))==-1)
207 printf("Error opening midi device '%s'\n", midi_dev);
208 return(3);
211 midiin();
213 ss_output_close();
215 return(0);