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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/************************************************************************
io_ncurses.c -- shows midi events using ncurses or printf
Copyright (C) 1994-1996 Nathan I. Laredo
This program is modifiable/redistributable under the terms
of the GNU General Public Licence.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Send your comments and all your spare pocket change to
laredo@gnu.ai.mit.edu (Nathan Laredo) or to PSC 1, BOX 709, 2401
Kelly Drive, Lackland AFB, TX 78236-5128, USA.
*************************************************************************/
#include "playmidi.h"
#include <ncurses.h>
#include "gsvoices.h"
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
char *metatype[7] =
{"Text", "Copyright Notice", "Sequence/Track name",
"Instrument Name", "Lyric", "Marker", "Cue Point"};
char *sharps[12] = /* for a sharp key */
{"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
char *flats[12] = /* for a flat key */
{"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"};
char *majflat[15] = /* name of major key with 'x' flats */
{"C", "F", "Bb", "Eb", "Ab", "Db", "Gb", "Cb", "Fb", "Bbb", "Ebb",
"Abb", "Gbb", "Cbb", "Fbb"}; /* only first 8 defined by file format */
char *majsharp[15] = /* name of major key with 'x' sharps */
{"C", "G", "D", "A", "E", "B", "F#", "C#", "G#", "D#", "A#",
"E#", "B#", "F##", "C##"}; /* only first 8 defined by file format */
char *minflat[15] = /* name of minor key with 'x' flats */
{"A", "D", "G", "C", "F", "Bb", "Eb", "Ab", "Db", "Gb", "Cb",
"Fb", "Bbb", "Ebb", "Abb"}; /* only first 8 defined by file format */
char *minsharp[15] = /* name of minor key with 'x' sharps */
{"A", "E", "B", "F#", "C#", "G#", "D#", "A#", "E#", "B#", "F##",
"C##", "G##", "D##", "A##"}; /* only first 8 defined by file format */
char *drumset[11] =
{
"STANDARD ", "ROOM ", "POWER ", "ELECTRONIC ",
"TR-808/909 ", "JAZZ ", "BRUSH ", "ORCHESTRA ",
"SFX ", "User Program", "CM-64/32L "
};
#define SET(x) (x == 8 ? 1 : x >= 16 && x <= 23 ? 2 : x == 24 ? 3 : \
x == 25 ? 4 : x == 32 ? 5 : x >= 40 && x <= 47 ? 6 : \
x == 48 ? 7 : x == 56 ? 8 : x >= 96 && x <= 111 ? 9 : \
x == 127 ? 10 : 0)
extern int graphics, verbose, perc;
extern int format, ntrks, division;
extern unsigned long int ticks;
extern char *filename, *gmvoice[256];
extern float skew;
extern void seq_reset();
extern struct timeval start_time;
struct timeval now_time, want_time;
char textbuf[1024], **nn;
int i, ytxt, karaoke;
void close_show(error,file)
int error;
const char *file;
{
int err = errno;
if (graphics) {
attrset(A_NORMAL);
refresh();
endwin();
}
if (err)
{
if (file)
fprintf (stderr, "%s: %s\n", file, strerror (err));
else
fprintf (stderr, "%s\n", strerror (err));
}
if (error)
exit(error);
}
#define CHN (cmd & 0xf)
#define NOTE ((int)data[0])
#define VEL ((int)data[1])
#define OCTAVE (NOTE / 12)
#define OCHAR (0x30 + OCTAVE)
#define XPOS (14 + ((NOTE/2) % ((COLS - 16) / 4)) * 4)
#define YPOS (CHN + 2)
#define NNAME nn[NOTE % 12]
int cdeltat(t1, t2)
struct timeval *t1;
struct timeval *t2;
{
int d1, d2;
d1 = t1->tv_sec - t2->tv_sec;
if((d2 = t1->tv_usec - t2->tv_usec) < 0)
(d2 += 1000000, d1 -= 1);
d2 /= 10000;
return (d2 + d1 * 100);
}
int updatestatus()
{
int ch, d1, d2;
want_time.tv_sec = start_time.tv_sec + (ticks / 100);
want_time.tv_usec = start_time.tv_usec + (ticks % 100) * 10000;
if (want_time.tv_usec > 1000000)
(want_time.tv_usec -= 1000000, want_time.tv_sec++);
do {
attrset(A_BOLD);
if ((ch = getch()) != ERR)
switch (ch) {
case KEY_RIGHT:
if ((skew -= 0.01) < 0.25)
skew = 0.25;
if (graphics)
mvprintw(1, COLS - 6, "%0.2f", skew);
break;
case KEY_LEFT:
if ((skew += 0.01) > 4)
skew = 4.0;
if (graphics)
mvprintw(1, COLS - 6, "%0.2f", skew);
break;
case KEY_PPAGE:
case KEY_UP:
seq_reset();
return (ch == KEY_UP ? 0 : -1);
break;
case 18:
case 12:
wrefresh(curscr);
break;
case 'q':
case 'Q':
case 3:
close_show(0,0);
exit (0);
default:
fprintf (stderr, "%d\n", ch);
return 1; /* skip to next song */
break;
}
gettimeofday(&now_time, NULL);
d1 = now_time.tv_sec - start_time.tv_sec;
d2 = now_time.tv_usec - start_time.tv_usec;
if (d2 < 0)
(d2 += 1000000, d1 -= 1);
mvprintw(1, 0, "%02d:%02d.%d", d1 / 60, d1 % 60, d2 / 100000);
refresh();
d1 = cdeltat(&want_time, &now_time);
if (d1 > 15)
usleep(100000);
} while (d1 > 10);
return NO_EXIT;
}
void showevent(cmd, data, length)
int cmd;
unsigned char *data;
int length;
{
if (cmd < 8 && cmd > 0) {
if (length > COLS)
length = COLS;
if (length >= sizeof (textbuf))
length = sizeof (textbuf) - 1;
if ((cmd == 1 || cmd == 2) && strncmp(textbuf, (const char *)data, length - 1) == 0)
return; /* ignore repeat messages, "WinJammer Demo" etc. */
if (verbose) {
printf("%s: ", metatype[cmd - 1]);
for (i = 0; i < length; i++)
putchar(data[i]);
putchar('\n');
} else {
attrset(A_BOLD | COLOR_PAIR(cmd));
if (!karaoke || *data == '\\' || *data == '/' ||
(*data >= '@' && *data <= 'Z') || *data == '(' ||
karaoke + length > COLS || (cmd != 1 && karaoke)) {
karaoke = 0;
if ((++ytxt) > LINES - 1)
ytxt = 19;
move(ytxt, 0);
clrtoeol();
if (*data == '\\' || *data == '/')
(data++, length--); /* karaoke newlines */
if (*data == '@') /* karaoke info */
(data += 2, length -= 2);
}
strncpy(textbuf, (const char *)data, length < COLS - karaoke ? length :
COLS - karaoke);
textbuf[length] = 0;
mvaddstr(ytxt, karaoke, textbuf);
if (cmd == 1) {
karaoke += strlen(textbuf);
if (karaoke > COLS - 10)
karaoke = 0;
} else
karaoke = 0;
}
} else if (cmd == key_signature) {
if (graphics || verbose)
nn = ((NOTE & 0x80) ? flats : sharps);
if (verbose) {
if (VEL) /* major key */
printf("Key: %s major\n", (!(NOTE & 0x80) ?
majsharp[NOTE] : majflat[256-NOTE]));
else /* minor key */
printf("Key: %s minor\n", (!(NOTE & 0x80) ?
minsharp[NOTE] : minflat[256-NOTE]));
}
} else
switch (cmd & 0xf0) {
case MIDI_KEY_PRESSURE:
if (verbose > 4)
printf("Chn %d Key Pressure %s%c=%d\n",
1 + (cmd & 0xf), NNAME, OCHAR, VEL);
break;
case MIDI_NOTEON:
if (graphics)
if (VEL) {
attrset(A_BOLD | COLOR_PAIR((CHN % 6 + 1)));
if (!ISPERC(CHN) || NOTE > 127 ||
gmvoice[NOTE + 128] == NULL)
mvprintw(YPOS, XPOS, "%s%c", NNAME, OCHAR);
else
mvprintw(YPOS, XPOS, "%c%c%c",
gmvoice[NOTE + 128][0],
gmvoice[NOTE + 128][1],
gmvoice[NOTE + 128][2]);
} else
mvaddstr(YPOS, XPOS, " ");
else if (verbose > 5)
printf("Chn %d Note On %s%c=%d\n",
1 + (cmd & 0xf), NNAME, OCHAR, VEL);
break;
case MIDI_NOTEOFF:
if (graphics)
mvaddstr(YPOS, XPOS, " ");
else if (verbose > 5)
printf("Chn %d Note Off %s%c=%d\n",
1 + (cmd & 0xf), NNAME, OCHAR, VEL);
break;
case MIDI_CTL_CHANGE:
if (verbose > 5)
printf("Chn %d Ctl Change %d=%d\n",
1 + (cmd & 0xf), NOTE, VEL);
break;
case MIDI_CHN_PRESSURE:
if (verbose > 5)
printf("Chn %d Pressure=%d\n",
1 + (cmd & 0xf), NOTE);
break;
case MIDI_PITCH_BEND:
{
register int val = (VEL << 7) | NOTE;
if (graphics) {
attrset(A_BOLD);
if (val > 0x2000)
mvaddch(YPOS, 11, '>');
else if (val < 0x2000)
mvaddch(YPOS, 11, '<');
else
mvaddch(YPOS, 11, ' ');
} else if (verbose > 4)
printf("Chn %d Bender=0x%04x\n",
1 + CHN, val);
}
break;
case MIDI_PGM_CHANGE:
if (graphics) {
attrset(COLOR_PAIR((CHN % 6 + 1)) | A_BOLD);
if (!ISPERC(CHN))
mvaddnstr(YPOS, 0, gsvoice[NOTE], 12);
else
mvaddstr(YPOS, 0, drumset[SET(NOTE)]);
} else if (verbose > 3)
printf("Chn %d Program=%s %d\n",
1 + CHN, (ISPERC(CHN) ? drumset[SET(NOTE)]
: gsvoice[NOTE]), NOTE + 1);
break;
case 0xf0:
case 0xf7:
if (verbose > 2) {
printf("Sysex(%2x): ", cmd);
for (i = 0; i < length; i++)
printf("%02x", data[i]);
putchar('\n');
}
break;
default:
break;
}
}
static void init_show_text (void)
{
char *tmp;
attrset(A_NORMAL);
mvprintw(0, 0, RELEASE " by Nathan Laredo");
mvprintw(0, 40, "Now Playing:");
mvprintw(1, 40, "[P]ause [N]ext [L]ast [O]ptions");
mvvline(2, 12, ACS_VLINE, 16); /*ytxt - 2);*/
mvhline(18 /*ytxt*/, 0, ACS_HLINE, 12);
mvaddch(18 /*ytxt*/, 12, ACS_LRCORNER);
mvprintw(1, 0, "00:00.0 - 00:00.0, %d track%c", ntrks,
ntrks > 1 ? 's' : ' ');
tmp = strrchr(filename, '/');
strncpy(textbuf, (tmp == NULL ? filename : tmp + 1), COLS - 53);
if (strlen(textbuf) > COLS - 53)
textbuf[COLS - 53] = '\0';
attrset(A_BOLD);
mvaddstr(0, 53, textbuf);
mvaddch(1, 41, 'P');
mvaddch(1, 49, 'N');
mvaddch(1, 56, 'L');
mvaddch(1, 63, 'O');
mvaddstr(0, 0, RELEASE);
}
void init_show()
{
nn = flats;
ytxt = 18;
karaoke = 0;
if (graphics) {
clear();
init_show_text ();
attrset(A_NORMAL);
for (i = 0; i < 16; i++)
mvprintw(i + 2, 0, "Channel %2d", i + 1);
refresh();
} else if (verbose) {
printf("** Now Playing \"%s\"\n", filename);
printf("** Format: %d, Tracks: %d, Division: %d\n", format,
ntrks, division);
}
}
static void resize_signal_handler (int sig)
{
struct winsize size;
if (!ioctl (fileno (stdout), TIOCGWINSZ, &size))
{
int y;
resize_term (size.ws_row, size.ws_col);
if (size.ws_col > 14)
for (y = 2; y < 18; ++y)
{
move (y, 14);
clrtoeol ();
}
init_show_text ();
wrefresh (curscr);
}
signal (SIGWINCH, resize_signal_handler);
}
void setup_show(argc, argv)
int argc;
char **argv;
{
if (graphics) {
signal (SIGWINCH, resize_signal_handler);
initscr();
start_color();
verbose = 0;
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
raw();
noecho();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
attrset(A_NORMAL);
}
}
|