📄 midifns.c
字号:
void midi_write(int n, int port, unsigned char c1, unsigned char c2, unsigned char c3){ if (n >= 1) mPutData(c1); if (n >= 2) mPutData(c2); if (n >= 3) mPutData(c3); midi_write_trace(n, port, c1, c2, c3);}#elsevoid midi_write(int n, int port, unsigned char c1, unsigned char c2, unsigned char c3){ midi_write_trace(n, port, c1, c2, c3);}#endif#endif#ifdef MACINTOSH#ifdef MIDIMGRvoid midi_write(int n, int port, unsigned char c1, unsigned char c2, unsigned char c3){ MIDIPacket TheMIDIPacket; TheMIDIPacket.flags = midiTimeStampCurrent; TheMIDIPacket.len = 6 + n; TheMIDIPacket.tStamp = 0; TheMIDIPacket.data[0] = c1; TheMIDIPacket.data[1] = c2; TheMIDIPacket.data[2] = c3; MIDIWritePacket(OutputRefNum, &TheMIDIPacket); midi_write_trace(n, port, c1, c2, c3);}#elsevoid midi_write(int n, int port, unsigned char c1, unsigned char c2, unsigned char c3){#ifndef NYQUIST Xmit(port, c1); if (n >= 2) Xmit(port, c2); if (n >= 3) Xmit(port, c3);#endif midi_write_trace(n, port, c1, c2, c3);}#endif#endifvoid midi_write_trace(int n, int port, unsigned char c1, unsigned char c2, unsigned char c3){ if (miditrace) { /* to indicate bytes going out on port 1, put message in brackets * with the port number, e.g. [1:~90~3c~64] */ if (port > 0) gprintf(TRANS, "[%d:", port); if (n >= 1) gprintf(TRANS, "~%2x", c1); if (n >= 2) gprintf(TRANS, "~%2x", c2); if (n >= 3) gprintf(TRANS, "~%2x", c3); if (port > 0) gprintf(TRANS, "]", port); }}/****************************************************************** set_pitch_default*****************************************************************/private void set_pitch_default(){ int i; for (i = 0; i < 128; i++) { pit_tab[i].pbend = 8192; pit_tab[i].ppitch = i; }}/****************************************************************** read_tuning*****************************************************************/void read_tuning(filename)char *filename;{ int index, pit, lineno = 0; float bend; FILE *fpp; user_scale = TRUE; set_pitch_default(); fpp = fileopen(filename, "tun", "r", "Tuning definition file"); while ((fscanf(fpp, "%d %d %f\n", &index, &pit, &bend) > 2) && (lineno < 128)) { lineno++; if (index >= 0 && index <= 127) { pit_tab[index].pbend = (int)(8192 * bend/100 + 8192); pit_tab[index].ppitch = pit; } }}/***************************************************************************** musicinit* Effect: ****************************************************************************/void musicinit(){ int i; char *filename; if (!tune_flag) { /* do this code only once */ miditrace = cl_switch("miditrace"); musictrace = cl_switch("trace"); } if (!initialized) { cu_register((cu_fn_type) musicterm, NULL); midi_init(); } initialized = TRUE; /* this does some random cleanup activity */#ifndef APPLICATION if (!tune_flag) { /* do this code only once */#ifdef DOS#ifndef WINDOWS#if 0 version = mPutGetCmd(GETMPUVER); revision = mPutGetCmd(GETMPUREV); gprintf(TRANS, "MPU version %d.%d%c\n", version >> 4, version & 0x0f, revision + 'A' - 1);#endif mPutCmd(UARTMODE); mPutCmd(NOREALTIME); /* initially prevent Real Time MIDI info */ mPutCmd(EXCLUSIVOFF); /* initially prevent Sys-Ex data */#endif#endif tune_flag = TRUE; filename = cl_option("tune"); if (filename != NULL) read_tuning(filename); } /* now that flags are set, print the trace message */ if (musictrace) gprintf(TRANS, "musicinit()\n"); if (user_scale) { for (i = 0; i < MAX_CHANNELS; i++) { midi_bend(i, 8192); bend[i] = 8192; } }#endif /* ifndef APPLICATION */ for (i = 0; i < MAX_CHANNELS; i++) { /* initialize to impossible values so that the * next call to midi_bend or midi_program will * not match and therefore send an output: */ bend[i] = -1; cur_midi_prgm[i] = -1; }#ifdef MIDI_THRU midi_thru(!(cl_switch("block"))); /* set MIDI thru */#endif timereset(); /* Reset clock */#ifdef AMIGA event_mask |= (1L << ascii_signal()) | (1L << cmt_mi->AlarmSigBit) | (1L << cmt_mi->RecvSigBit);#endif}/***************************************************************************** musicterm* Effect: * Miscellaneous cleanup of things done by musicinit.****************************************************************************/private void musicterm(){ if (musictrace) gprintf(TRANS, "musicterm()\n"); initialized = FALSE;}/***************************************************************************** cmtrand* Inputs:* int lo: Lower limit of value* int hi: Upper limit of value* Result: int* random number (lo <= result <= hi)****************************************************************************/long randseed = 1534781L;short cmtrand(short lo, short hi){ randseed *= 13L; randseed += 1874351L; return((short)(lo + (((hi + 1 - lo) * ((0x00ffff00 & randseed) >> 8)) >> 16)));}#ifdef AMIGA/* remove_sysex_buffer -- a cleanup procedure for the Amiga *//**/void remove_sysex_buffer(void *obj){ ClearSysExQueue(cmt_mi);}#endif /* AMIGA *//***************************************************************************** settime* Inputs: new time* Effect: * Sets the current time to the new time.* DMH: for MAC, sets the clock to absTime* implemented by adjusting ticksATStart****************************************************************************/void settime(newtime) time_type newtime;{ if (musictrace) gprintf(TRANS, "settime(%lu)\n", newtime);#ifdef AMIGA timeoffset = *camdtime - (newtime >> 1);#endif#ifdef MACINTOSH#ifdef MIDIMGR ticksAtStart = MIDIGetCurTime(OutputRefNum);#else ticksAtStart = TickCount() - MS_TO_TICKS(newtime);#endif#endif }/***************************************************************************** timereset* Effect: * Resets the time.* DMH: for MAC, implemented by setting ticksAtStart to* current value of system tick counter* JMN: for DOS, resets the time on the MPU-401. Ticks is reset to 0****************************************************************************/void timereset(){#if HAS_GETTIMEOFDAY struct timeval timeval;#endif#if HAS_FTIME struct timeb ftime_res;#endif if (!initialized) fixup(); if (musictrace) gprintf(TRANS,"timereset()\n");#ifdef AMIGA timeoffset = *camdtime;#endif#ifdef DOS#ifndef WINDOWS timeoffset = (ulong) readtimer();#endif#endif#ifdef MACINTOSH#ifdef MIDIMGR ticksAtStart = MIDIGetCurTime(OutputRefNum);#else ticksAtStart = TickCount();#endif#endif #if HAS_GETTIMEOFDAY gettimeofday(&timeval, 0); timeoffset = timeval.tv_sec * 1000 + timeval.tv_usec / 1000 - timeoffset;#endif#if HAS_FTIME ftime(&ftime_res); timeoffset = ftime_res.time;#endif}/***************************************************************************** trace* Inputs:* boolean flag: TRUE for trace on* Effect: * turns tracing on (flag == TRUE) or off (flag == FALSE)****************************************************************************/void trace(boolean flag){ musictrace = flag;}/***************************************************************************** tracemidi* Inputs:* boolean flag: TRUE for trace on* Effect: * turns midi tracing on (flag == TRUE) or off (flag == FALSE)****************************************************************************/void tracemidi(boolean flag){ miditrace = flag;}/************************************************************************* midi and timer initialization************************************************************************/#ifdef DOS/* binary value of hex char */private int xval(int c){ int i; static char t[]="0123456789abcdef"; for (i=0; i<16; i++) if(tolower(c)==t[i]) return(i); return (-1);}/* binary value of hex string */private int atox(char *t){ int i=0; int x; while(*t) { if ((x=xval(*t++))<0)return (0); i=(i<<4)+x; } return (i);}#endif /* def DOS */private void midi_init(){#ifdef UNIX_IRIX_MIDIFNS#define PBUFLEN 4 MIconfig *config; static u_int pbuf[] = { MI_STAMPING, MINOSTAMP, MI_BLOCKING, MINONBLOCKING};#endif#ifdef UNIX_MACH mach_midi_init();#else#ifdef ITC midiconn = mi_open(NULL); if (midiconn == NULL) { gprintf(FATAL, "could not open a MIDI device\n"); EXIT(1); } cu_register((cu_fn_type) mi_close, (void *) midiconn);#endif#endif#ifdef AMIGA amiga_midi_init();#endif /* def AMIGA */#ifdef DOS#ifndef WINDOWS int err; int irq=SEARCHIRQ; int base=MPUBASEADDR; char *t; if (t=getenv("MPUIRQ")) { if (musictrace) gprintf(TRANS,"MPUIRQ %s\n",t); irq=atoi(t); } if (t=getenv("MPUBASE")) { if (musictrace) gprintf(TRANS,"MPUBASE %s\n",t); base=atox(t); } if(err = mOpen(base, irq)) { mClose(err); EXIT(1); } cu_register((cu_fn_type) mClose, 0); cu_register((cu_fn_type) mPutCmd, (cu_parm_type) MPURESET); initializetimer(); cu_register((cu_fn_type) restoretimer, NULL);#endif#endif#ifdef MACINTOSH#ifndef NYQUIST /* if NYQUIST, do nothing */#ifdef MIDIMGR setup_midimgr(); /* this registers itself for cleanup */#else init_abort_handler(); cu_register(cleanup_abort_handler, NULL); setupMIDI(portA, 0x80); cu_register(restoreMIDI, (long) portA); /* only initialize portB if necessary */ if (MAX_CHANNELS > CHANNELS_PER_PORT) { setupMIDI(portB, 0x80); cu_register(restoreMIDI, (long) portB); }#endif#endif /* NYQUIST */#ifdef MIDIMGR ticksAtStart = MIDIGetCurTime(OutputRefNum);#else ticksAtStart = TickCount(); /* reset the clock */#endif#endif /* def MACINTOSH */ if (!(cl_switch("noalloff"))) cu_register((cu_fn_type) alloff, NULL);}#ifdef DOS/***************************************************************************** set_x_mfr* Inputs:* unsigned char mfr: Manufacturer ID for MIDI* Result: void* * Effect: * Sets the xcode and xcodemask to allow only these sysex messages****************************************************************************/void set_x_mfr(mfr)unsigned char mfr;{ xcode = mfr; xcodemask = 0xFF;}/***************************************************************************** clear_x_mfr* Result: void* * Effect: * Clears sysex manufacturer code filter; accepts all sysex messages****************************************************************************/void clear_x_mfr(){ xcode = 0; xcodemask = 0;}#endif /* DOS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -