Browse Source

cmDspKr.h/c: Added 'ScaleRange' DSP class.

master
kevin 11 years ago
parent
commit
14f163394d
2 changed files with 102 additions and 0 deletions
  1. 101
    0
      dsp/cmDspKr.c
  2. 1
    0
      dsp/cmDspKr.h

+ 101
- 0
dsp/cmDspKr.c View File

@@ -1515,3 +1515,104 @@ struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx )
1515 1515
 
1516 1516
   return &_cmGSwitchDC;
1517 1517
 }
1518
+
1519
+
1520
+//==========================================================================================================================================
1521
+
1522
+enum
1523
+{
1524
+  kMinInSrId,
1525
+  kMaxInSrId,
1526
+  kMinOutSrId,
1527
+  kMaxOutSrId,
1528
+  kValInSrId,
1529
+  kValOutSrId,
1530
+};
1531
+
1532
+cmDspClass_t _cmScaleRangeDC;
1533
+
1534
+typedef struct
1535
+{
1536
+  cmDspInst_t    inst;
1537
+
1538
+} cmDspScaleRange_t;
1539
+
1540
+
1541
+cmDspInst_t*  _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
1542
+{
1543
+  va_list vl1;
1544
+  va_copy(vl1,vl);
1545
+
1546
+  cmDspVarArg_t args[] =
1547
+  {
1548
+    { "min_in",   kMinInSrId,  0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
1549
+    { "max_in",   kMaxInSrId,  0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
1550
+    { "min_out",  kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
1551
+    { "max_out",  kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
1552
+    { "val_in",   kValInSrId,  0,0, kInDsvFl | kDoubleDsvFl,  "Input value."},
1553
+    { "val_out",  kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
1554
+    { NULL, 0, 0, 0, 0 }
1555
+  };
1556
+
1557
+  cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
1558
+
1559
+  cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
1560
+  cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
1561
+  cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
1562
+  cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
1563
+
1564
+
1565
+  return &p->inst;
1566
+}
1567
+
1568
+cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
1569
+{
1570
+  cmDspRC_t       rc          = kOkDspRC;
1571
+
1572
+  cmDspApplyAllDefaults(ctx,inst);
1573
+
1574
+  return rc;
1575
+}
1576
+
1577
+cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
1578
+{
1579
+  cmDspRC_t       rc = kOkDspRC;
1580
+  cmDspScaleRange_t* p  = (cmDspScaleRange_t*)inst;
1581
+
1582
+  cmDspSetEvent(ctx,inst,evt);
1583
+
1584
+  if( evt->dstVarId == kValInSrId )
1585
+  {
1586
+    double val     = cmDspDouble(inst,kValInSrId);
1587
+    double min_in  = cmDspDouble(inst,kMinInSrId);
1588
+    double max_in  = cmDspDouble(inst,kMaxInSrId);    
1589
+    double min_out = cmDspDouble(inst,kMinOutSrId);    
1590
+    double max_out = cmDspDouble(inst,kMaxOutSrId);    
1591
+
1592
+    double x = cmMax(min_in,cmMin(max_in,val));
1593
+    
1594
+    x = (x - min_in)/(max_in - min_in);
1595
+
1596
+    x = min_out + x * (max_out - min_out);
1597
+
1598
+    cmDspSetDouble(ctx,inst,kValOutSrId, x );
1599
+    //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
1600
+  }
1601
+  return rc;
1602
+}
1603
+
1604
+
1605
+struct cmDspClass_str* cmScaleRangeClassCons( cmDspCtx_t* ctx )
1606
+{
1607
+  cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
1608
+    NULL,
1609
+    _cmDspScaleRangeAlloc,
1610
+    NULL,
1611
+    _cmDspScaleRangeReset,
1612
+    NULL,
1613
+    _cmDspScaleRangeRecv,
1614
+    NULL,NULL,
1615
+    "Scale a value inside an input range to a value in the output range.");
1616
+
1617
+  return &_cmScaleRangeDC;
1618
+}

+ 1
- 0
dsp/cmDspKr.h View File

@@ -12,6 +12,7 @@ extern "C" {
12 12
   struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx );
13 13
   struct cmDspClass_str* cmScModClassCons( cmDspCtx_t* ctx );
14 14
   struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx );
15
+  struct cmDspClass_str* cmScaleRangeClassCons( cmDspCtx_t* ctx );
15 16
 
16 17
 #ifdef __cplusplus
17 18
 }

Loading…
Cancel
Save