Tweaked CFLAGS detection in config.sh.
[ahxm.git] / midiin.c
blob9cd5c943b407b8cdbfd1a9d96016d89b6363b12b
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2008 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 "ahxm.h"
40 /** data **/
42 #define MIDI_NOTE_OFF 0x80
43 #define MIDI_NOTE_ON 0x90
44 #define MIDI_AFTERTOUCH 0xA0
45 #define MIDI_PARAMETER 0xB0
46 #define MIDI_PROGRAM 0xC0
47 #define MIDI_PRESSURE 0xD0
48 #define MIDI_PITCHWHEEL 0xE0
49 #define MIDI_SYSEX 0xF0
50 #define MIDI_EOX 0xf7
52 /* MIDI device */
53 char midi_dev[150] = "/dev/midi";
54 int midi_fd = -1;
56 /* instrument */
57 char instrument[150] = "samples/acpiano.pat";
59 /* the MIDI message */
60 unsigned char midimsg[128];
61 int i_msg = 0;
63 /* use the instruments in ss_ins */
64 extern struct ss_ins ss_song_ins[256];
67 /** code **/
69 void print_note(int note)
71 static int octave = -1;
72 int o, d, n;
73 char *notestr[] = {
74 "c", "c#", "d", "d#", "e", "f",
75 "f#", "g", "g#", "a", "a#", "b"
78 o = note / 12;
79 n = note % 12;
81 if (octave == -1 || (d = (octave - o)) > 1 || d < -1) {
82 /* first time or octave too far; set it */
83 octave = o;
84 printf("o%d ", o);
85 d = 0;
88 printf("%s%s ", notestr[n], (d == -1 ? "'" : (d == 1 ? "," : "")));
90 fflush(stdout);
94 void process_MIDI(void)
96 /* strip MIDI channel */
97 midimsg[0] &= 0xf0;
99 if (midimsg[0] == MIDI_NOTE_OFF)
100 ss_ins_note_off(&ss_song_ins[0], midimsg[1]);
101 else
102 if (midimsg[0] == MIDI_NOTE_ON) {
103 ss_ins_note_on(&ss_song_ins[0], midimsg[1],
104 ((sample_t) midimsg[2]) / 127.0, midimsg[1]);
106 print_note(midimsg[1]);
109 i_msg = 0;
113 void midiin(void)
115 unsigned char midibuf[128];
116 int c, n;
117 sample_t sample[2];
119 for (;;) {
120 /* read from MIDI device */
121 c = read(midi_fd, midibuf, sizeof(midibuf));
123 if (c > 0) {
124 for (n = 0; n < c; n++) {
125 /* if 7bit of input is set,
126 it's a start of MIDI message */
127 if ((midibuf[n] & 0x80) && i_msg)
128 process_MIDI();
130 midimsg[i_msg++] = midibuf[n];
134 ss_output_init_frame(sample);
135 ss_ins_frame(&ss_song_ins[0], sample);
136 ss_output_write(sample);
141 int main(int argc, char *argv[])
143 char *ext;
145 ss_interpolation = 1;
146 ss_frequency = 44100;
147 ss_master_volume = 1;
148 ss_nchannels = 2;
150 libpath_add("~/ahxmlib", 0);
152 /* default converters */
153 transconv_add(".flac", ".wav", "flac -d -s -o '%s' '%s'");
154 transconv_add(".mp3", ".wav", "mpg321 -q -w '%s' '%s'");
156 if (argc == 0) {
157 printf("Usage:\n");
158 printf("midiin [instrument.pat] [midi device]\n");
159 return 1;
162 if (argc > 1)
163 strcpy(instrument, argv[1]);
165 if (argc > 2)
166 strcpy(midi_dev, argv[2]);
168 ss_gen_init();
170 /* loads the instrument or bangs */
171 ss_ins_init(&ss_song_ins[0]);
173 if ((ext = strchr(instrument, '.')) == NULL) {
174 printf("The instrument '%s' doesn't have an extension\n",
175 instrument);
176 return 2;
179 if (strcmp(ext, ".pat") == 0 || strcmp(ext, ".PAT") == 0) {
180 if (ss_load_pat_file(&ss_song_ins[0], instrument) < 0) {
181 printf("Error loading '%s'\n", instrument);
182 return 2;
185 else {
186 if (compile_ahs(instrument))
187 return 2;
189 if (ss_song_render(0, "default", NULL))
190 return 4;
193 /* opens the MIDI channel */
194 if ((midi_fd = open(midi_dev, O_RDONLY | O_NONBLOCK)) == -1) {
195 printf("Error opening midi device '%s'\n", midi_dev);
196 return 3;
199 midiin();
201 ss_output_close();
203 return 0;