Android Code Snippet Audio Streamer - Amplify Audio Buffer/ Microphone Boost

Hello

I recently needed to add an audio amplifier to a new PTT app we have developed. I found this code on the web and through I would share it.

https://stackoverflow.com/questions/14485873/audio-change-volume-of-samples-in-byte-array

B4X:
' // in an activity
' // slider bar values 0-30 (30 gives a smooth range)
Sub sbDataSpeakerBoost_ValueChanged (Value As Int, UserChanged As Boolean)
 
   If UserChanged Then
       APPSET.callvolumes.data_boostlevel = Value ' global var (type.type.var)     
   End If

End Sub

' // in a service

Sub udpSocket_PacketArrived (Packet As UDPPacket)

 
   ' // only play if listening
   If Not(sendingAudio) Then

       Dim data(Packet.Length) As Byte
       bc.ArrayCopy(Packet.Data,Packet.Offset,data,0,Packet.Length)   
   
       ' // amplify buffered data
       If Main.APPSET.callvolumes.data_boostlevel > 0 Then
           Dim volAdjust As Float
           ' // we need to create the float val for the vol adjust
           volAdjust = Main.APPSET.callvolumes.data_boostlevel/10 + 1
           ' // amplify
           Dim jo As JavaObject
           jo.InitializeContext
           audioStream.Write(jo.RunMethod("adjustVolume", Array As Object(data,volAdjust)))
       Else
           ' // play the received audio data
           audioStream.Write(data)
       End If
   
   End If
   
End Sub

#If Java

public byte[] adjustVolume(byte[] audioSamples, float volume)    
   {
        byte[] array = new byte[audioSamples.length];
        for (int i = 0; i < array.length; i+=2)
           {
               // convert byte pair to int
               short buf1 = audioSamples[i+1];
               short buf2 = audioSamples[i];

               buf1 = (short) ((buf1 & 0xff) << 8);
               buf2 = (short) (buf2 & 0xff);

               short res= (short) (buf1 | buf2);
               res = (short) (res * volume);
           
               // convert back
               array[i] = (byte) res;
               array[i+1] = (byte) (res >> 8);
           }
        return array;
   }

#End If

Regards

John
 
Last edited:

Jmu5667

Well-Known Member
Licensed User
Longtime User
This is an update if you want to amply the the Mic:

B4X:
Sub AudioStream_RecordBuffer (Data() As Byte)
   
   If sendingAudio Then
       Try
           Dim p As UDPPacket
           
           If Main.APPSET.callvolumes.data_micboostlevel > 0 Then
               Dim volAdjust As Float
               ' // we need to create the float val for the vol adjust
               volAdjust = (Main.APPSET.callvolumes.data_micboostlevel/10) + 1
               ' // adjust the volume when the data arrives
               Dim jo As JavaObject
               jo.InitializeContext
               p.Initialize(jo.RunMethod("adjustVolume", Array As Object(Data,volAdjust)),Main.APPSET.currentChannel.host,Main.APPSET.currentChannel.udp_speaker)
           Else
               p.Initialize(Data,Main.APPSET.currentChannel.host,Main.APPSET.currentChannel.udp_speaker)
           End If
           ' // send the audio to the PTT server
           udpSckTalk.Send(p)
       Catch
           mod_functions.writelog("svc_data(), AudioStream_RecordBuffer, error " & LastException.Message)
           StopSendingAudio
       End Try
   End If
   
End Sub
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hey Guys

I am wondering if there is a similar thing that can be applied to MP3/MP4 files to amplify the speaker output.

Thanks
iCAB
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hey Guys

I am wondering if there is a similar thing that can be applied to MP3/MP4 files to amplify the speaker output.

Thanks
iCAB
The examples I provided only work on PCM data directly from the MIC etc. It does not refer to the playing of files. You should check the app store to see if someone else has done this, and I'm sure they have.

Sorry I could not be of further help.
 

iCAB

Well-Known Member
Licensed User
Longtime User
The examples I provided only work on PCM data directly from the MIC etc. It does not refer to the playing of files. You should check the app store to see if someone else has done this, and I'm sure they have.

Sorry I could not be of further help.

Thanks John, I clearly understand what you are saying. I was hoping that someone out there have done it using B4X tools. Perhaps I should try to create a new thread
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hey John
I was trying to play with the above example a bit. I didn't notice a huge difference unless I am doing something wrong.
Can you please tell me what is your experience with it?

Thanks
iCAB
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hey John
I was trying to play with the above example a bit. I didn't notice a huge difference unless I am doing something wrong.
Can you please tell me what is your experience with it?

Thanks
iCAB
Hi

I am away until January of next year, play with it, it works fines, you need to write an app with a slider so you can play with it properly.
 
Top