// Hex Synth by Stefan Blixt // Synth setup to be used with a guitar hex pickup/MIDI converter. // It receives MIDI note messages on six different channels. // One voice is assigned to each channel respectively. // Run chuck --probe to see what MIDI device number to put here 4 => int MIDI_DEVICE; // Hex Synth will listen to midi channels BASE_CHANNEL, BASE_CHANNEL+1, BASE_CHANNE…+2, ... , BASE_CHANNEL+5 // So for BASE_CHANNEL == 0, the midi channels will be 0, 1, 2, 3, 4, 5 0 => int BASE_CHANNEL; Gain mixer => dac; class HexVoice { // Put your sound-making stuff here, and route it to mixer BlitSaw osc => Envelope env => mixer; 0.1 => osc.gain; 5 => osc.harmonics; int channel; // This gets called whenever a note on is received for the channel assigned to this voice fun void noteOn(int note, int velocity) { <<< "Channel ", channel, note >>>; Std.mtof( note + 12 ) => osc.freq; env.keyOn(); } // This gets called whenever a note off is received for the channel assigned to this voice fun void noteOff() { env.keyOff(); } // this will be called if control messages are received on this channel fun void controlMessage(int control, int value) { } } HexVoice voices[6]; MidiIn midiIn; fun void setup() { for (0 => int i; i < voices.size(); i++) { i + BASE_CHANNEL => voices[i].channel; } if (midiIn.open(MIDI_DEVICE)) { <<< "Opened device: ", midiIn.name() >>>; } else { <<< "Couldn't open device #", MIDI_DEVICE >>>; } } fun HexVoice getVoice(int channel) { for (0 => int i; i < voices.size(); i++) { if (voices[i].channel == channel) { return voices[i]; } } return null; } fun void listener() { MidiMsg midiMsg; while (true) { midiIn => now; while (midiIn.recv(midiMsg)) { // <<< "Received midi: ", midiMsg.data1, midiMsg.data2, midiMsg.data3 >>>; (midiMsg.data1 & 0xf0) / 16 => int message; midiMsg.data1 & 0x0f => int channel; getVoice(channel) @=> HexVoice @ voice; if (voice != null) { if (message == 0x0b) { voice.controlMessage(midiMsg.data2, midiMsg.data3); } else if (message == 0x09) { midiMsg.data3 => int velocity; if (velocity == 0) { voice.noteOff(); } else { voice.noteOn(midiMsg.data2, velocity); } } else if (message == 0x08) { voice.noteOff(); } } } } } setup(); spork ~ listener(); 1::week => now;