cwDynRefTbl.cpp : Added use of levelLookupA[] to convert from MIDI vel. to level.

This commit is contained in:
kevin 2023-09-12 17:39:55 -04:00
parent cea07592bc
commit 8c37bbe0f0

View File

@ -15,6 +15,8 @@ namespace cw
{
dyn_ref_t* dynRefA;
unsigned dynRefN;
unsigned* levelLookupA;
unsigned levelLookupN;
} dyn_ref_tbl_t;
@ -33,6 +35,23 @@ namespace cw
return rc;
}
void _fill_level_lookup_table( dyn_ref_tbl_t* p )
{
for(midi::byte_t vel=0; vel<midi::kMidiVelCnt; ++vel)
for(unsigned i=0; i<p->levelLookupN; ++i)
if( p->dynRefA[i].velocity >= vel )
{
midi::byte_t d0 = p->dynRefA[i].velocity - vel;
midi::byte_t d1 = i>0 ? (vel - p->dynRefA[i-1].velocity) : d0;
unsigned j = d0 <= d1 ? i : i-1;
p->levelLookupA[vel] = p->dynRefA[j].level;
break;
}
}
rc_t _parse_cfg( dyn_ref_tbl_t* p, const object_t* cfg )
{
rc_t rc = kOkRC;
@ -85,6 +104,10 @@ cw::rc_t cw::dyn_ref_tbl::create( handle_t& hRef, const object_t* cfg )
goto errLabel;
}
p->levelLookupN = midi::kMidiVelCnt;
p->levelLookupA = mem::allocZ<unsigned>(p->levelLookupN);
_fill_level_lookup_table(p);
hRef.set(p);
errLabel:
@ -140,26 +163,25 @@ unsigned cw::dyn_ref_tbl::velocity_to_level( handle_t h, midi::byte_t vel )
{
dyn_ref_tbl_t* p = _handleToPtr(h);
for(unsigned i=0; i<p->dynRefN; ++i)
if( p->dynRefA[i].velocity > vel )
{
midi::byte_t d0 = p->dynRefA[i].velocity - vel;
if( vel <= p->levelLookupN )
return p->levelLookupA[vel];
midi::byte_t d1 = i>0 ? (vel - p->dynRefA[i-1].velocity) : d0;
unsigned j = d0 <= d1 ? i : i-1;
return p->dynRefA[j].level;
}
return p->dynRefA[ p->dynRefN-1 ].velocity;
cwLogError(kInvalidArgRC,"The velocity value %i is out of range on level lookup.");
return 0;
}
void cw::dyn_ref_tbl::report( handle_t h )
{
dyn_ref_tbl_t* p = _handleToPtr(h);
unsigned i = 0;
printf("Dynamics Table\n");
auto rpt = [](dyn_ref_t& x) { printf("%2i %3i %s\n",x.level,x.velocity,x.marker); };
std::for_each(p->dynRefA,p->dynRefA+p->dynRefN, rpt );
printf("Dynamics Table Level Lookup Table.\n");
auto rpt_level = [&i](unsigned& x) { printf("%3i %2i\n",i++,x); };
std::for_each(p->levelLookupA,p->levelLookupA+p->levelLookupN, rpt_level);
}