Android Question --> Help New to B4A: Write Left/Right Audio Bytes

Bill Jensen

New Member
Licensed User
Hello,

I just bought my B4A, and my first program I want to Write the individual Low and High Byte pairs to the Stereo Sound/Audio channels, and Read the byte pairs from the AC coupled Microphone Input. I read that this can be done with audioStreamer Library calls which I have been muddling through for 3 days.

What I think I know is that the Read data in a buffer from audioStreamer operations is Low Byte, followed by High Byte for the microphone AC coupled signal idling at about +2.5 volts. And the Low High bytes form a signed integer of the microphone signal value sampled. No idea if the right and left byte pairs are interleaved back in forth in the audio buffer writing to the L/R speakers?

I'm really confused about B4A arrays and accessing the bytes in them with indexes, and how to write 1 byte at a time with: "streamer.Write(b)" in the example.

Any ideas would be Greatly Appreciated. I cant even get a byte = 100 to write to the speakers with streamer.Write(100)

Sincerely,
Bill Jensen
 

ronell

Well-Known Member
Licensed User
Longtime User
pic.png

use code tags when posting codes so it will be easier to read
 
Upvote 0

Bill Jensen

New Member
Licensed User
Dear Ronell, Ill try to implement your "code" dropdown here:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region



Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private streamer As AudioStreamer
    Private buffers As List
    Private timer1 As Timer
    Private recordingStart As Long
    Private j As Int
    Private s As Int
    Private t As Int
    Private z As Byte
End Sub

Sub Globals
    Dim Label1 As Label
    Dim btnPlay As Button
    Dim btnStartRecording As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    If FirstTime Then
        streamer.Initialize("streamer", 44100, True, 16, streamer.VOLUME_MUSIC)
        buffers.Initialize
        timer1.Initialize("timer1", 1000)
    End If
End Sub

Sub streamer_RecordBuffer (Buffer() As Byte)
    'collect the recording data
   
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
'    Dim j As Int
'    j = Buffer.Length/2
'    Dim intbuffer() As Short
'    'Conv.LittleEndian = True
'    'intbuffer = Conv.ShortsFromBytes(Buffer)
'  
'    For i = 0 To j - 1
'        buffers.Add(intbuffer(i))
'    Next
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
   
    buffers.Add(Buffer)
   
    'buffers.Add(120)        '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
   
End Sub

Sub btnStartRecording_Click
    buffers.Clear
    streamer.StartRecording
   
    'streamer.StopRecording'&&&&&&&&&&&&

    recordingStart = DateTime.Now
    timer1.Enabled = True
    Timer1_Tick
    btnPlay.Enabled = False
End Sub

Sub Timer1_Tick
    Label1.Text = "Recording: " & _
        Round((DateTime.Now - recordingStart) / DateTime.TicksPerSecond) & " seconds"
End Sub

Sub btnStopRecording_Click
    streamer.StopRecording
    timer1.Enabled = False
    btnPlay.Enabled = True
    Label1.Text = ""   
End Sub

Sub btnPlay_Click
   
    btnStartRecording.Enabled = False
    streamer.StartPlaying
'    For Each b() As Byte In buffers  'REMOVED&&&&&&&&&&&&&
    For i = 0 To buffers.Size - 1
        'Dim b2() As Byte = buffers.Get(i)
        Dim b2() As Byte = Type(200)
        'streamer.Write(b)    '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        streamer.Write(b2)    '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    Next
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
'    For i = 0 To buffers.Size - 1
'        Dim b2() As Byte = buffers.Get(i)
'        'Log("b2.Length = " & b2.Length)
'        streamer.Write(b2)
'    Next
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'    For z = 0 To 100 Step 4        '&&^^^^^^^^^^^^^^^^^^^^^^^
'        'For Each b() As Byte In buffers
'        'streamer.Write(b)
'        Log(b(z))
'        Log(b(z+1))
'        Log(b(z)+256*b(z+1))
'        Log(" ")
'    Next
   
    'To convert the byte buffer To signed ints Do something like this (For 16 Bit):'
     'j = b.Length / 2
     'For i = 0 To 9
     '    s = Bit.And ( b( 2 * i ), 255)
     '    t = Bit.And ( b( 2 * i + 1), 255)
    '    Log( s + Bit.ShiftLeft(t, 8) + Bit.And(t,128) * -512 )
     'Next
'    Dim buf() As List
'    buf.add(0)
'    buf.add(200)
'    For s = 0 To 10000
'        For t = 0 To 10000
'            streamer.Write(buf(0))    'Left LSB
'            streamer.Write(buf(1))    'Left MSB
'            streamer.Write(buf(0))    'Right LSB
'            streamer.Write(buf(0))    'Right MSB
'        Next
'       
'        For t = 0 To 10000
'            streamer.Write(buf(0))    'Left LSB
'            streamer.Write(buf(0))    'Left MSB
'            streamer.Write(buf(0))    'Right LSB
'            streamer.Write(buf(0))    'Right MSB
'        Next
'       
'    Next

   
    streamer.Write(Null) 'when this "message" will be processed, the player will stop.
End Sub

Sub streamer_PlaybackComplete
    Log("PlaybackComplete")
    btnStartRecording.Enabled = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0
Top