소스 검색

cmProc2.h/c : Added cmExpander and cmExpanderBank.

master
Kevin Larke 9 년 전
부모
커밋
e9ad88ae0f
2개의 변경된 파일396개의 추가작업 그리고 5개의 파일을 삭제
  1. 350
    5
      cmProc2.c
  2. 46
    0
      cmProc2.h

+ 350
- 5
cmProc2.c 파일 보기

@@ -5494,13 +5494,330 @@ cmRC_t    cmFbCtlExec( cmFbCtl_t* p, const cmReal_t* x0V )
5494 5494
   return rc;
5495 5495
 }
5496 5496
 
5497
+//=======================================================================================================================
5498
+cmExpander* cmExpanderAlloc( cmCtx* c, cmExpander* p, 
5499
+  double srate, unsigned procSmpCnt, double threshDb, double rlsDb, 
5500
+  double threshMs, double rmsMs, double atkMs, double rlsMs )
5501
+{
5502
+  cmExpander* op = cmObjAlloc(cmExpander,c,p);
5503
+  
5504
+  if( srate > 0 )  
5505
+    if( cmExpanderInit(op,srate, procSmpCnt, threshDb, rlsDb, threshMs, rmsMs, atkMs, rlsMs) != cmOkRC )
5506
+      cmExpanderFree(&op);
5507
+
5508
+  return op;
5509
+
5510
+}
5511
+
5512
+cmRC_t      cmExpanderFree(  cmExpander** pp )
5513
+{
5514
+  cmRC_t rc = cmOkRC;
5515
+  if( pp==NULL || *pp==NULL )
5516
+    return rc;
5517
+
5518
+  cmExpander* p = *pp;
5519
+  if((rc = cmExpanderFinal(p)) != cmOkRC )
5520
+    return rc;
5521
+
5522
+  cmMemFree(p->rmsV);
5523
+  cmMemFree(p->envV);
5524
+  cmObjFree(pp);
5525
+  return rc;
5526
+
5527
+}
5528
+
5529
+cmRC_t      cmExpanderInit( cmExpander* p, 
5530
+  double srate, unsigned procSmpCnt, double threshDb, double rlsDb, 
5531
+  double threshMs, double rmsMs, double atkMs, double rlsMs )
5532
+{
5533
+  cmRC_t rc;
5534
+  unsigned i;
5535
+
5536
+  if((rc = cmExpanderFinal(p)) != cmOkRC )
5537
+    return rc;
5538
+  
5539
+  unsigned atkN = cmMax(1,ceil( atkMs / (srate * 1000.0)));
5540
+  unsigned rlsN = cmMax(1,ceil( rlsMs / (srate * 1000.0)));
5541
+  
5542
+  p->rmsN      = cmMax(1,ceil(rmsMs / (srate * 1000.0)));
5543
+  p->rmsV      = cmMemResizeZ(cmReal_t,p->rmsV,p->rmsN);
5544
+  p->rmsIdx    = 0;
5545
+
5546
+  p->envN      = atkN + rlsN;
5547
+  p->envV      = cmMemResizeZ(cmSample_t,p->envV,p->envN);
5548
+  p->envIdx    = p->envN;
5549
+
5550
+  p->threshN   = cmMax(1,ceil(threshMs / (srate * 1000.0)));
5551
+  p->threshIdx = 0;
5552
+
5553
+  p->threshLvl = pow(10.0,(threshDb/20.0));
5554
+  p->rlsLvl    = pow(10.0,(rlsDb/20.0));
5555
+
5556
+  p->gain = 1.0;
5557
+  p->atkCnt = 0;
5558
+
5559
+  cmSample_t G = (1.0 - p->rlsLvl);
5560
+  for(i=0; i<atkN; ++i)
5561
+  {
5562
+    p->envV[i] = 1.0 - G*i/atkN;
5563
+  }
5564
+
5565
+  for(i=0; i<rlsN; ++i)
5566
+  {
5567
+    p->envV[atkN+i] = p->rlsLvl + (G*i/rlsN);
5568
+  }
5569
+
5570
+  printf("rmsN:%i atkN:%i rlsN:%i thr:%f %f rls:%f %f\n",p->rmsN,atkN,rlsN,threshDb,p->threshLvl,rlsDb,p->rlsLvl);
5571
+
5572
+  //for(i=0; i<p->envN; ++i)
5573
+  //  printf("%i %f\n",i,p->envV[i]);
5574
+  //printf("\n");
5575
+  
5576
+  return cmOkRC;
5577
+}
5578
+
5579
+cmRC_t      cmExpanderFinal( cmExpander* p )
5580
+{  return cmOkRC; }
5581
+
5582
+cmRC_t      cmExpanderExec( cmExpander* p, cmSample_t* x, cmSample_t* y, unsigned xyN )
5583
+{
5584
+  unsigned i;
5585
+
5586
+  // update the RMS buffer
5587
+  for(i=0; i<xyN; ++i)
5588
+  {
5589
+    // NOTE: using abs() instead of pow(x,2)
5590
+    p->rmsV[p->rmsIdx] = abs(x[i]);
5591
+
5592
+    if( ++p->rmsIdx >= p->rmsN )
5593
+      p->rmsIdx = 0;
5594
+  }
5595
+
5596
+  // calculate the RMS
5597
+  double rms = cmVOR_Mean(p->rmsV,p->rmsN);
5598
+  
5599
+  // update the duration that the signal has been above the threshold 
5600
+  if( rms > p->threshLvl )
5601
+    p->threshIdx += 1;
5602
+  else
5603
+    p->threshIdx = 0;
5604
+
5605
+  // begin the atk phase?
5606
+  if( p->threshIdx > p->threshN && p->envIdx >= p->envN )
5607
+  {
5608
+    p->envIdx = 0;
5609
+  }
5610
+
5611
+  // update the output
5612
+  if( p->envIdx >= p->envN )
5613
+  {
5614
+    if( y != NULL )
5615
+      cmVOS_Copy(y,xyN,x);   
5616
+    
5617
+  }
5618
+  else
5619
+  {
5620
+    if( y == NULL )
5621
+      y = x;
5622
+
5623
+    for(i=0; i<xyN && p->envIdx<p->envN; ++i,++p->envIdx)
5624
+      y[i] = p->envV[p->envIdx] * x[i];
5625
+  }
5626
+ 
5627
+  return cmOkRC;
5628
+}
5629
+
5630
+cmRC_t cmExpanderExecD( cmExpander* p, double* x, double* y, unsigned xyN )
5631
+{
5632
+  unsigned i;
5633
+
5634
+  // update the RMS buffer
5635
+  for(i=0; i<xyN; ++i)
5636
+  {
5637
+    // NOTE: using abs() instead of pow(x,2)
5638
+    p->rmsV[p->rmsIdx] = x[i];
5639
+
5640
+    p->rmsIdx += 1;
5641
+
5642
+    if( p->rmsIdx >= p->rmsN )
5643
+      p->rmsIdx = 0;
5644
+  }
5645
+
5646
+  // calculate the RMS
5647
+  p->rmsValue = cmVOR_Mean(p->rmsV,p->rmsN);  
5648
+
5649
+  // update the duration that the signal has been above the threshold 
5650
+  if( p->rmsValue > p->threshLvl )
5651
+    p->threshIdx += 1;
5652
+  else
5653
+    p->threshIdx = 0;
5654
+
5655
+  // begin the atk phase?
5656
+  if( p->threshIdx > p->threshN && p->envIdx >= p->envN )
5657
+  {
5658
+    p->envIdx = 0;
5659
+    p->atkCnt += 1;
5660
+  }
5661
+
5662
+  /*
5663
+  if( p->envIdx >= p->envN )
5664
+    p->gain = 1.0;
5665
+  else
5666
+  {
5667
+    p->gain = p->envV[p->envIdx];
5668
+
5669
+    p->envIdx += 1;    
5670
+  }
5671
+  */
5672
+
5673
+  // update the output
5674
+  if( p->envIdx >= p->envN )
5675
+  {
5676
+    if( y != NULL )
5677
+      cmVOD_Copy(y,xyN,x);   
5678
+  }
5679
+  else
5680
+  {
5681
+    if( y == NULL )
5682
+      y = x;
5683
+
5684
+    for(i=0; i<xyN && p->envIdx<p->envN; ++i,++p->envIdx)
5685
+      y[i] = p->envV[p->envIdx] * x[i];
5686
+  }
5687
+
5688
+  return cmOkRC;
5689
+}
5690
+
5691
+
5692
+//=======================================================================================================================
5693
+cmExpanderBank* cmExpanderBankAlloc( cmCtx* c, cmExpanderBank* p, unsigned bandN, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs )
5694
+{
5695
+  cmExpanderBank* op = cmObjAlloc(cmExpanderBank,c,p);
5696
+  
5697
+  if( bandN > 0 )  
5698
+    if( cmExpanderBankInit(op,bandN,srate, procSmpCnt, threshDb, rlsDb, threshMs, rmsMs, atkMs, rlsMs) != cmOkRC )
5699
+      cmExpanderBankFree(&op);
5700
+
5701
+  return op;
5702
+
5703
+}
5704
+
5705
+cmRC_t      cmExpanderBankFree(  cmExpanderBank** pp )
5706
+{
5707
+  cmRC_t rc = cmOkRC;
5708
+  if( pp==NULL || *pp==NULL )
5709
+    return rc;
5710
+
5711
+  cmExpanderBank* p = *pp;
5712
+  if((rc = cmExpanderBankFinal(p)) != cmOkRC )
5713
+    return rc;
5714
+
5715
+  cmMemFree(p->b);
5716
+  cmObjFree(pp);
5717
+  return rc;
5718
+
5719
+}
5720
+
5721
+cmRC_t      cmExpanderBankInit( cmExpanderBank* p, unsigned bandN, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs )
5722
+{
5723
+  cmRC_t rc;
5724
+  unsigned i;
5725
+
5726
+  if((rc = cmExpanderBankFinal(p)) != cmOkRC )
5727
+    return rc;
5728
+  
5729
+  p->bandN = bandN;
5730
+  p->b = cmMemResizeZ(cmExpander*,p->b,p->bandN);
5731
+
5732
+  for(i=0; i<bandN; ++i)
5733
+    p->b[i] = cmExpanderAlloc(p->obj.ctx,NULL,srate, procSmpCnt,threshDb,rlsDb,threshMs,rmsMs,atkMs,rlsMs);
5734
+  
5735
+  return cmOkRC;
5736
+}
5737
+
5738
+cmRC_t      cmExpanderBankFinal( cmExpanderBank* p )
5739
+{ 
5740
+  unsigned i;
5741
+  for(i=0; i<p->bandN; ++i)
5742
+    cmExpanderFree(&p->b[i]);
5743
+
5744
+  return cmOkRC; 
5745
+}
5746
+
5747
+cmRC_t      cmExpanderBankExec( cmExpanderBank* p, cmSample_t* x, unsigned bandN )
5748
+{
5749
+  assert( bandN <= p->bandN);
5750
+  unsigned i;
5751
+  for(i=0; i<bandN; ++i)
5752
+  {
5753
+    cmExpanderExec(p->b[i],&x[i],NULL,1);
5754
+  }
5755
+
5756
+  return cmOkRC;
5757
+}
5758
+
5759
+/*
5760
+cmRC_t      cmExpanderBankExecD( cmExpanderBank* p, double* x, unsigned binN )
5761
+{
5762
+  unsigned i;
5763
+  p->rmsValue = 0;
5764
+  for(i=0; i<p->bandN; ++i)
5765
+  {
5766
+    
5767
+    double sum = cmVOD_Sum(x,binN);
5768
+    
5769
+    cmExpanderExecD(p->b[i],&sum,NULL,1);
5770
+
5771
+    //printf("%f %f\n",sum, p->b[i]->rmsValue);
5772
+
5773
+    p->rmsValue += p->b[i]->rmsValue;    
5774
+    
5775
+    cmVOR_MultVS(x,binN,p->b[i]->gain);
5776
+
5777
+  }
5778
+
5779
+  p->rmsValue /= p->bandN;
5780
+
5781
+  return cmOkRC;
5782
+}
5783
+*/
5784
+
5785
+cmRC_t      cmExpanderBankExecD( cmExpanderBank* p, double* x, unsigned bandN )
5786
+{
5787
+  unsigned i;
5788
+  //unsigned n = 3;
5789
+  //unsigned no2 = n/2;
5790
+  double xx;
5791
+  p->rmsValue = 0;
5792
+  p->atkCnt = 0;
5793
+  for(i=0; i<p->bandN; ++i)
5794
+  {    
5795
+    unsigned atkCnt = p->b[i]->atkCnt;
5796
+    
5797
+    //if( i >= no2 && i < bandN-no2 )
5798
+      //  xx = cmVOR_Mean(x-no2,n);
5799
+    //else
5800
+      xx = x[i];
5801
+
5802
+    cmExpanderExecD(p->b[i],&xx,NULL,1);
5803
+
5804
+    p->rmsValue += p->b[i]->rmsValue;    
5805
+
5806
+    p->atkCnt += p->b[i]->atkCnt != atkCnt;
5807
+  }
5808
+
5809
+  p->rmsValue /= p->bandN;
5810
+
5811
+  return cmOkRC;
5812
+}
5813
+
5497 5814
 //------------------------------------------------------------------------------------------------------------
5498 5815
 cmSpecDist_t* cmSpecDistAlloc( cmCtx* ctx,cmSpecDist_t* ap, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopFcmt, unsigned olaWndTypeId  )
5499 5816
 {
5500 5817
   cmSpecDist_t* p = cmObjAlloc( cmSpecDist_t, ctx, ap );
5501 5818
 
5502 5819
   //p->iSpecVa   = cmVectArrayAlloc(ctx,kRealVaFl);
5503
-  //p->oSpecVa   = cmVectArrayAlloc(ctx,kRealVaFl);
5820
+  p->oSpecVa   = cmVectArrayAlloc(ctx,kRealVaFl);
5504 5821
 
5505 5822
   if( procSmpCnt != 0 )
5506 5823
   {
@@ -5521,7 +5838,7 @@ cmRC_t cmSpecDistFree( cmSpecDist_t** pp )
5521 5838
   
5522 5839
   cmSpecDistFinal(p);
5523 5840
   //cmVectArrayFree(&p->iSpecVa);
5524
-  //cmVectArrayFree(&p->oSpecVa);
5841
+  cmVectArrayFree(&p->oSpecVa);
5525 5842
   cmMemPtrFree(&p->hzV);
5526 5843
   cmMemPtrFree(&p->iSpecM);
5527 5844
   cmMemPtrFree(&p->oSpecM);
@@ -5534,7 +5851,7 @@ cmRC_t cmSpecDistFree( cmSpecDist_t** pp )
5534 5851
 
5535 5852
 cmRC_t cmSpecDistInit( cmSpecDist_t* p, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopFcmt, unsigned olaWndTypeId  )
5536 5853
 {
5537
-  cmFrqTrkArgs_t fta;
5854
+  //cmFrqTrkArgs_t fta;
5538 5855
 
5539 5856
   cmRC_t rc;
5540 5857
   if((rc = cmSpecDistFinal(p)) != cmOkRC )
@@ -5557,7 +5874,7 @@ cmRC_t cmSpecDistInit( cmSpecDist_t* p, unsigned procSmpCnt, double srate, unsig
5557 5874
 
5558 5875
   p->pva = cmPvAnlAlloc(  p->obj.ctx, NULL, procSmpCnt, srate, wndSmpCnt, p->hopSmpCnt, flags );
5559 5876
   p->pvs = cmPvSynAlloc(  p->obj.ctx, NULL, procSmpCnt, srate, wndSmpCnt, p->hopSmpCnt, olaWndTypeId );
5560
-
5877
+  /*
5561 5878
   fta.srate         = srate;
5562 5879
   fta.chCnt         = 50;       
5563 5880
   fta.binCnt        = p->pva->binCnt;
@@ -5580,18 +5897,35 @@ cmRC_t cmSpecDistInit( cmSpecDist_t* p, unsigned procSmpCnt, double srate, unsig
5580 5897
   fta.levelFn       = "/home/kevin/temp/frqtrk/level.va";
5581 5898
   fta.specFn        = "/home/kevin/temp/frqtrk/spec.va";
5582 5899
   fta.attenFn       = "/home/kevin/temp/frqtrk/atten.va";
5900
+  */
5583 5901
 
5584 5902
   //p->ft  = cmFrqTrkAlloc( p->obj.ctx, NULL, &fta );
5585 5903
 
5904
+  /*
5586 5905
   cmFbCtlArgs_t fba;
5587 5906
   fba.srate = srate;
5588 5907
   fba.binCnt = p->pva->binCnt;
5589 5908
   fba.hopSmpCnt = p->hopSmpCnt;
5590 5909
   fba.bufMs = 500;
5591 5910
   fba.maxHz = 5000;
5911
+  */
5592 5912
 
5593 5913
   //p->fbc  = cmFbCtlAlloc( p->obj.ctx, NULL, &fba );
5594 5914
 
5915
+
5916
+  //unsigned expBandN  = 1; //
5917
+  unsigned expBandN  = 20000.0 / (p->srate / p->pva->binCnt);
5918
+  double   expSrate      = p->pva->hopSmpCnt / srate;
5919
+  unsigned expProcSmpCnt = 1; 
5920
+  double expThreshDb = -80.0; 
5921
+  double expRlsDb    = -18.0; 
5922
+  double expThreshMs = 250.0; 
5923
+  double expRmsMs    =  100.0; 
5924
+  double expAtkMs    =  25.0; 
5925
+  double expRlsMs    = 1000.0;
5926
+
5927
+  p->exb = cmExpanderBankAlloc( p->obj.ctx, NULL, expBandN, expSrate, expProcSmpCnt, expThreshDb, expRlsDb, expThreshMs, expRmsMs, expAtkMs, expRlsMs );
5928
+
5595 5929
   p->spcBwHz   = cmMin(srate/2,10000);
5596 5930
   p->spcSmArg  = 0.05;
5597 5931
   p->spcMin    = p->spcBwHz;
@@ -5628,12 +5962,13 @@ cmRC_t cmSpecDistFinal(cmSpecDist_t* p )
5628 5962
   cmRC_t rc = cmOkRC;
5629 5963
 
5630 5964
   //cmVectArrayWrite(p->iSpecVa, "/home/kevin/temp/frqtrk/iSpec.va");
5631
-  //cmVectArrayWrite(p->oSpecVa, "/home/kevin/temp/frqtrk/oSpec.va");
5965
+  cmVectArrayWrite(p->oSpecVa, "/home/kevin/temp/expand/oSpec.va");
5632 5966
 
5633 5967
   cmPvAnlFree(&p->pva);
5634 5968
   cmPvSynFree(&p->pvs);
5635 5969
   //cmFrqTrkFree(&p->ft);
5636 5970
   //cmFbCtlFree(&p->fbc);
5971
+  cmExpanderBankFree(&p->exb);
5637 5972
   return rc;
5638 5973
 }
5639 5974
 
@@ -5949,7 +6284,17 @@ cmRC_t  cmSpecDistExec( cmSpecDist_t* p, const cmSample_t* sp, unsigned sn )
5949 6284
 
5950 6285
       p->hi = (p->hi + 1) % p->hN;
5951 6286
     }
6287
+
6288
+    //unsigned binN = 12500.0 / (p->srate / p->pva->binCnt);
6289
+    //cmExpanderBankExecD(p->exb, X1m, binN );
5952 6290
     
6291
+    /*
6292
+    cmExpanderBankExecD(p->exb, X1m, p->exb->bandN );
6293
+
6294
+    cmReal_t mean = cmVOR_Mean(X1m,p->exb->bandN);
6295
+    cmReal_t arr[3] = { p->exb->rmsValue, mean, p->exb->atkCnt };
6296
+    cmVectArrayAppendR(p->oSpecVa,arr,3);
6297
+    */
5953 6298
 
5954 6299
     cmPvSynExec(p->pvs, X1m, p->pva->phsV );
5955 6300
   

+ 46
- 0
cmProc2.h 파일 보기

@@ -1054,6 +1054,51 @@ extern "C" {
1054 1054
   cmRC_t     cmFbCtlFinal(cmFbCtl_t* p );
1055 1055
   cmRC_t     cmFbCtlExec( cmFbCtl_t* p, const cmReal_t* xV );
1056 1056
 
1057
+  //-----------------------------------------------------------------------------------------------------------------------
1058
+
1059
+  typedef struct
1060
+  {
1061
+    cmObj obj;
1062
+    cmReal_t* rmsV;  // rmsV[rmsN]
1063
+    unsigned    rmsN;  // 
1064
+    unsigned    rmsIdx;//
1065
+    cmReal_t    rmsValue; // last RMS value
1066
+    cmSample_t* envV;  // envV[envN]
1067
+    unsigned    envN;  // atkSmp + rlsSmp;
1068
+    unsigned    threshN;
1069
+    unsigned    threshIdx;
1070
+    float       threshLvl;
1071
+    float       rlsLvl;
1072
+    unsigned    envIdx;
1073
+    double      gain;
1074
+    unsigned    atkCnt;
1075
+  } cmExpander;
1076
+  
1077
+  cmExpander* cmExpanderAlloc( cmCtx* c, cmExpander* p, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
1078
+  cmRC_t      cmExpanderFree(  cmExpander** pp );
1079
+  cmRC_t      cmExpanderInit( cmExpander* p, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
1080
+  cmRC_t      cmExpanderFinal( cmExpander* p );
1081
+  cmRC_t      cmExpanderExec( cmExpander* p, cmSample_t* x, cmSample_t* y, unsigned xyN );
1082
+  cmRC_t      cmExpanderExecD( cmExpander* p, double* x, double* y, unsigned xyN );
1083
+  //-----------------------------------------------------------------------------------------------------------------------
1084
+  typedef struct
1085
+  {
1086
+    cmObj obj;
1087
+    cmExpander** b;   // b[bandN]
1088
+    unsigned    bandN; 
1089
+    double      rmsValue;
1090
+    unsigned    atkCnt;
1091
+  } cmExpanderBank;
1092
+
1093
+
1094
+  cmExpanderBank* cmExpanderBankAlloc( cmCtx* c, cmExpanderBank* p, unsigned bandN, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
1095
+  cmRC_t      cmExpanderBankFree(  cmExpanderBank** pp );
1096
+  cmRC_t      cmExpanderBankInit(  cmExpanderBank* p, unsigned bandN, double srate, unsigned procSmpCnt, double threshDb, double rlsDb, double threshMs, double rmsMs, double atkMs, double rlsMs );
1097
+  cmRC_t      cmExpanderBankFinal( cmExpanderBank* p );
1098
+  cmRC_t      cmExpanderBankExec(  cmExpanderBank* p, cmSample_t* x, unsigned bandN );
1099
+  cmRC_t      cmExpanderBankExecD(  cmExpanderBank* p, double* x, unsigned bandN );
1100
+  
1101
+
1057 1102
   //------------------------------------------------------------------------------------------------------------
1058 1103
 
1059 1104
   enum
@@ -1080,6 +1125,7 @@ extern "C" {
1080 1125
 
1081 1126
     cmFrqTrk* ft;
1082 1127
     cmFbCtl_t*  fbc;
1128
+    cmExpanderBank* exb;
1083 1129
 
1084 1130
     unsigned mode;
1085 1131
     double   thresh;

Loading…
취소
저장