SyntaxBomb - Indie Coders

Languages & Coding => Blitz Code Archives => Audio => Topic started by: BlitzBot on June 29, 2017, 12:28:41 AM

Title: [bmx] (BMX) Synthesize a simple sound sample by Perturbatio [ 1+ years ago ]
Post by: BlitzBot on June 29, 2017, 12:28:41 AM
Title : (BMX) Synthesize a simple sound sample
Author : Perturbatio
Posted : 1+ years ago

Description : It's very basic but it works, includes a primitive waveform drawing function

Code :
Code: BlitzMax
  1. SuperStrict
  2. Graphics 1024,768,0,0
  3.  
  4. Global Quit:Int = False
  5. Const SampleSize:Int = 100000
  6. Global sample:TAudioSample = TAudioSample.Create(SampleSize, 44100, SF_STEREO16BE)
  7. Global sampleData:Byte[SampleSize]
  8.  
  9. Print sample.length
  10. Print getSampleLength(sample)
  11.  
  12. Function getSampleLength:Float(sample:TAudioSample)
  13.         Return Float( (sample.length / Float( (sample.hertz * 60) ) ) * 60)
  14. End Function
  15.  
  16.  
  17.  
  18. Function drawWave(sample:TAudioSample)
  19.         Local x:Int, y:Int
  20.         Local xscale:Double = sample.length/GraphicsWidth()
  21.         For x = 0 Until sample.length Step 100
  22.                 y = sample.samples[x]
  23.                 If (y > 0) Then
  24.                         SetColor(255,y,y)
  25.                         DrawLine((x/xscale)-1, sample.samples[x-1], x/xscale, y)
  26.                         'DrawLine((x/xscale)-1, Abs(sample.samples[x-1]-255)+255, x/xscale, Abs(y-255)+255)
  27.                 End If
  28.         Next
  29. End Function
  30.  
  31. SeedRnd MilliSecs()
  32.  
  33. For Local m:Int = 0 Until SampleSize
  34.         sample.samples[m] = Sin(m)*128
  35.         If (m Mod 1000) > 900 Then sample.samples[m] = 100
  36. Next
  37.  
  38. Local sound:TSound = LoadSound(sample)
  39. Local channel:TChannel = PlaySound(sound)
  40.  
  41. While Not Quit
  42.         Cls
  43.         drawWave(sample)
  44.         If channel.playing() Then
  45.                 SetColor(255,0,0)
  46.                 DrawText("Playing", 10, 300)
  47.         Else
  48.                 SetColor(255,255,255)
  49.                 DrawText("Not Playing (Press space)", 10, 300)
  50.         EndIf
  51.        
  52.         Flip
  53.         If KeyDown(KEY_SPACE) And Not channel.playing() Then channel = PlaySound(sound)
  54.         If KeyDown(KEY_ESCAPE) Then quit = True
  55. Wend
  56. End


Comments :


ImaginaryHuman(Posted 1+ years ago)

 Is there a way to get this to play a looping sound which you can modify, so that you have one long endless sound that is being generated as it is played, like using a long sound buffer and modifying the part that isn't playing (like a double buffer)?


Perturbatio(Posted 1+ years ago)

 I was wondering if there was a way to access the data that the TSound object uses (I presume it makes a copy of the data when you use loadsound on the TAudioSample.  A bit of investigation may be in order (but not right now since I'm not at home).

SimplePortal 2.3.6 © 2008-2014, SimplePortal