Skip to content

Commit

Permalink
MIDI pedals ; Manufacturer ID detect
Browse files Browse the repository at this point in the history
  • Loading branch information
vlcoo authored May 6, 2022
1 parent ba34e73 commit 123226b
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 15 deletions.
74 changes: 68 additions & 6 deletions Channel.pde
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import java.util.Stack;


public class ChannelOsc {
int id;
HashMap<Integer, SoundObject> current_notes; // Pairs <note midi code, oscillator object>
float curr_global_amp = 1.0; // channel volume (0.0 to 1.0)
float amp_multiplier = 1.0; // basically expression
float curr_global_bend = 0.0; // channel pitch bend (-curr_bend_range to curr_bend_range semitones)
float curr_global_pan = 0.0; // channel stereo panning (-1.0 to 1.0)
float curr_bend_range = 2.0; // channel pitch bend range +/- semitones... uh, sure.
boolean hold_pedal = false;
boolean sostenuto_pedal = false;
boolean soft_pedal = false;
ArrayList<Integer> curr_holding;
ArrayList<Integer> curr_sustaining;
float curr_freqDetune = 0.0;
float curr_noteDetune = 0.0;
String please_how_many_midi_params_are_there = "dw, around 100+"; // darn.
Expand All @@ -29,18 +35,23 @@ public class ChannelOsc {

ChannelOsc() {
current_notes = new HashMap<Integer, SoundObject>();
curr_holding = new ArrayList();
curr_sustaining = new ArrayList();
}


ChannelOsc(int osc_type) {
current_notes = new HashMap<Integer, SoundObject>();
curr_holding = new ArrayList();
curr_sustaining = new ArrayList();
set_osc_type(osc_type);
}


void create_display(int x, int y, int id) {
ChannelDisplay d = new ChannelDisplay(x, y, id, this);
this.disp = d;
this.id = id;
}


Expand All @@ -59,7 +70,7 @@ public class ChannelOsc {


void play_note(int note_code, int velocity) {
if (curr_global_amp <= 0 || silenced) return;
if (curr_global_amp <= 0 || silenced || osc_type == -1) return;
if (osc_type == 4) {
play_drum(note_code, velocity);
return;
Expand All @@ -77,7 +88,7 @@ public class ChannelOsc {
current_notes.put(note_code, s);
}
s.pan(curr_global_pan);
s.amp(amp * (osc_type == 1 || osc_type == 2 ? 0.12 : 0.05) * curr_global_amp * amp_multiplier); // give a volume boost to TRI and SIN
s.amp(amp * (osc_type == 1 || osc_type == 2 ? 0.12 : 0.05) * curr_global_amp * amp_multiplier * (soft_pedal ? 0.5 : 1)); // give a volume boost to TRI and SIN
if (osc_type == 0) ((Pulse) s).width(pulse_width);

s.play();
Expand All @@ -95,7 +106,7 @@ public class ChannelOsc {
SoundFile s = (SoundFile) samples[sample_code-1];
if (s == null || s.isPlaying()) return;

s.amp(amp * 0.32 * curr_global_amp * amp_multiplier);
s.amp(amp * 0.32 * curr_global_amp * amp_multiplier * (soft_pedal ? 0.5 : 1));
s.play();

last_amp = amp;
Expand All @@ -105,6 +116,14 @@ public class ChannelOsc {


void stop_note(int note_code) {
if (hold_pedal) {
curr_holding.add(note_code);
return;
}
if (sostenuto_pedal && curr_sustaining.contains(note_code)) {
return;
}

if (osc_type != 4) {
SoundObject s = current_notes.get(note_code);
if (s == null) return;
Expand All @@ -117,6 +136,43 @@ public class ChannelOsc {
}


void set_hold(int value) {
if (osc_type == 4) return;
hold_pedal = value < 63 ? false : true;
if (!hold_pedal) {
for (int note_code : curr_holding) {
stop_note(note_code);
}
curr_holding.clear();
}
}


void set_sostenuto(int value) {
if (osc_type == 4) return;
sostenuto_pedal = value < 63 ? false : true;
if (sostenuto_pedal) {
for (Entry<Integer, SoundObject> s : this.current_notes.entrySet()) {
if (((Oscillator) s.getValue()).isPlaying()) {
curr_sustaining.add(s.getKey());
}
}
}
else {
for (int note_code : curr_sustaining) {
stop_note(note_code);
}
curr_sustaining.clear();
}
}


void set_soft(int value) {
soft_pedal = value < 63 ? false : true;
set_all_oscs_amp();
}


void set_osc_type(float osc_type) { // if 0.0 < value < 1.0, then pulse osc
shut_up();
current_notes.clear();
Expand Down Expand Up @@ -156,13 +212,13 @@ public class ChannelOsc {

void set_all_oscs_amp() {
for (SoundObject s : current_notes.values()) {
((Oscillator) s).amp((osc_type == 1 || osc_type == 2 ? 0.12 : 0.05) * curr_global_amp * amp_multiplier);
((Oscillator) s).amp((osc_type == 1 || osc_type == 2 ? 0.12 : 0.05) * curr_global_amp * amp_multiplier * (soft_pedal ? 0.5 : 1));
}
}


void set_bend(int bits_lsb, int bits_msb) {
if (osc_type == -1) return; // no bend for drums...
if (osc_type == 4) return; // no bend for drums...

int value = (bits_msb << 7) + bits_lsb;
curr_global_bend = map(value, 0, 16383, -1.0, 1.0) * curr_bend_range;
Expand Down Expand Up @@ -193,16 +249,22 @@ public class ChannelOsc {


void shut_up() {
set_hold(0);
set_sostenuto(0);
set_soft(0);
for (int note_code : current_notes.keySet()) stop_note(note_code);
}


void reset_params() {
current_notes.clear();
curr_holding.clear();
curr_sustaining.clear();
last_amp = 0.0;
last_freq = 0;
last_notecode = -1;
osc_type = -1;
if (id == 9) osc_type = 4;
else osc_type = -1;
pulse_width = 0.5;
curr_global_amp = 1.0;
amp_multiplier = 1.0;
Expand Down
47 changes: 47 additions & 0 deletions Player.pde
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class Player {
float last_freqDetune = 0.0;
float last_noteDetune = 0.0;
String custom_info_msg = "";
boolean file_is_GM = false;
boolean file_is_GM2 = false;
boolean file_is_XG = false;
boolean file_is_GS = false;


Synthesizer syn;
Expand Down Expand Up @@ -118,6 +122,27 @@ class Player {
}


protected void set_params_from_sysex(byte[] arr) {
int man_id = arr[0];
switch(man_id) {
case 67: // Yamh, XG
file_is_XG = true;
break;

case 65: // Rold, GS
file_is_GS = true;
break;

default: // check if GM or GM2
if (arr[2] == 9) {
file_is_GM = arr[3] == 1 ? true : false;
file_is_GM2 = arr[3] == 3 ? true : false;
}
break;
}
}


protected void set_rpn_param_val(int chan, float value) {
switch (curr_rpn) {
case 0:
Expand Down Expand Up @@ -330,6 +355,11 @@ class Player {

seq.setLoopEndPoint(-1);
seq.setLoopStartPoint(0);

file_is_GM = false;
file_is_GM2 = false;
file_is_XG = false;
file_is_GS = false;
}


Expand Down Expand Up @@ -415,6 +445,18 @@ class Player {
set_rpn_param_val(chan, data2); // data entry
break;

case 64: // hold pedal
channels[chan].set_hold(data2);
break;

case 66: // sostenuto pedal
channels[chan].set_sostenuto(data2);
break;

case 67: // soft pedal
channels[chan].set_soft(data2);
break;

case 96:
add_rpn_param_val(chan, data2, false); // data increment
break;
Expand All @@ -428,6 +470,11 @@ class Player {
}
}
}

else if (msg instanceof SysexMessage) {
SysexMessage event = (SysexMessage) msg;
set_params_from_sysex(event.getData());
}
}


Expand Down
Loading

0 comments on commit 123226b

Please sign in to comment.