Sub GenerateSamples(NumSamples As Int) As Short()
Dim S(NumChannels * NumSamples) As Short
Dim SP As Int = 0
Dim TotalChannelSample(NumChannels) As Int
For I = 1 To NumSamples
For C = 0 To NumChannels - 1
TotalChannelSample(C) = 0
Next
For G = 0 To NumGenerators - 1
Dim WaveTable() As Short = WaveTables(G) 'cast from object wtf
PhaseAccumulator(G) = Bit.And(PhaseAccumulator(G) + PhaseIncrement(G), 0x3FFFFFFF) 'clear high two bits to avoid Int overflow
For C = 0 To NumChannels - 1
Dim WaveTableIndex As Int = Bit.ShiftRight(PhaseAccumulator(G) + PhaseShift(G, C), 16)
Dim WaveSample As Int = WaveTable(Bit.And(WaveTableIndex, WaveTableIndexMask))
Dim ChannelSample As Int = (WaveSample * Level(G, C) + 50) / 100
TotalChannelSample(C) = TotalChannelSample(C) + ChannelSample
Next
Next
For C = 0 To NumChannels - 1
Dim Temp As Int = TotalChannelSample(C)
If Temp < -32768 Then
Temp = -32768
else if Temp > 32767 Then
Temp = 32767
End If
S(SP) = Temp
SP = SP + 1 'interleaved channel eg 5.1 = 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5
Next
Next
Return S
End Sub