Sub Class_Globals
Private Root As B4XView 'ignore
Private xui As XUI 'ignore
Private SDL As SourceDataLineWrapper
Private OutDevice As JavaObject, AudioFormat As JavaObject, SelectedMixerInfo As JavaObject, Devices As List
Private SampleRateInHz As Float = 44100
Private SampleSizeInBits As Int = 16
Private Channels As Int = 2
Private genOff As Boolean
Dim MixBuffer(1) As Short
Dim timFreq As Timer
Dim BC As ByteConverter
End Sub
Sub gene_rate
SDL.Start
BC.LittleEndian = True
Do While genOff=False
SDL.Write(BC.ShortsToBytes(MixBuffer),0,MixBuffer.Length * 2)
Loop
SDL.Flush
SDL.Stop
End Sub
Sub Prepare_Data1
Dim Freq1 As Int = IIf(IsNumber(txtLeftFreq.Text), txtLeftFreq.Text, 440)
Dim Freq2 As Int = IIf(IsNumber(txtRightFreq.Text), txtRightFreq.Text, 1000)
Dim Level1 As Double = IIf(chkMuteLeft.Checked,0, volLeft.Value / 100)
Dim Level2 As Double = IIf(chkMuteRight.Checked,0, volRight.Value / 100)
Dim LeftPhase As Int = IIf(chkAntiLeft.Checked, 180, 0)
Dim RightPhase As Int = IIf(chkAntiRight.Checked, 180, 0)
Dim Dur As Double = 0.5 'seconds
Dim Ampl1 As Int = 32767
Dim Ampl2 As Int = 32767
Dim L() As Short = generateSinWav(Freq1,Dur,Level1 * Ampl1, LeftPhase)
Dim R() As Short = generateSinWav(Freq2,Dur,Level2 * Ampl2, RightPhase)
Dim MixBuffer(L.Length * 2) As Short
Dim MixBufferIndex As Int = 0
For i = 0 To L.Length - 1
MixBuffer(MixBufferIndex) = L(i)
MixBuffer(MixBufferIndex + 1) = R(i)
MixBufferIndex = MixBufferIndex + 2
Next
' Log(MixBuffer.Length)
End Sub
Sub Array_to_List(arr() As Short) As List
Dim l As List
l.Initialize
For i = 0 To arr.Length - 1
l.Add(arr(i))
Next
Return l
End Sub
Public Sub generateSinWav(Freq As Int, Dur As Double, MaxVal As Double, DegreePhaseShift As Int) As Short()
Dim Arr(Dur * SampleRateInHz) As Short
Dim SampleInterval As Double = SampleRateInHz / Freq
Dim shift As Double = DegreePhaseShift / 180 * cPI
For i = 0 To Arr.Length - 1
Dim Angle As Double = (2 * cPI * i) / SampleInterval - shift
Arr(i) = Sin(Angle) * MaxVal
Next
Return Arr
End Sub
Private Sub Setup_Audio
Stop_Sound
'Setup Device names in a B$XCombobox and MixerInfo objects in a list
Dim MixerInfos As List = jAudioTrack2_Utils.GetDevices(jAudioTrack2_Utils.DEVICETYPE_OUTPUT,"")
Dim L As List
L.Initialize
Dim Devices As List
Devices.Initialize
For Each MI As JavaObject In MixerInfos
'Add the name to the list for the B4xCombobox
L.add(MI.RunMethod("getName",Null))
'Add the MixerInfo to the corresponding Devices List
Devices.Add(MI)
Next
If Devices.Size > 0 Then
Dim SelectedMixerInfo As JavaObject
SelectedMixerInfo = Devices.Get(0)
Dim AudioFormat As JavaObject
AudioFormat = jAudioTrack2_Utils.NewAudioFormat(SampleRateInHz, SampleSizeInBits, Channels, True, False)
Dim OutDevice As JavaObject
OutDevice = jAudioTrack2_Utils.GetSourceDataLine2(AudioFormat, SelectedMixerInfo)
Dim SDL As SourceDataLineWrapper
SDL.Initialize(OutDevice)
Else
others.Toast("No ready audio devices found.")
End If
End Sub