/* GETINST.C * The routine that implements the 'Get Instrument' message. * BTW, this routine is generally considered to be the standard * implementation for two-part messages. * * Copyright (C) 1991 by Andrew Arensburger. Permission is granted to * use, copy and/or distribute this file freely for non-commercial purposes, * as long as this notice remains intact. Any commercial use is prohibited * without express permission from the author. * * If you make any changes, fix bugs, etc., please send them to me that I * might coordinate fixes and improvements. */ #include #include "eps.h" /* GET_INSTRUMENT * Get instrument parameters for the instrument described in 'inst'. * The parameters are returned by filling in the fields in 'out'. * Returns 0 on no error, or a negative error code on error. */ int get_instrument(chan,inst,out) int chan; /* MIDI channel */ edit_spec *inst; /* Which instrument? */ inst_par *out; /* Description will be put here */ { int i; int waiting; /* Are we waiting for the EPS to do things? */ /* Send request for instrument parameters */ send_head(chan); /* Send SysEx head */ mpu_sbyte(GET_INST); /* Request instrument parameters */ send12(inst->inst_num); /* Instrument number */ send12(inst->layer_num);/* Layer number */ send12(inst->ws_num); /* Wavesample number */ send_tail(); /* Send SysEx tail */ if (timeout(SHORT_TIMEOUT)) return(-1); /* Short timeout occurred */ /* This loop is a bit tricky. The basic idea is that the EPS can * send out one of three responses at this point: ACK, WAIT, or * something else. * If it sends an ACK, then all is well, and we can go on to * receive the data. If it sends a something else, then there's * clearly an error, and we can abort with a clean conscience. * If it sends a WAIT, however, then we have to wait for up to * 30 seconds, during which time any of the above can happen, * including a second, third, etc. WAIT message. * If the EPS has sent a WAIT, it will send an ACK before * sending the data (but this was implied by the above para- * graphs). */ waiting = 1; while (waiting) { /* See which response the EPS sends, and react * accordingly. */ switch(recv_response(NULL)) { case ACK: /* Acknowledgement */ waiting = 0; /* Stop waiting */ break; case WAIT: /* Wait for up to 30 seconds */ send_response(chan, ACK); /* Okay, I'll wait */ /* Wait for a timeout */ if (timeout(LONG_TIMEOUT)) return(-3); /* 30-sec timeout * has occurred */ break; default: /* Something's wrong */ wait(BREATHER); return(-4); } } /* Receive the instrument parameters, part 1 */ recv_head(NULL); /* Read the head of the incoming message */ mpu_rbyte(); /* Should return 0x0c */ recv12(); /* Should return instrument number */ recv12(); /* Should return layer number */ recv12(); /* Should return wavesample number */ recv_tail(); /* Read the tail */ /* Send ACK to receive inst. parameters Part 2 */ send_response(chan, ACK); /* Ask for next part */ /* Receive the instrument parameters (Part 2: the revenge) */ recv_head(NULL); for (i = 0; i < 12; i++) /* Receive instrument name */ out->name[i] = recv16() >> 8; out->midi_chan = recv16() >> 8; /* Receive MIDI channel */ out->midi_prog = recv16() >> 8; /* Receive MIDI program number */ out->midi_pres = recv16() >> 8; /* Receive MIDI pressure */ out->size = recv16(); /* Receive instrument size */ out->key_dest = recv16() >> 8; /* Receive key destination */ for (i = 0; i < 4; i++) /* Receive bitmaps of layers */ out->patches[i] = recv16() >> 8; out->down_layers = recv16() >> 8; /* Receive key down layers */ out->up_layers = recv16() >> 8; /* Receive key up layers */ for (i = 23; i < 26; i++) /* Unused */ recv16(); out->low_key = recv16() >> 8; /* Receive key range: low key */ out->high_key = recv16() >> 8; /* Receive key range: high key */ out->transpose = recv16() >> 8; /* Receive transposition */ for (i = 0; i < 16; i++) /* Receive pitch table offsets */ out->pitch_off[i] = recv16() >> 8; for (i = 0; i < 16; i++) /* Receive layer offsets */ out->layer_off[i] = recv16() >> 8; for (i = 0; i < 256; i++) /* Receive wavesample offsets */ out->ws_off[i] = recv16() >> 8; for (i = 317; i < 323; i++) /* Unused */ recv16(); recv_tail(); /* Receive end of SysEx */ /* Send ACK to EPS to say that everything has been received * successfully. */ send_response(chan, ACK); wait(BREATHER); return(0); }