Browse Source

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 years ago
parent
commit
64831fef9f
2 changed files with 75 additions and 27 deletions
  1. 66
    18
      dsp/cmDspBuiltIn.c
  2. 9
    9
      dsp/cmDspPgmKr.c

+ 66
- 18
dsp/cmDspBuiltIn.c View File

732
   cmDspPhasor_t* p = cmDspInstAlloc(cmDspPhasor_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
732
   cmDspPhasor_t* p = cmDspInstAlloc(cmDspPhasor_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
733
 
733
 
734
   // assign default values to any of the the optional arg's which may not have been set from vl.
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
   cmDspSetDefaultDouble(ctx, &p->inst, kPhsPhId,  0.0, 0.0);
737
   cmDspSetDefaultDouble(ctx, &p->inst, kPhsPhId,  0.0, 0.0);
738
 
738
 
739
   return &p->inst;
739
   return &p->inst;
750
 {
750
 {
751
   cmSample_t*       bp   = cmDspAudioBuf(ctx,inst,kOutPhId,0);
751
   cmSample_t*       bp   = cmDspAudioBuf(ctx,inst,kOutPhId,0);
752
   const cmSample_t* ep   = bp + cmDspAudioBufSmpCount(ctx,inst,kOutPhId,0);
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
   double            phs  = cmDspDouble(inst,kPhsPhId);
755
   double            phs  = cmDspDouble(inst,kPhsPhId);
756
-  double            inc  = mult;
756
+  cmSample_t        inc  = mult;
757
 
757
 
758
   for(; bp<ep; ++bp)
758
   for(; bp<ep; ++bp)
759
   {
759
   {
760
     while( phs >= max )
760
     while( phs >= max )
761
       phs -= max;
761
       phs -= max;
762
 
762
 
763
-    *bp = phs;
763
+    *bp = (cmSample_t)phs;
764
 
764
 
765
     phs += inc;
765
     phs += inc;
766
 
766
 
767
   }
767
   }
768
 
768
 
769
-  cmDspSetDouble(ctx,inst,kPhsPhId,phs);
769
+  cmDspSetSample(ctx,inst,kPhsPhId,phs);
770
   
770
   
771
   return kOkDspRC;
771
   return kOkDspRC;
772
 }
772
 }
2773
   cmThreadH_t    thH;
2773
   cmThreadH_t    thH;
2774
   bool           loadFileFl;
2774
   bool           loadFileFl;
2775
   cmDspCtx_t*    ctx;
2775
   cmDspCtx_t*    ctx;
2776
-  double         phsOffs;
2777
-  double         phsLast;
2776
+  cmSample_t     phsOffs;
2777
+  cmSample_t     phsLast;
2778
   unsigned       onSymId;
2778
   unsigned       onSymId;
2779
   unsigned       offSymId;
2779
   unsigned       offSymId;
2780
   unsigned       doneSymId;
2780
   unsigned       doneSymId;
2781
   bool           useThreadFl;
2781
   bool           useThreadFl;
2782
+  unsigned       wt_oi;
2782
  } cmDspWaveTable_t;
2783
  } cmDspWaveTable_t;
2783
 
2784
 
2784
 bool _cmDspWaveTableThreadFunc( void* param);
2785
 bool _cmDspWaveTableThreadFunc( void* param);
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
 cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
3256
 cmDspRC_t _cmDspWaveTableExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
3205
 {
3257
 {
3206
   cmDspRC_t         rc       = kOkDspRC;
3258
   cmDspRC_t         rc       = kOkDspRC;
3233
   for(i=0; i<outCnt; ++i)
3285
   for(i=0; i<outCnt; ++i)
3234
   {
3286
   {
3235
     // get the wave table location
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
     // if the wt loctn is passed the end of the table
3291
     // if the wt loctn is passed the end of the table
3239
     /*
3292
     /*
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
     outV[i] = gain * p->wt[x];
3300
     outV[i] = gain * p->wt[x];
3254
   }
3301
   }
3255
 
3302
 
4951
   cmWaveTableClassCons,
4998
   cmWaveTableClassCons,
4952
 
4999
 
4953
   cmSprintfClassCons,
5000
   cmSprintfClassCons,
4954
-  cmKrClassCons,
4955
   cmAMixClassCons,
5001
   cmAMixClassCons,
4956
   cmASplitClassCons,
5002
   cmASplitClassCons,
4957
   cmAMeterClassCons,
5003
   cmAMeterClassCons,
4998
   cmGateToSymClassCons,
5044
   cmGateToSymClassCons,
4999
   cmPortToSymClassCons,
5045
   cmPortToSymClassCons,
5000
   cmRouterClassCons,
5046
   cmRouterClassCons,
5047
+  cmAvailChClassCons,
5001
 
5048
 
5002
   cmPresetClassCons,
5049
   cmPresetClassCons,
5003
   cmBcastSymClassCons,
5050
   cmBcastSymClassCons,
5004
   cmSegLineClassCons,
5051
   cmSegLineClassCons,
5005
-  
5052
+
5053
+  cmKrClassCons,  
5006
   cmTimeLineClassCons,
5054
   cmTimeLineClassCons,
5007
   cmScoreClassCons,
5055
   cmScoreClassCons,
5008
   cmMidiFilePlayClassCons,
5056
   cmMidiFilePlayClassCons,

+ 9
- 9
dsp/cmDspPgmKr.c View File

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

Loading…
Cancel
Save