2023-08-20 21:14:52 +00:00
|
|
|
#include "cwCommon.h"
|
|
|
|
#include "cwLog.h"
|
|
|
|
#include "cwCommonImpl.h"
|
|
|
|
#include "cwMem.h"
|
|
|
|
#include "cwText.h"
|
|
|
|
#include "cwObject.h"
|
|
|
|
#include "cwMidi.h"
|
|
|
|
#include "cwDynRefTbl.h"
|
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
|
|
|
namespace dyn_ref_tbl
|
|
|
|
{
|
|
|
|
typedef struct dyn_ref_tbl_str
|
|
|
|
{
|
2023-10-21 13:54:23 +00:00
|
|
|
dyn_ref_t* dynRefA; // dynRefA[ dynRefN ] one entry per dyn. level
|
|
|
|
unsigned dynRefN; //
|
|
|
|
unsigned* levelLookupA; // levelLoopA[ levelLookupN ] - one entry per velocity value (0-127)
|
|
|
|
unsigned levelLookupN; // levelLooupN = kMidiVelCnt
|
2023-08-20 21:14:52 +00:00
|
|
|
|
|
|
|
} dyn_ref_tbl_t;
|
|
|
|
|
|
|
|
dyn_ref_tbl_t* _handleToPtr( handle_t h )
|
|
|
|
{ return handleToPtr<handle_t,dyn_ref_tbl_t>(h); }
|
|
|
|
|
|
|
|
rc_t _destroy( dyn_ref_tbl_t* p )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
auto relse = [](dyn_ref_t& x) { mem::release(x.marker); };
|
|
|
|
|
|
|
|
std::for_each(p->dynRefA, p->dynRefA+p->dynRefN, relse );
|
|
|
|
mem::release(p->dynRefA);
|
2023-10-21 13:54:23 +00:00
|
|
|
mem::release(p->levelLookupA);
|
2023-08-20 21:14:52 +00:00
|
|
|
mem::release(p);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:39:55 +00:00
|
|
|
void _fill_level_lookup_table( dyn_ref_tbl_t* p )
|
|
|
|
{
|
|
|
|
|
|
|
|
for(midi::byte_t vel=0; vel<midi::kMidiVelCnt; ++vel)
|
2023-10-21 13:54:23 +00:00
|
|
|
for(unsigned i=0; i<p->dynRefN; ++i)
|
2023-09-12 21:39:55 +00:00
|
|
|
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;
|
2023-10-21 13:54:23 +00:00
|
|
|
|
|
|
|
assert( vel <= p->levelLookupN );
|
|
|
|
|
2023-09-12 21:39:55 +00:00
|
|
|
p->levelLookupA[vel] = p->dynRefA[j].level;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-20 21:14:52 +00:00
|
|
|
rc_t _parse_cfg( dyn_ref_tbl_t* p, const object_t* cfg )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
// parse the dynamics ref. array
|
|
|
|
p->dynRefN = cfg->child_count();
|
|
|
|
|
|
|
|
if( p->dynRefN == 0 )
|
|
|
|
cwLogWarning("The dynamic reference array cfg. is empty.");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p->dynRefA = mem::allocZ<dyn_ref_t>(p->dynRefN);
|
|
|
|
|
|
|
|
for(unsigned i=0; i<p->dynRefN; ++i)
|
|
|
|
{
|
|
|
|
const object_t* r = cfg->child_ele(i);
|
|
|
|
const char* marker = nullptr;
|
|
|
|
|
|
|
|
if((rc = r->getv("mark", marker,
|
|
|
|
"level", p->dynRefA[i].level,
|
|
|
|
"vel", p->dynRefA[i].velocity)) != kOkRC )
|
|
|
|
{
|
|
|
|
rc = cwLogError(kSyntaxErrorRC,"Error parsing the dynamics reference array.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->dynRefA[i].marker = mem::duplStr(marker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
errLabel:
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::dyn_ref_tbl::create( handle_t& hRef, const object_t* cfg )
|
|
|
|
{
|
|
|
|
rc_t rc;
|
|
|
|
if((rc = destroy(hRef)) != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
dyn_ref_tbl_t* p = mem::allocZ<dyn_ref_tbl_t>();
|
|
|
|
|
|
|
|
if((rc = _parse_cfg( p, cfg )) != kOkRC )
|
|
|
|
{
|
|
|
|
rc = cwLogError(rc,"Dynamics reference table parse failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:39:55 +00:00
|
|
|
p->levelLookupN = midi::kMidiVelCnt;
|
|
|
|
p->levelLookupA = mem::allocZ<unsigned>(p->levelLookupN);
|
|
|
|
_fill_level_lookup_table(p);
|
|
|
|
|
2023-08-20 21:14:52 +00:00
|
|
|
hRef.set(p);
|
|
|
|
|
|
|
|
errLabel:
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::dyn_ref_tbl::destroy( handle_t& hRef )
|
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
if(!hRef.isValid())
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
dyn_ref_tbl_t* p = _handleToPtr(hRef);
|
|
|
|
|
|
|
|
if((rc = _destroy(p)) != kOkRC )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
hRef.clear();
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cw::dyn_ref_tbl::level_to_marker( handle_t h, unsigned level )
|
|
|
|
{
|
|
|
|
dyn_ref_tbl_t* p = _handleToPtr(h);
|
|
|
|
for(unsigned i=0; i<p->dynRefN; ++i)
|
|
|
|
if( p->dynRefA[i].level )
|
|
|
|
return p->dynRefA[i].marker;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned cw::dyn_ref_tbl::marker_to_level( handle_t h, const char* marker )
|
|
|
|
{
|
|
|
|
dyn_ref_tbl_t* p = _handleToPtr(h);
|
|
|
|
for(unsigned i=0; i<p->dynRefN; ++i)
|
|
|
|
if( textIsEqual(p->dynRefA[i].marker,marker) )
|
|
|
|
return p->dynRefA[i].level;
|
|
|
|
return kInvalidIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::midi::byte_t cw::dyn_ref_tbl::level_to_velocity( handle_t h , unsigned level )
|
|
|
|
{
|
|
|
|
dyn_ref_tbl_t* p = _handleToPtr(h);
|
|
|
|
for(unsigned i=0; i<p->dynRefN; ++i)
|
|
|
|
if( p->dynRefA[i].level == level )
|
|
|
|
return p->dynRefA[i].velocity;
|
|
|
|
return midi::kInvalidMidiByte;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned cw::dyn_ref_tbl::velocity_to_level( handle_t h, midi::byte_t vel )
|
|
|
|
{
|
|
|
|
dyn_ref_tbl_t* p = _handleToPtr(h);
|
|
|
|
|
2023-09-12 21:39:55 +00:00
|
|
|
if( vel <= p->levelLookupN )
|
|
|
|
return p->levelLookupA[vel];
|
|
|
|
|
|
|
|
cwLogError(kInvalidArgRC,"The velocity value %i is out of range on level lookup.");
|
|
|
|
return 0;
|
2023-08-20 21:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cw::dyn_ref_tbl::report( handle_t h )
|
|
|
|
{
|
|
|
|
dyn_ref_tbl_t* p = _handleToPtr(h);
|
2023-09-12 21:39:55 +00:00
|
|
|
unsigned i = 0;
|
2023-08-20 21:14:52 +00:00
|
|
|
|
2023-09-12 21:39:55 +00:00
|
|
|
printf("Dynamics Table\n");
|
2023-08-20 21:14:52 +00:00
|
|
|
auto rpt = [](dyn_ref_t& x) { printf("%2i %3i %s\n",x.level,x.velocity,x.marker); };
|
|
|
|
|
2023-09-12 21:39:55 +00:00
|
|
|
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);
|
2023-08-20 21:14:52 +00:00
|
|
|
}
|