Don't be sorry, it's great if you can look into it.
It might very well be me not quite grasping the finer details.
I updated your code like shown below. It runs and readpos returns what looks like valid results to me by casting the channel to a TFreeAudioChannel , but I still get no sound.
SuperStrict
Graphics 400,300
'Const AudioDriverName:String = "FreeAudio Default"
Const AudioDriverName:String = "FreeAudio Multimedia"
If Not SetAudioDriver(AudioDriverName) 'cludge around build order problems
RuntimeError "can't set ~q" + AudioDriverName + "~q Audio driver"
EndIf
?BigEndian
Const SF_STEREO16=SF_STEREO16BE
Const SF_MONO16=SF_MONO16BE
?LittleEndian
Const SF_STEREO16:Int=SF_STEREO16LE
Const SF_MONO16:Int=SF_MONO16LE
?
Type TSynth
Global timer:TTimer
Global synthlist:TList=New TList
Field sample:TAudioSample
Field channel:TChannel
Field sound:TSound
Field bits:Int
Field channels:Int
Field format:Int
Field fragsize:Int
Field writepos:Int
Function eventhook:Object(id%,data:Object,context:Object)
Local synth:TSynth
If context=timer
For synth=EachIn synthlist
synth.Poll
Next
Return Null
EndIf
End Function
Method Poll()
Local readpos:Int
Local count:Int
readpos=TFreeAudioChannel(channel).Position()/channels
If readpos=0 Return 'this channel is stopped
count=((readpos+fragsize*2-writepos)/fragsize)
DebugLog readpos
While count
Select bits
Case 8
Mix8
Case 16
Mix16
End Select
writepos=writepos+fragsize
count=count-1
Wend
End Method
' mix the next .fragsize samples into .sample at (.writepos mod .fragsize)
Method Mix8()
End Method
Method Mix16()
End Method
Method CreateSynth:TSynth(frag:Int=8192,_bits:Int=16,_channels:Int=2)
fragsize=frag
bits=_bits
channels=_channels
If bits=16
If channels=2
format=SF_STEREO16
Else
format=SF_MONO16
EndIf
Else
If channels=2
format=SF_STEREO8
Else
format=SF_MONO8
EndIf
EndIf
sample=CreateAudioSample(fragsize*4,44100,format) 'SF_MONO8)
MemClear sample.samples,sample.capacity
channel=AllocChannel()
sound=LoadSound(sample,True)
PlaySound sound,channel
If Not timer
timer=CreateTimer(100)
AddHook EmitEventHook,eventhook,timer
EndIf
synthlist.AddLast Self
Return Self
End Method
Function Close()
If timer
RemoveHook EmitEventHook,eventhook
StopTimer timer
timer=Null
EndIf
End Function
End Type
Type TSweepSynth Extends TSynth
Field rad#,radf#
Method Mix8()
Local b:Byte Ptr
Local p:Int,i:Int
p=writepos Mod (fragsize*4)
b=sample.samples+p*channels
For i=0 Until fragsize*channels
rad=rad+radf/channels
radf=radf+.00001/channels
b[i]=128+127*Sin(rad)
Next
End Method
Method Mix16()
Local s:Short Ptr
Local p:Int,i:Int
p=writepos Mod (fragsize*4)
s=Short Ptr sample.samples+p*channels
For i=0 Until fragsize*channels
rad=rad+radf/channels
radf=radf+.00001/channels
s[i]=32767*Sin(rad)
Next
End Method
Method CreateSweepSynth:TSweepSynth(frag:Int=8192,_bits:Int=16,_channels:Int=2)
CreateSynth frag,_bits,_channels
Return Self
End Method
End Type
Function CreateSweepSynth:TSynth(fragsize:Int,bits:Int,channels:Int)
Return New TSweepSynth.CreateSweepSynth(fragsize,bits,channels)
End Function
CreateSweepSynth 8192/8,16,2
While True
WaitEvent
Print "yo!"
Wend