March 05, 2021, 07:45:52 AM

Author Topic: [bmx] TWaveform, TCompositeWaveform by tesuji [ 1+ years ago ]  (Read 419 times)

Offline BlitzBot

Title : TWaveform, TCompositeWaveform
Author : tesuji
Posted : 1+ years ago

Description : Waveforms can be used wherever there's a need for oscillating data - eg platform height
Complex waveforms can be formed by blending multiple simple waveforms together aka analog waveform synthesis
Basic waves catered for are Sine, Triangle & Square


Code :
Code: BlitzMax
  1. ' ------------------------------------------
  2. ' TWaveform, TCompositeWaveform example
  3. ' Waveforms can be used wherever there's a need for oscillating data - eg platform height
  4. ' Complex waveforms can be formed by blending multiple simple waveforms together aka analog waveform synthesis
  5. ' x axis range 0..1 y axis range -1..+1
  6. ' Tesuji 2010
  7. ' ------------------------------------------
  8.  
  9. SuperStrict
  10.  
  11. Graphics 1024,768,32
  12.  
  13. Local blobImage:TImage = CreateBlobImage(32)
  14. MidHandleImage blobImage
  15.  
  16. ' setup waveform examples
  17.  
  18. ' basic sine wave
  19. Local wave:TCompositeWaveform = TCompositeWaveform.Create( [TWaveform.Create(TWaveform.SINE)] )
  20. Local blob1:TTestBlob = TTestBlob.Create(0, wave, blobImage, 0,128,255)
  21.  
  22. wave = TCompositeWaveform.Create( ..
  23.         [ ..
  24.                 TWaveform.Create(TWaveform.SINE, 16), ..
  25.                 TWaveform.Create(TWaveform.TRI, 3) ..
  26.     ] ..
  27. )
  28. Local blob2:TTestBlob = TTestBlob.Create(1, wave, blobImage, 255,128,0)
  29.  
  30. wave = TCompositeWaveform.Create( ..
  31.         [ ..
  32.                 TWaveform.Create(TWaveform.SINE), ..
  33.                 TWaveform.Create(TWaveform.TRI, 4), ..
  34.                 TWaveform.Create(TWaveform.SQUARE, 2) ..
  35.     ] ..
  36. )
  37. Local blob3:TTestBlob = TTestBlob.Create(2, wave, blobImage, 64,255,64)
  38.  
  39. wave = TCompositeWaveform.Create( [TWaveform.Create(TWaveform.SINE, 3), TWaveform.Create(TWaveform.SINE, 4)] )
  40. Local blob4:TTestBlob = TTestBlob.Create(3, wave, blobImage, 196,196,0)
  41.  
  42. Local x:Int = 0
  43.  
  44. While Not KeyHit(KEY_ESCAPE)
  45.  
  46.         ' motion blur cls
  47.         SetBlend ALPHABLEND
  48.         SetColor 0,0,0
  49.         SetAlpha .075
  50.         If KeyDown(KEY_SPACE) Then SetAlpha .02
  51.         DrawRect 0,0,GraphicsWidth(),GraphicsHeight()
  52.  
  53.         SetColor 0,0,0
  54.         SetBlend ALPHABLEND
  55.         SetAlpha .5
  56.         DrawRect 0,0,GraphicsWidth(),64
  57.  
  58.         blob1.render(x Mod GraphicsWidth())
  59.         blob2.render((x+(GraphicsWidth()*.25)) Mod GraphicsWidth())
  60.         blob3.render((x+(GraphicsWidth()*.5)) Mod GraphicsWidth())
  61.         blob4.render((x+(GraphicsWidth()*.75)) Mod GraphicsWidth())
  62.  
  63.         x :+ 2
  64.        
  65.         Flip
  66.  
  67. Wend
  68.  
  69. End
  70.  
  71. Function createBlobImage:TImage(size:Int=48)
  72.         Cls
  73.         Local cm:Float = 256/Float(size)
  74.         For Local r:Int = 0 To size-1
  75.                 SetColor r*cm,r*cm,r*cm
  76.                 DrawOval r/2,r/2,size-r,size-r
  77.         Next
  78.         Local pixmap:TPixmap = GrabPixmap(0,0,size,size)
  79.         Return LoadImage(pixmap)        
  80. End Function
  81.  
  82. ' ------------------------------------------------------
  83. Type TTestBlob
  84.  
  85.         Field number:Int = 0
  86.         Field osc:TCompositeWaveform
  87.         Field img:TImage
  88.         Field red:Int,green:Int,blue:Int
  89.        
  90.         Function Create:TTestBlob(number:Int=0, osc:TCompositeWaveform, img:TImage, red:Int, green:Int, blue:Int)
  91.                 Local o:TTestBlob = New TTestBlob
  92.                 o.number = number
  93.                 o.osc = osc
  94.                 o.img = img
  95.                 o.red = red
  96.                 o.green = green
  97.                 o.blue = blue
  98.                 Return o
  99.         End Function
  100.  
  101.         Method render(position:Float)
  102.  
  103.                 Local v:Float = osc.value(position*(1.0/GraphicsWidth()))
  104.                 SetAlpha .75
  105.                 SetBlend LIGHTBLEND    
  106.                 SetColor red,green,blue
  107.                 DrawImage img, position, (GraphicsHeight()*.5)+(v*(GraphicsHeight()*.5)*.5)
  108.  
  109.                 SetAlpha .5
  110.                 DrawText "oscillator "+number+" value: "+v, 0,number*16
  111.                 DrawText osc.toString(), 300,number*16
  112.  
  113.         End Method
  114.        
  115.  
  116. End Type
  117.  
  118. ' ---------------------------------------------------------------------------------------------------------
  119.  
  120. ' -------------------------------------------------------------------------
  121. ' Represents a simple waveform
  122. ' Sine, Triangle or Square wave
  123. ' frequency dictates the number of wave cycles in the time period provided
  124. ' -------------------------------------------------------------------------
  125. Type TWaveform
  126.  
  127.         Const SINE:Int = 0
  128.         Const TRI:Int = 1
  129.         Const SQUARE:Int = 2
  130.        
  131.         Field labels:String[] = ["Sine","Triangle","Square"]
  132.        
  133.         Field waveformType:Int
  134.         Field amplitude:Float = 1.0
  135.         Field frequency:Float = 1.0
  136.         Field offset:Float = 0.0
  137.  
  138.         Function Create:TWaveform(waveformType:Int, frequency:Float=1.0, amplitude:Float=1.0)
  139.                 Local w:TWaveform = New TWaveform
  140.                 w.waveformType = waveFormType
  141.                 w.frequency = frequency
  142.                 w.amplitude = amplitude
  143.                 Return w
  144.         End Function
  145.  
  146.         ' provide the waveform y position at the given x axis position
  147.         ' time should be in the range 0.0 to 1.0
  148.         ' output is given in the range of -ve amplitude to +ve amplitude
  149.        
  150.         Method value:Float(time:Float)
  151.  
  152.                 Local v:Float = 1.0
  153.                 If amplitude > 0.0
  154.                         Select waveformType
  155.                                 Case SINE v = (Sin(frequency*(time+offset)*360))*amplitude
  156.                                 Case SQUARE v = (((Ceil((time+offset) * frequency * 2) Mod 2.0)*2)-1) * amplitude
  157.                                 Case TRI
  158.                                         v = (((((time+offset+.25) * frequency * 2) Mod 1.0)*2)-1)
  159.                                         If (Ceil((time+offset+.25) * frequency * 2) Mod 2.0) = 0.0 Then v = -v
  160.                         End Select
  161.                 End If
  162.  
  163.                 Return v
  164.                
  165.         End Method
  166.        
  167.         Method toString:String()
  168.                 Return labels[waveformType] +" "+Left(frequency,3)
  169.         End Method
  170.  
  171. End Type
  172.  
  173. ' ---------------------------------------------------
  174. ' represents a blending of multiple simple waveforms
  175. ' ---------------------------------------------------
  176. Type TCompositeWaveform
  177.  
  178.         Field waveforms:TWaveform[]
  179.         Field amplitude:Float = 1.0
  180.        
  181.         Function Create:TCompositeWaveform(waveforms:TWaveform[], amplitude:Float=1.0)
  182.                 Local mw:TCompositeWaveform = New TCompositeWaveform
  183.                 mw.waveforms = waveforms
  184.                 mw.amplitude = amplitude
  185.                 Return mw
  186.         End Function
  187.        
  188.         ' provide the blended amplitude of all waveforms for the given x axis position
  189.         ' time should be in the range 0.0 to 1.0
  190.        
  191.         Method value:Float(time:Float)
  192.        
  193.                 Local v:Float = 1.0
  194.                 For Local w:TWaveform = EachIn waveforms
  195.                         v :* w.value(time)
  196.                 Next
  197.                
  198.                 Return v*amplitude
  199.        
  200.         End Method
  201.  
  202.         Method toString:String()
  203.                 Local s:String
  204.                 For Local w:TWaveform = EachIn waveforms
  205.                         s :+ "[" + w.toString() + "] "
  206.                 Next
  207.                 Return s
  208.         End Method
  209.  
  210. End Type


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal