Pārlūkot izejas kodu

cmVectOpsTemplateCode.h : Fixed problem in SynthImpulse which would cause it to produce noisey output.

master
kevin 8 gadus atpakaļ
vecāks
revīzija
1276770efb
1 mainītis faili ar 12 papildinājumiem un 31 dzēšanām
  1. 12
    31
      vop/cmVectOpsTemplateCode.h

+ 12
- 31
vop/cmVectOpsTemplateCode.h Parādīt failu

@@ -1891,42 +1891,23 @@ unsigned      VECT_OP_FUNC(SynthPulseCos)(  VECT_OP_TYPE* dbp, unsigned dn, unsi
1891 1891
 
1892 1892
 unsigned      VECT_OP_FUNC(SynthImpulse)(   VECT_OP_TYPE* dbp, unsigned dn, unsigned phase, double srate, double hz )
1893 1893
 {
1894
-  const VECT_OP_TYPE* dep = dbp + dn;
1895
-  double pi2 = 2.0*M_PI;
1896
-  double rps = pi2*hz/srate;
1897
-  
1898
-  double v0,v1 = fmod( rps * phase, pi2);
1899
-
1900
-  if( dbp == dep )
1901
-    return phase;
1902
-
1903
-  // the phase is set to zero when the first output should be a 1
1904
-  if( phase == 0 )
1905
-  {
1906
-    *dbp++ = 1;
1907
-    ++phase;
1908
-  }
1909
-
1910 1894
   
1911
-  while( dbp < dep )
1895
+  VECT_OP_FUNC(Zero)(dbp,dn);
1896
+  unsigned i=0;
1897
+  while(1)
1912 1898
   {
1913
-    // the phase vector will always be increasing
1914
-    // the modulus of the phase vector will wrap with frequency 'hz'
1915
-    v0 = fmod(  rps * phase++, pi2 );
1916
-
1917
-    // notice when wrapping occurs
1918
-    *dbp++ = (VECT_OP_TYPE)(v0 < v1);
1899
+    double samplesPerCycle = srate / hz;
1900
+    unsigned j = round( (srate * i + phase) / hz);
1901
+    if( j >= dn )
1902
+      break;
1919 1903
 
1920
-    v1 = v0;
1904
+    dbp[j] = 1;
1905
+    ++i;
1921 1906
   }
1922 1907
 
1923
-  // check if the next output should be a 1 
1924
-  // (this eliminates the problem of not having access to v1 on the next call to this function
1925
-  if( fmod(  rps * phase, pi2 ) < v1 )
1926
-    phase = 0;
1927
-  
1928
-  
1929
-  return phase;
1908
+  // Note that returning an integer value here loses precision
1909
+  // since j was rounded to the nearest integer.
1910
+  return j - dn;
1930 1911
 }
1931 1912
 
1932 1913
 VECT_OP_TYPE VECT_OP_FUNC(SynthPinkNoise)( VECT_OP_TYPE* dbp, unsigned n, VECT_OP_TYPE delaySmp )

Notiek ielāde…
Atcelt
Saglabāt