Android Question Audiostreamer help

Arf

Well-Known Member
Licensed User
Longtime User
Hi ,
I'm just starting to work with audiostreamer so will have a few silly questions while I get going.
I ran the AudioStreamerTutorial, that works fine. For my application all I want to do is analyse the MIC audio for a clap sound to trigger an event, I don't want to record. So I modified the streamer_RecordBuffer event as follows, but it's not working (expectedly). I know there are many errors in approach, any pinted to what I'm doing wrong will be much appreciated.

B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
   
    'lets check the buffer for average and peak volumes
    AvgVol = 0
    PeakVol = 0
    NumSamps = 0
   
    For Each b As Byte In Buffer
        AvgVol = AvgVol + Abs(b)
        NumSamps = NumSamps + 1
        If Abs(b) > PeakVol Then
            PeakVol = Abs(b)
        End If
    Next
   
    AvgVol = AvgVol/NumSamps
   
    'collect the recording data
    'buffers.Add(Buffer)        - I don't want to record the audio so commented this out
End Sub

When running this, the program never leaves the " For Each b As Byte In Buffer" loop, thats problem number one.
I changed "For Each b() As Byte In Buffer" to "For Each b As.." as it wouldn't compile with the (), so that's probably why but I am not sure how I can looop through Buffer in the manner I wish if this doesn't work.
Also, do I need to concact each two subsequent bytes together to form a 16 bit value?

Thanks.
 

Arf

Well-Known Member
Licensed User
Longtime User
Also, I changed the sample rate down to 8000Hz. I see in the RecordBuffer event the Buffer has 768 bytes in it, and if I do record the Buffer into buffer by uncommenting the last line in that sub, when breaking on the TimerTick at 1 second after start to record, there are 25 sets of 768 bytes in buffer.
So, 25 x 768 = 19200, divide by two (assuming two byte make up an Int) = 9600.

So is the sampling rate actually 9600, or what am I looking at?
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Thanks, I've got it working now. The 9600 bytes in the buffer when I expected 8000 was just because it was the first Tick I was breaking, on, in subsequent ones there were indeed 8000sps.
 
Upvote 0

robertyoungs

Member
Licensed User
Longtime User
Hi Arf, been working a similar project to detect mechanical clock ticks, but can't seem to get sensible conversion to shorts from bytes - don't suppose you'd be prepared to share your working version of streamer_RecordBuffer to help me on my way?
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Sure, here you go:

B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
    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    

End Sub
 
Upvote 0
Top