// // Copyright: Copyright 2003 Craig Stuart Sapp // Programmer: Craig Stuart Sapp // Creation Date: Sun Feb 9 10:51:25 PST 2003 // Last Modified: Sun Feb 9 10:54:37 PST 2003 // Filename: polyflip.c // Web Address: http://peabody.sapp.org/class/dmp2/lab/flip/polyflip.c // Syntax: C; Max4/MSP2 External Object; CodeWarrior 6.0 // OS: Mac OS 9; PPC // // Description: Flips the key number and duration of MIDI notes. // #include "ext.h" typedef struct { t_object max_data; // Max/MSP data, MUST come first in struct long keymap[127]; // note mappings long velin; // current input velocity void* outputKeyNumber; // output the note number of the note void* outputVelocity; // output the duration of note in milliseconds } MyObject; void* object_data = NULL; // function declarations: void* create_object (void); void InputKeyNumber (MyObject* mo, long value); void InputVelocity (MyObject* mo, long value); long midilimit (long value); ///////////////////////////////////////////////////////////////////////// // // Initialization functions // ////////////////////////////// // // main -- called once when the object is created in a patcher window. // void main(void) { setup((t_messlist**)&object_data, (method)create_object, NULL, sizeof(MyObject), NULL, A_NOTHING); addint ((method)InputKeyNumber); // inlet 1 addinx ((method)InputVelocity, 1); // inlet 2 } ////////////////////////////// // // create_object -- create the data storage for the mydiff object and // and setup input 1. // void* create_object(void) { int i; MyObject* mo = (MyObject*)newobject(object_data); mo->velin = 0; for (i=0; i<128; i++) { mo->keymap[i] = 0; } mo->outputVelocity = intout(mo); // outlet 2 mo->outputKeyNumber = intout(mo); // outlet 1 intin(mo, 1); // inlet 2 return mo; } ///////////////////////////////////////////////////////////////////////// // // Behavior functions // ////////////////////////////// // // InputKeyNumber -- behavior of the object when a new number comes // in on inlet 1. // void InputKeyNumber(MyObject* mo, long value) { value = midilimit(value); if (mo->velin == 0) { outlet_int(mo->outputVelocity, mo->velin); outlet_int(mo->outputKeyNumber, mo->keymap[value]); } else { outlet_int(mo->outputVelocity, value); outlet_int(mo->outputKeyNumber, mo->velin); mo->keymap[value] = midilimit(mo->velin); } } ////////////////////////////// // // InputVelocity -- behavior of the object when a new number comes // in on inlet 2. // void InputVelocity(MyObject* mo, long value) { mo->velin = value; } ///////////////////////////////////////////////////////////////////////// // // Non-interface functions: // ////////////////////////////// // // midilimit -- limit a number to the range from 0 to 127. // if the input is less than 0, return 0. // if the input is greater than 127, return 127. // long midilimit(long value) { if (value < 0) return 0; if (value > 127) return 127; return value; }