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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
/*
* seqmread.c
*
* Convert a MIDI file to a seq.
*/
/* Copyright 1989 Carnegie Mellon University */
/*****************************************************************************
* Change Log
* Date | who : Change
*-----------+-----------------------------------------------------------------
* 17-Feb-92 | GWL : only one stdio.h
* : fix to satisfy compiler:
void returns, time_type giotime(), int filegetc()
*****************************************************************************/
#include "switches.h"
#include "stdio.h"
#include "cext.h"
#include "cmdline.h"
#include "midifns.h" /* to get time_type */
#include "timebase.h"
#include "moxc.h" /* to get debug declared */
#include "seq.h"
#include "seqread.h" /* to get scale */
#include "seqmread.h"
#include "userio.h"
#include "ctype.h"
#include "midifile.h"
#include "tempomap.h"
int filegetc();
void initfuncs();
void prtime();
void snding_free();
typedef struct snding_struct {
struct snding_struct *next;
event_type event_ptr;
int pitch;
int channel;
} snding_node, *snding_type;
#define snding_alloc() (snding_type) memget(sizeof(snding_node))
#define snding_free(s) memfree(s, sizeof(snding_node))
snding_type snding_list = NULL;
tempomap_type the_tempomap;
event_type initial_clock; /* remember the first clock event */
long prev_ticksize; /* remember the previous ticksize */
int sysex_id = 0;
void smf_noteoff();
void smf_error();
void smf_header();
void smf_trackstart();
void smf_trackend();
void smf_noteon();
void smf_pressure();
void smf_parameter();
void smf_pitchbend();
void smf_program();
void smf_chanpressure();
void smf_sysex();
void smf_metamisc();
void smf_metaseq();
void smf_metaeot();
void smf_timesig();
void smf_smpte();
void smf_tempo();
void smf_keysig();
void smf_metaspecial();
void smf_metatext();
void smf_arbitrary();
private seq_type the_score;
static FILE *F;
int filegetc()
{
/* int temp = getc(F);
printf(" %x ", temp);*/
return(int)(getc(F));
}
void seq_read_smf(seq, fp)
seq_type seq;
FILE *fp;
{
F = fp;
initfuncs();
sysex_id = 0; /* sysex in seq has to correspond to a symbol */
the_score = seq; /* current sequence is a global within this module */
if (!seq) return;
the_tempomap = tempomap_create();
/* insert an initial clock to correspond to the default midifile tempo
(tempomap_create creates a corresponding initial entry in the tempomap)
(see smf_tempo for explanation of the scale() call)
*/
initial_clock = insert_clock(the_score, 0L, 0, 500L << 16);
/* scale(24 * 500000, 1 << 16, 24000) */
if (!the_tempomap) return;
Mf_getc = filegetc;
midifile();
/* fmac_close(F); -- do not close the file because the caller might try to
* close it (in fact XLISP insists on closing it as a side effect of
* garbage collection.
*/
gprintf(TRANS, "\nLoaded Midi file with %ld note(s), %ld ctrl(s).\n\n",
seq_notecount(seq), seq_ctrlcount(seq));
seq_reset(seq);
while (snding_list) {
snding_type snding = snding_list;
snding_list = snding_list->next;
gprintf(TRANS, "Note-on (key %d, chan %d) has no matching noteoff\n",
snding->pitch, snding->channel + 1);
snding_free(snding);
}
tempomap_free(the_tempomap);
}
/* gio_time -- get the time in millisec for Adagio */
/*
* Since Adagio times are (in their precise form) 1/256 ms, we want
* a similar time for midifiles, whose natural unit would be microseconds.
* We'll shift the microsecond time by 2 to get 1/250 ms = 4 us units
* and convert using the scale function when necessary.
* Real time is the time of the last tempo change (last_tempo_time)
* which is in 4us units + elapsed time.
* Elapsed time is the elapsed beats times the beat duration.
* Elapsed beats is Mf_currtime - last_tempo_beat.
* Beat duration is the specified tempo / division, where specified tempo
* is in microseconds, and division is parts per quarternote.
*/
unsigned long divisions = 24L;
time_type gio_time()
{
return (tempomap_lookup(the_tempomap, Mf_currtime) + 125L) / 250L;
}
void smf_header(format,ntrks,division)
{
/* gprintf(TRANS, "Header format=%d ntrks=%d division=%d\n",
format,ntrks,division); */
if (format > 1) gprintf(TRANS,
"Warning: format %d midi file may not work.\n",
format);
divisions = division;
/* adjust the initial tempochange */
the_tempomap->entries->tempo = 500000L / division;
}
void smf_trackstart()
{
/* gprintf(TRANS, "Track start\n"); */
}
void smf_trackend()
{
/* gprintf(TRANS, "Track end\n"); */
}
void smf_noteon(chan,pitch,vol)
{
snding_type snding;
if (vol == 0) { /* convert to a noteoff */
smf_noteoff(chan, pitch, 0);
return;
}
/* prtime();
gprintf(TRANS, "Note on, chan=%d pitch=%d vol=%d\n",chan+1,pitch,vol);
*/
/* get ready to remember the sounding note */
snding = snding_alloc();
snding->next = snding_list;
snding_list = snding;
/* enter an event into score and remember it */
snding->event_ptr = insert_note(the_score, gio_time(), 0,
chan + 1, pitch, 0L, vol);
snding->pitch = pitch;
snding->channel = chan;
}
void smf_noteoff(chan,pitch,vol)
{
snding_type *snding_ptr;
register snding_type snding;
/* prtime();
gprintf(TRANS, "Note off, chan=%d pitch=%d vol=%d\n",chan+1,pitch,vol);
*/ /* search for the snding record */
for (snding_ptr = &snding_list;
(snding = *snding_ptr) &&
((snding->pitch != pitch) || (snding->channel != chan));
snding_ptr = &(snding->next)) /* printf("* search *\n") */;
if (!snding) {
gprintf(TRANS, "Note off %d, channel %d ignored: no note on\n",
pitch, chan + 1);
} else {
event_type event = snding->event_ptr;
event->u.note.ndur += (gio_time() - event->ntime) << 8;
/* free the snding record */
*snding_ptr = snding->next;
snding_free(snding);
}
}
void smf_pressure(chan,pitch,press)
{
prtime();
gprintf(TRANS, "Pressure, chan=%d pitch=%d press=%d (IGNORED)\n",
chan + 1, pitch, press);
}
void smf_parameter(chan,control,value)
{
int ctrl = 0;
/* prtime();
gprintf(TRANS, "Parameter, chan=%d c1=%d c2=%d\n",chan+1,control,value);
*/ /* see if the control is one of the standard Adagio controls that
can be encoded in a special way. If not, ctrl remains at zero.
*/
switch (control) {
case PORTASWITCH: ctrl = PSWITCH_CTRL; break;
case MODWHEEL: ctrl = MODWHEEL_CTRL; break;
case VOLUME: ctrl = VOLUME_CTRL; break;
}
if (ctrl) /* then do special ctrl insert and save storage */
insert_ctrl(the_score, gio_time(), 0, ctrl, chan + 1, value);
else insert_macctrl(the_score, gio_time(), 0, control, chan + 1, value);
}
/* smf_pitchbend -- handle a pitch bend event */
/*
* NOTE: the midifile code from Tim Thompson has the msb and lsb bytes swapped.
* Thus the parameter msb is really the low order byte and lsb is high order.
*/
void smf_pitchbend(chan,msb,lsb)
{
/* prtime();
gprintf(TRANS, "Pitchbend, chan=%d msb=%d lsb=%d\n",chan+1,msb,lsb); */
insert_ctrl(the_score, gio_time(), 0, BEND_CTRL, chan + 1,
((lsb << 7) + msb) >> 6);
}
void smf_program(chan,program)
{
/* prtime();
gprintf(TRANS, "Program, chan=%d program=%d\n",chan+1,program); */
insert_ctrl(the_score, gio_time(), 0, PROGRAM_CTRL, chan + 1, program);
}
void smf_chanpressure(chan,press)
{
/* prtime();
gprintf(TRANS, "Channel pressure, chan=%d pressure=%d\n",chan+1,press);
*/
insert_ctrl(the_score, gio_time(), 0, TOUCH_CTRL, chan + 1, press);
}
void smf_sysex(leng,mess)
int leng;
char *mess;
{
char symb[10];
def_type defn;
int i;
sprintf(symb, "X%d", sysex_id++);
if (leng > 255) {
gprintf(TRANS, "sysex too long (%d bytes), ignored\n", leng - 2);
return;
}
/* need to end up with a prefix of [0][length], so add 2 to length;
note that this will copy past the end of the message -- this is
slightly dangerous and definitely crufty:
*/
defn = insert_def(the_score, symb, (unsigned char *) mess, leng + 2);
/* now fix up the definition by inserting the prefix bytes: */
for (i = leng + 1; i > 1; i--)
defn->definition[i] = defn->definition[i - 2];
defn->definition[0] = 0;
defn->definition[1] = leng;
insert_macro(the_score, gio_time(), 0, defn, 1, 0, NULL);
/* prtime();
gprintf(TRANS, "Sysex, leng=%d (IGNORED)\n",leng); */
}
void smf_metamisc(type,leng,mess)
char *mess;
{
prtime();
gprintf(TRANS,
"Meta event, unrecognized, type=0x%02x leng=%d (IGNORED)\n",
type, leng);
}
void smf_metaspecial(type,leng,mess)
char *mess;
{
prtime();
gprintf(TRANS,
"Meta event, sequencer-specific, type=0x%02x leng=%d (IGNORED)\n",
type, leng);
}
void smf_metatext(type,leng,mess)
char *mess;
{
static char *ttype[] = {
NULL,
"Text Event", /* type=0x01 */
"Copyright Notice", /* type=0x02 */
"Sequence/Track Name",
"Instrument Name", /* ... */
"Lyric",
"Marker",
"Cue Point", /* type=0x07 */
"Unrecognized"
};
int unrecognized = (sizeof(ttype)/sizeof(char *)) - 1;
if ( type < 1 || type > unrecognized )
type = unrecognized;
}
void smf_metaseq(num)
{
prtime();
gprintf(TRANS, "Meta event, sequence number = %d (IGNORED)\n",num);
}
void smf_metaeot()
{
/* prtime();
gprintf(TRANS, "Meta event, end of track\n"); */
}
void smf_keysig(sf,mi)
{
/* prtime();
gprintf(TRANS, "Key signature, sharp/flats=%d minor=%d\n",sf,mi); */
}
/* smf_tempo -- handle a midifile tempo change */
/*
* NOTE: if divisions is positive, it gives time units per quarter, and
* tempo is microsec per division. The product is microsec per quarter.
* To convert to ticksize (parameter to insert_clock), we divide by 24*1000
* to get units of millisec and 24ths of quarter notes. insert_clock
* expects this to have a 16 bit fractional part.
*/
void smf_tempo(tempo)
long tempo;
{
time_type ctime = gio_time();
long ticksize = scale(tempo, 1024L, 375L);
/* (tempo / 24000) << 16; microsec/clock converted to ms/quarter, shifted 16*/
/* prtime();
gprintf(TRANS, "Tempo, microseconds-per-MIDI-quarter-note = %ld\n",tempo);
*/
tempomap_insert(the_tempomap, Mf_currtime, tempo / divisions);
if (ctime == 0) {
/* we already have a clock event at t=0 -> fix it */
initial_clock->u.clock.ticksize = ticksize;
} else { /* we need a new one */
/* NOTE: after the first clock, insert clock events 1/2 tick early
to make sure ticksize is set before clock_tick() wakes up and
reads it.
*/
insert_clock(the_score, ctime - (prev_ticksize >> 17), 0, ticksize);
prev_ticksize = ticksize;
}
}
void smf_timesig(nn,dd,cc,bb)
{
/* int denom = 1;
while ( dd-- > 0 )
denom *= 2;
prtime();
gprintf(TRANS,
"Time signature=%d/%d MIDI-clocks/click=%d 32nd-notes/24-MIDI-clocks=%d\n",
nn,denom,cc,bb); */
}
void smf_smpte(hr,mn,se,fr,ff)
{
prtime();
gprintf(TRANS,
"SMPTE, hour=%d minute=%d second=%d frame=%d fract-frame=%d (IGNORED)\n",
hr, mn, se, fr, ff);
}
void smf_arbitrary(leng,mess)
char *mess;
{
prtime();
gprintf(TRANS, "Arbitrary bytes, leng=%d (IGNORED)\n",leng);
}
void smf_error(msg)
char *msg;
{
gprintf(ERROR, msg);
}
void prtime()
{
gprintf(TRANS, "Time=%ld/%ld ",Mf_currtime, gio_time());
}
void initfuncs()
{
Mf_error = smf_error;
Mf_header = smf_header;
Mf_starttrack = smf_trackstart;
Mf_endtrack = smf_trackend;
Mf_on = smf_noteon;
Mf_off = smf_noteoff;
Mf_pressure = smf_pressure;
Mf_controller = smf_parameter;
Mf_pitchbend = smf_pitchbend;
Mf_program = smf_program;
Mf_chanpressure = smf_chanpressure;
Mf_sysex = smf_sysex;
Mf_metamisc = smf_metamisc;
Mf_seqnum = smf_metaseq;
Mf_eot = smf_metaeot;
Mf_timesig = smf_timesig;
Mf_smpte = smf_smpte;
Mf_tempo = smf_tempo;
Mf_keysig = smf_keysig;
Mf_sqspecific = smf_metaspecial;
Mf_text = smf_metatext;
Mf_arbitrary = smf_arbitrary;
}
|