fand / veda

⚡VJ / Live Coding on Atom⚡

Home Page:http://veda.gl/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Receive MIDI CC mesages

AlexandreRangel opened this issue · comments

REALLY hooked on #VEDAVJ here! Congratulations for the great project!
Is there a way to receive MIDI Control Change CC messages (from MIDI controllers with knobs and faders)?

// is this code ok to receive midi CC from knob 8?
//////////////////////////////////////////////////////////////////////

/{
"midi": true
}
/

#ifdef GL_ES
precision mediump float;
#endif

#extension GL_OES_standard_derivatives : enable

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
uniform sampler2D midi;

void main( void ) {

vec2 pos = gl_FragCoord.xy;
float d = dot(pos, vec2(sqrt(pos.x), sqrt(pos.y + time)));

// midi plug:
float myRed = float(texture2D(midi, vec2(8., 0.)));

gl_FragColor = vec4( myRed , cos(d), tan(d), 100.0);

}

// also tried the following code, but throws 2 errors on the last line:
// 'texture2D': no matching overloaded function found
// 'assign' : cannot convert from 'const float' to 'fragColor mediump 4-component vector of float FragColor'

/{
"midi": true
}
/

#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D midi;

void main( void ) {
gl_FragColor = texture2D(midi, 8. / 255., 0.);
}

Hi @AlexandreRangel ! Sorry for late reply 😅
The snippets you wrote are almost right, but we have to fix 2 problems.

First, the comment must be /* */, so the line 1~3 must be:

/*{
  "midi": true,
}*/

Next, the coordinate of midi is not correct.

MIDI CC messsages consist of 3 values.
For example, when you turn the 8th knob max, the MIDI message is b0 08 7f.

The size of texture midi is 256x128.
x-coord and y-coord correspond to the 1st and 2nd byte of MIDI messages, respectively.

So, if you want the value of 8th knob, add this:

float myRed = texture2D(midi, vec2(176. / 256., 8. / 128.)).r;

Here is the complete example.

/*{
  "midi": true
}*/

#ifdef GL_ES
precision mediump float;
#endif

#extension GL_OES_standard_derivatives : enable

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
uniform sampler2D midi;

void main( void ) {
  vec2 pos = gl_FragCoord.xy;
  float d = dot(pos, vec2(sqrt(pos.x), sqrt(pos.y + time)));

  // midi plug:
  float myRed = texture2D(midi, vec2(176. / 256., 8. / 128.)).r;

  gl_FragColor = vec4( myRed , cos(d), tan(d), 100.0) * .5;
}

The 2nd snippet must be like this:

/*{
  "midi": true
}*/

#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D midi;

void main( void ) {
  gl_FragColor = texture2D(midi, vec2(176. / 256., 8. / 128.));
}

Perfect! Thank you very much!