GlobalLiquidity

Untitled

Feb 20th, 2023
1,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum WavetableType
  2. {
  3.     IterationTwo = 0,
  4.     IterationThree = 1,
  5.     Both = 2
  6. }
  7.  
  8. export class WavetableGenerator
  9. {
  10.     constructor(){}
  11.  
  12.     public generateWavetable(potPos1:number, potPos2:number, potPos3:number, type: WavetableType) : number[]
  13.     {
  14.         let outputWT : number[] = new Array<number>(256)
  15.         // FRACTAL CODE STARTS HERE
  16.         let maxVal : number = 0
  17.         let maxVal2 : number = 0
  18.         let seed : number[] = new Array<number>(3)
  19.         let tempWT : number[] = new Array<number>(256)
  20.         let WTiteration2 :number[] = new Array<number>(256)
  21.         let WTiteration3 : number[] = new Array<number>(256)
  22.  
  23.         let skipCounter : number = 0      // for selecting every second array entry
  24.    
  25.    
  26.         let convPot1 : number = potPos1;
  27.         let convPot2 : number = potPos2;
  28.         let convPot3 : number = potPos3;
  29.  
  30.         for (let i: number =0; i<256; i++) {
  31.             tempWT[i]=0;
  32.             WTiteration2[256]=0;
  33.             WTiteration3[i]=0;
  34.         }
  35.  
  36.         // convert pot positions to [0.0 - 2.0]
  37.         seed[0]=(convPot1/256)*2;
  38.         seed[1]=(convPot2/256)*2;
  39.         seed[2]=(convPot3/256)*2;
  40.        
  41.        
  42.         // first iteration
  43.         tempWT[0]=seed[0]*seed[0];
  44.         tempWT[1]=seed[1]*seed[0];
  45.         tempWT[2]=seed[2]*seed[0];
  46.  
  47.         // second iteration
  48.         WTiteration2[0]=tempWT[0];
  49.         WTiteration2[1]=tempWT[1];
  50.         WTiteration2[2]=tempWT[2];
  51.         for (let i:number=3; i<256;i++) {
  52.             WTiteration2[i]=WTiteration2[i-3]*WTiteration2[1];
  53.         }
  54.        
  55.         // third iteration
  56.         WTiteration3[0]=WTiteration2[0];
  57.         WTiteration3[1]=WTiteration2[1];
  58.         WTiteration3[2]=WTiteration2[2];
  59.         WTiteration3[3]=WTiteration2[3];
  60.         WTiteration3[4]=WTiteration2[4];
  61.         WTiteration3[5]=WTiteration2[5];
  62.         WTiteration3[6]=WTiteration2[6];
  63.         WTiteration3[7]=WTiteration2[7];
  64.         WTiteration3[8]=WTiteration2[8];
  65.         for (let i : number =9; i<256; i++) {
  66.             WTiteration3[i]=WTiteration3[i-9]*WTiteration3[2];
  67.         }
  68.  
  69.         // using WT type 1 or 2 (iteration 1 or 2)
  70.         if (type !== WavetableType.Both) {
  71.            
  72.             // generate output wavetable
  73.             for (let i:number=0; i<256; i++) {
  74.                
  75.                 switch (type) {
  76.                        
  77.                     // use iteration 2
  78.                     case(WavetableType.IterationThree):
  79.                         tempWT[i]=WTiteration2[i];
  80.                         break;
  81.                        
  82.                     // use iteration 3
  83.                     case(WavetableType.IterationThree):
  84.                         tempWT[i]=WTiteration3[i];
  85.                         break;
  86.                 }
  87.                 // find the maximum value
  88.                 if (tempWT[i]>maxVal) {
  89.                     maxVal=tempWT[i];
  90.                 }
  91.             }
  92.         } else {
  93.             // using WT type 2 (both iterations A<>B)  
  94.             if (type === WavetableType.Both) {
  95.                 for (let j:number=0; j<256; j++) {
  96.                     if (j<128) {
  97.                         tempWT[j]=WTiteration3[skipCounter];
  98.                         if (tempWT[j]>maxVal) {
  99.                             maxVal=tempWT[j];
  100.                         }
  101.                     } else {
  102.                         tempWT[j]=WTiteration2[skipCounter-256];
  103.                         if (tempWT[j]>maxVal2) {
  104.                             maxVal2=tempWT[j];
  105.                         }
  106.                     }
  107.                     skipCounter++;
  108.                     skipCounter++;
  109.                 }      
  110.             } else {
  111.                 // using WT type 3 (both iterations  B><A)
  112.                 for (let j:number=0; j<256; j++) {  
  113.                     if (j<128) {
  114.                         tempWT[j]=WTiteration2[skipCounter];
  115.                         if (tempWT[j]>maxVal) {
  116.                             maxVal=tempWT[j];
  117.                         }
  118.                     } else {
  119.                         tempWT[j]=WTiteration3[skipCounter-256];
  120.                         if (tempWT[j]>maxVal2) {
  121.                             maxVal2=tempWT[j];
  122.                         }
  123.                     }
  124.                    
  125.                     skipCounter++;
  126.                     skipCounter++;
  127.                 }
  128.             }    
  129.         }
  130.         // rescale output to maximum value and write to pot array  
  131.         // for WT mode 0/1
  132.         if (type !== WavetableType.Both) {
  133.             for (let i : number =0; i<256; i++) {
  134.                 outputWT[i]= (tempWT[i]/maxVal)*256;
  135.             }
  136.         }
  137.        
  138.         if (type === WavetableType.IterationTwo) {                                              // for WT mode 2 amplify A<>B
  139.             for (let i : number=0; i<128; i++) {
  140.                 outputWT[i]= (tempWT[i]/maxVal)*256;
  141.             }
  142.             for (let i : number=128; i<256; i++) {
  143.                 outputWT[i]= (tempWT[i]/maxVal2)*256;
  144.             }
  145.         }
  146.        
  147.         if (type === WavetableType.IterationThree)  {                                             // for WT mode 2 amplify B><A
  148.             for (let i : number=0; i<128; i++) {
  149.                 outputWT[i]= (tempWT[i]/maxVal)*256;
  150.             }
  151.             for (let i : number=128; i<256; i++) {
  152.                 outputWT[i]= (tempWT[i]/maxVal2)*256;
  153.             }
  154.         }
  155.  
  156.         return outputWT;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment