Просмотр исходного кода

Changes to phasor and wavetable DSP objects to correct problem with phase use in the wavetable.

The phasor value must be limited to a max value - the size of the wave table
otherwise the resolution of the phasor object phase counter eventually
diminishes due to the increasing value of the integer part of the
floating point value.  This will result in distortion in the wave table
output because the phase value will not increment reliably.
master
kevin 12 лет назад
Родитель
Сommit
64831fef9f
2 измененных файлов: 75 добавлений и 27 удалений
  1. 66
    18
      dsp/cmDspBuiltIn.c
  2. 9
    9
      dsp/cmDspPgmKr.c

+ 66
- 18
dsp/cmDspBuiltIn.c Просмотреть файл

@@ -732,8 +732,8 @@ cmDspInst_t*  _cmDspPhasorAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigne
732 732
   cmDspPhasor_t* p = cmDspInstAlloc(cmDspPhasor_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
733 733
 
734 734
   // assign default values to any of the the optional arg's which may not have been set from vl.
735
-  cmDspSetDefaultDouble(ctx, &p->inst, kMaxPhId,  0.0, DBL_MAX);
736
-  cmDspSetDefaultDouble(ctx, &p->inst, kMultPhId, 0.0, 1.0);
735
+  cmDspSetDefaultSample(ctx, &p->inst, kMaxPhId,  0.0, cmSample_MAX);
736
+  cmDspSetDefaultSample(ctx, &p->inst, kMultPhId, 0.0, 1.0);
737 737
   cmDspSetDefaultDouble(ctx, &p->inst, kPhsPhId,  0.0, 0.0);
738 738
 
739 739
   return &p->inst;
@@ -750,23 +750,23 @@ cmDspRC_t _cmDspPhasorExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t*
750 750
 {
751 751
   cmSample_t*       bp   = cmDspAudioBuf(ctx,inst,kOutPhId,0);
752 752
   const cmSample_t* ep   = bp + cmDspAudioBufSmpCount(ctx,inst,kOutPhId,0);
753
-  double            mult = cmDspDouble(inst,kMultPhId);
754
-  double            max  = cmDspDouble(inst,kMaxPhId);
753
+  cmSample_t        mult = cmDspSample(inst,kMultPhId);
754
+  cmSample_t        max  = cmDspSample(inst,kMaxPhId);
755 755
   double            phs  = cmDspDouble(inst,kPhsPhId);
756
-  double            inc  = mult;
756
+  cmSample_t        inc  = mult;
757 757
 
758 758
   for(; bp<ep; ++bp)
759 759
   {
760 760
     while( phs >= max )
761 761
       phs -= max;
762 762
 
763
-    *bp = phs;
763
+    *bp = (cmSample_t)phs;
764 764
 
765 765
     phs += inc;
766 766
 
767 767
   }
768 768
 
769
-  cmDspSetDouble(ctx,inst,kPhsPhId,phs);
769
+  cmDspSetSample(ctx,inst,kPhsPhId,phs);
770 770
   
771 771
   return kOkDspRC;
772 772
 }
@@ -2773,12 +2773,13 @@ typedef struct
2773 2773
   cmThreadH_t    thH;
2774 2774
   bool           loadFileFl;
2775 2775
   cmDspCtx_t*    ctx;
2776
-  double         phsOffs;
2777
-  double         phsLast;
2776
+  cmSample_t     phsOffs;
2777
+  cmSample_t     phsLast;
2778 2778
   unsigned       onSymId;
2779 2779
   unsigned       offSymId;
2780 2780
   unsigned       doneSymId;
2781 2781
   bool           useThreadFl;
2782
+  unsigned       wt_oi;
2782 2783
  } cmDspWaveTable_t;
2783 2784
 
2784 2785
 bool _cmDspWaveTableThreadFunc( void* param);
@@ -3201,6 +3202,57 @@ cmDspRC_t _cmDspWaveTableReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEv
3201 3202
 
3202 3203
 }
3203 3204
 
3205
+cmDspRC_t _cmDspWaveTableExec1(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
3206
+{
3207
+  cmDspRC_t         rc       = kOkDspRC;
3208
+  cmDspWaveTable_t* p        = (cmDspWaveTable_t*)inst;
3209
+  unsigned          mode     = cmDspSymbol(inst,kCmdWtId);
3210
+  unsigned          srcId    = cmDspUInt(inst,kShapeWtId);
3211
+
3212
+  if( mode == p->offSymId || srcId == kSilenceWtId )
3213
+  {
3214
+    cmDspZeroAudioBuf(ctx,inst,kOutWtId);
3215
+    return kOkDspRC;
3216
+  }
3217
+
3218
+  cmSample_t*       outV     = cmDspAudioBuf(ctx,inst,kOutWtId,0);
3219
+  unsigned          outCnt   = cmDspVarRows(inst,kOutWtId);
3220
+  unsigned          wtSmpCnt = cmDspUInt(inst,kLenWtId);
3221
+  double            gain     = cmDspDouble(inst,kGainWtId);
3222
+  unsigned          i;
3223
+
3224
+  // for each output sample
3225
+  for(i=0; i<outCnt; ++i)
3226
+  {
3227
+    p->wt_oi = (p->wt_oi + 1) % wtSmpCnt;
3228
+    outV[i] = gain * p->wt[p->wt_oi];
3229
+  }
3230
+
3231
+  // if we are reading from a file ...
3232
+  if( srcId == kFileWtId )
3233
+  {
3234
+    unsigned rdSmpCnt = 8192; // file read block sample count
3235
+
3236
+    p->wtn += outCnt;
3237
+
3238
+    // ... and there are rdSmpCnt avail locations in the wave table
3239
+    if( p->wtn >= rdSmpCnt )
3240
+      rc =  _cmDspWaveTableReadAudioFile(ctx, p, wtSmpCnt, rdSmpCnt );
3241
+
3242
+    // send the current audio file index
3243
+    if( p->doneFl && p->cfi < p->cfn && p->cfn <= (p->cfi + outCnt) )
3244
+      cmDspSetSymbol(ctx,inst,kDoneWtId,p->doneSymId);
3245
+    else
3246
+      cmDspSetUInt(ctx,inst,kFIdxWtId,p->cfi);
3247
+
3248
+    p->cfi += outCnt;
3249
+
3250
+  }
3251
+
3252
+  return rc;
3253
+
3254
+}
3255
+
3204 3256
 cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
3205 3257
 {
3206 3258
   cmDspRC_t         rc       = kOkDspRC;
@@ -3233,7 +3285,8 @@ cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt
3233 3285
   for(i=0; i<outCnt; ++i)
3234 3286
   {
3235 3287
     // get the wave table location
3236
-    unsigned x = fmod(phsV[i] - p->phsOffs,wtSmpCnt);
3288
+    //unsigned x = fmodf(phsV[i] - p->phsOffs,wtSmpCnt);
3289
+    unsigned x = fmodf(phsV[i],wtSmpCnt);
3237 3290
 
3238 3291
     // if the wt loctn is passed the end of the table
3239 3292
     /*
@@ -3244,12 +3297,6 @@ cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt
3244 3297
     }
3245 3298
     */
3246 3299
 
3247
-    // read the wt into the output
3248
-
3249
-    // BUG BUG BUG BUG 
3250
-    // BUG BUG BUG BUG - THIS DIVISION BY 0.5 IS HACK
3251
-    // BUG BUG BUG BUG 
3252
-    //outV[i] = 0.5 * p->wt[x];
3253 3300
     outV[i] = gain * p->wt[x];
3254 3301
   }
3255 3302
 
@@ -4951,7 +4998,6 @@ cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
4951 4998
   cmWaveTableClassCons,
4952 4999
 
4953 5000
   cmSprintfClassCons,
4954
-  cmKrClassCons,
4955 5001
   cmAMixClassCons,
4956 5002
   cmASplitClassCons,
4957 5003
   cmAMeterClassCons,
@@ -4998,11 +5044,13 @@ cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
4998 5044
   cmGateToSymClassCons,
4999 5045
   cmPortToSymClassCons,
5000 5046
   cmRouterClassCons,
5047
+  cmAvailChClassCons,
5001 5048
 
5002 5049
   cmPresetClassCons,
5003 5050
   cmBcastSymClassCons,
5004 5051
   cmSegLineClassCons,
5005
-  
5052
+
5053
+  cmKrClassCons,  
5006 5054
   cmTimeLineClassCons,
5007 5055
   cmScoreClassCons,
5008 5056
   cmMidiFilePlayClassCons,

+ 9
- 9
dsp/cmDspPgmKr.c Просмотреть файл

@@ -63,7 +63,7 @@ cmDspRC_t _cmDspSysPgm_TimeLine(cmDspSysH_t h, void** userPtrPtr )
63 63
   cmCtx_t*        cmCtx      = cmDspSysPgmCtx(h);
64 64
   cmErr_t         err;
65 65
   krRsrc_t        r;
66
-  unsigned        wtLoopCnt  = 1;                            // play once (do not loop)
66
+  unsigned        wtLoopCnt  = -1;                            // play once (do not loop)
67 67
   unsigned        wtInitMode = 0;                            // initial wt mode is 'silence'
68 68
   unsigned        wtSmpCnt   = floor(cmDspSysSampleRate(h)); // wt length == srate
69 69
 
@@ -75,7 +75,7 @@ cmDspRC_t _cmDspSysPgm_TimeLine(cmDspSysH_t h, void** userPtrPtr )
75 75
 
76 76
   cmDspInst_t* tlp  = cmDspSysAllocInst(h,"TimeLine",    "tl",  2, r.tlFn, r.audPath );
77 77
   cmDspInst_t* scp  = cmDspSysAllocInst(h,"Score",       "sc",  1, r.scFn );
78
-  cmDspInst_t* php  = cmDspSysAllocInst(h,"Phasor",      NULL,  0 );
78
+  cmDspInst_t* php  = cmDspSysAllocInst(h,"Phasor",      NULL,  1, cmDspSysSampleRate(h) );
79 79
   cmDspInst_t* wtp  = cmDspSysAllocInst(h,"WaveTable",   NULL,  4, wtSmpCnt, wtInitMode, NULL, wtLoopCnt );
80 80
   cmDspInst_t* pts  = cmDspSysAllocInst(h,"PortToSym",   NULL,  2, "on", "off" );
81 81
   cmDspInst_t* mfp  = cmDspSysAllocInst(h,"MidiFilePlay",NULL,  0 );
@@ -99,8 +99,8 @@ cmDspRC_t _cmDspSysPgm_TimeLine(cmDspSysH_t h, void** userPtrPtr )
99 99
   cmDspSysConnectAudio(h, php, "out", wtp,  "phs" );   // phs -> wt
100 100
   cmDspSysConnectAudio(h, wtp, "out", ao0p, "in"  );   // wt  -> aout0
101 101
   cmDspSysConnectAudio(h, wtp, "out", ao1p, "in" );    // wt  -> aout1
102
+ 
102 103
   cmDspSysInstallCb(   h, wtp, "fidx",tlp,  "curs", NULL); 
103
-  //cmDspSysInstallCb(   h, wtp, "fidx",prp,  "in", NULL );
104 104
 
105 105
   // start connections
106 106
   cmDspSysInstallCb(h, onb, "sym", tlp, "reset", NULL );
@@ -127,22 +127,22 @@ cmDspRC_t _cmDspSysPgm_TimeLine(cmDspSysH_t h, void** userPtrPtr )
127 127
   cmDspSysInstallCb(h, tlp, "mfn",  mfp, "fn",    NULL );
128 128
 
129 129
   // score to score follower - to set initial search location
130
-  cmDspSysInstallCb(h, scp, "sel",    sfp, "index",  NULL );
130
+  //cmDspSysInstallCb(h, scp, "sel",    sfp, "index",  NULL );
131 131
   
132 132
 
133 133
   // MIDI file player to score-follower and score - the order of connections is the same
134 134
   // as the msg transmision order from MFP
135 135
   cmDspSysInstallCb(h, mfp, "smpidx", scp, "smpidx", NULL );
136 136
   cmDspSysInstallCb(h, mfp, "d1",     scp, "d1",     NULL );
137
-  cmDspSysInstallCb(h, mfp, "d1",     sfp, "d1",     NULL );
137
+  //cmDspSysInstallCb(h, mfp, "d1",     sfp, "d1",     NULL );
138 138
   cmDspSysInstallCb(h, mfp, "d0",     scp, "d0",     NULL );
139
-  cmDspSysInstallCb(h, mfp, "d0",     sfp, "d0",     NULL );
139
+  //cmDspSysInstallCb(h, mfp, "d0",     sfp, "d0",     NULL );
140 140
   cmDspSysInstallCb(h, mfp, "status", scp, "status", NULL );
141
-  cmDspSysInstallCb(h, mfp, "status", sfp, "status", NULL );
141
+  //cmDspSysInstallCb(h, mfp, "status", sfp, "status", NULL );
142 142
 
143 143
 
144 144
   // score follower to score
145
-  cmDspSysInstallCb(h, sfp, "out",  scp, "loc", NULL );
145
+  //cmDspSysInstallCb(h, sfp, "out",  scp, "loc", NULL );
146 146
 
147 147
 
148 148
   // Printer connections
@@ -156,6 +156,6 @@ cmDspRC_t _cmDspSysPgm_TimeLine(cmDspSysH_t h, void** userPtrPtr )
156 156
 
157 157
   cmDspSysInstallCb(h, prtb, "sym", sfp, "cmd", NULL );
158 158
   cmDspSysInstallCb(h, qtb,  "sym", sfp, "cmd", NULL );
159
-
159
+  
160 160
   return rc;
161 161
 }

Загрузка…
Отмена
Сохранить