I have nearly identical functions for soft takeover adjustment of volume and pan values. The issue is that the volume function is working fine, while pan function stops at midi value 64 (D_PAN value 0), when reaching the center position from either side..
Guess it might be somehow related with the "D_PAN" range, which is -1...1.
Tried GetSetMediaTrack, SetMediaTrack, CSurf_OnPanChange functions. The behavior stays the same.
Conversion functions were taken from Reaper SDK.
Why is it happening?
Code:
static double charToVol(unsigned char val)
{
double pos = ((double)val*1000.0) / 127.0;
pos = SLIDER2DB(pos);
return DB2VAL(pos);
}
static unsigned char volToChar(double vol)
{
double d = (DB2SLIDER(VAL2DB(vol))*127.0 / 1000.0);
if (d < 0.0)d = 0.0;
else if (d > 127.0)d = 127.0;
return (unsigned char)(d + 0.5);
}
static double charToPan(unsigned char val)
{
double pos = ((double)val*1000.0 + 0.5) / 127.0;
pos = (pos - 500.0) / 500.0;
if (fabs(pos) < 0.08) pos = 0.0;
return pos;
}
static unsigned char panToChar(double pan)
{
pan = (pan + 1.0)*63.5;
if (pan < 0.0)pan = 0.0;
else if (pan > 127.0)pan = 127.0;
return (unsigned char)(pan + 0.5);
}
const int slE[8]{ 120, 121, 122, 123, 124, 125, 126, 127 }; // Encoders
const int slP[8]{ 8, 9, 10, 11, 12, 13, 14, 15 }; // Pots
void OnMIDIEvent(MIDI_event_t *evt)
{
if (evt->midi_message[0] == 0xBF)
{
//Faders
if (slF[0] <= evt->midi_message[1] && evt->midi_message[1] <= slF[7]) //Faders
{
int tid = evt->midi_message[1] - slF[0] + 1;
MediaTrack *tr = CSurf_TrackFromID(tid, g_csurf_mcpmode);
int curVal = volToChar(GetMediaTrackInfo_Value(tr, "D_VOL"));
if (lastF[tid - 1] < curVal && evt->midi_message[2] >= curVal)
{
CSurf_OnVolumeChange(tr, charToVol(evt->midi_message[2]), 0);
}
else if (lastF[tid - 1] > curVal && evt->midi_message[2] <= curVal)
{
CSurf_OnVolumeChange(tr, charToVol(evt->midi_message[2]), 0);
}
else if (lastF[tid - 1] == curVal)
{
CSurf_OnVolumeChange(tr, charToVol(evt->midi_message[2]), 0);
}
lastF[tid - 1] = evt->midi_message[2];
}
//Pots
else if (slP[0] <= evt->midi_message[1] && evt->midi_message[1] <= slP[7]) //Pots
{
int tid = evt->midi_message[1] - slP[0] + 1;
MediaTrack *tr = CSurf_TrackFromID(tid, g_csurf_mcpmode);
int curPan = panToChar(GetMediaTrackInfo_Value(tr, "D_PAN"));
//if (lastP[tid - 1] < curPan && evt->midi_message[2] >= curPan)
if (lastP[tid - 1] < curPan && evt->midi_message[2] >= curPan)
{
//CSurf_OnPanChange(tr, charToPan(evt->midi_message[2], 0);
double amt = charToPan(evt->midi_message[2]);
GetSetMediaTrackInfo(tr, "D_PAN", &amt);
}
else if (lastP[tid - 1] > curPan && evt->midi_message[2] <= curPan)
{
//CSurf_OnPanChange(tr, charToPan(evt->midi_message[2], 0);
double amt = charToPan(evt->midi_message[2]);
GetSetMediaTrackInfo(tr, "D_PAN", &amt);
}
else if (lastP[tid - 1] == curPan)
{
//CSurf_OnPanChange(tr, charToPan(evt->midi_message[2], 0);
double amt = charToPan(evt->midi_message[2]);
GetSetMediaTrackInfo(tr, "D_PAN", &amt);
}
lastP[tid - 1] = evt->midi_message[2];
}
}
}