Saturday, June 7, 2014

freeaudio streaming

or grrr bloody hell, old code should just work

The design of FreeAudio is based on limitless numbers of channel buffers being multiplexed by a very fast and simple accumulator with per sample rate and amplitude modulation.

Yesterday I failed to run some old code that wrote sample buffers during program execution.

Blitz audio is traditionally sound oriented, which is something that represents the channel that has been chosen from a limited pool, to be allocated to play your sample, which can then be modulated, or is that the channel? I always get a bit confused...

freestream.bmx

And not a few hours or is that days later on a Tuesday evening without a click on either win32 or macos the first release of freestream is ready.

' freestream.bmx
' mono 8 bit streaming example for 

Strict
Import pub.freeaudio
Const FRAG=1024
Print "freestream is free streaming..."
fa_Init(0) ' brl.freefreeaudio usually does this
Local buffer:Byte[FRAG*8]
Local writepos
Local sound
Local channel
sound=fa_CreateSound(FRAG*8,8,1,44100,buffer,$80000000)
Print "Sound:"+sound
channel=fa_PlaySound(sound,FA_CHANNELSTATUS_STREAMING,0)
Print "PlaySound:"+channel

Local streaming
Local lfo#
Local osc1#

While True
' Print "Status:"+fa_ChannelStatus( channel ) 
 Local readpos=fa_ChannelPosition( channel )
 Local write=readpos+FRAG*4-writepos
 Local frags=write/FRAG 
 While frags>0 
  Print "Write to "+writepos
  Local pos=writepos Mod (FRAG*8)
  For Local f=0 Until frag    
   Local t=writepos+f 
   lfo=Sin(0.001*t)
   osc1=Sin(t*(lfo+2))
   buffer[pos+f]=128+10*lfo*osc1
  Next
  writepos:+FRAG
  frags:-1
 Wend
 
 If Not streaming And writepos>=FRAG*4
  fa_SetChannelPaused( channel, False )
  streaming=True
 EndIf
 
 Print "." 

 Delay 50
Wend

The variable lfo refers to a low frequency oscillator, as BlitzMax is using degrees here the multiplier .001 is effectively 1 thousandth of a degree per 1/44100 seconds the suspected hz is ummm, errr... it seems to be oscillating at 5 seconds...

Latest version can be found here http://www.blitzbasic.com/codearcs/codearcs.php?code=3129