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

Discussion in 'Code Snippets' started by Jmu5667, Aug 23, 2018.

  1. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    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

    Code:
    ' // 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: Aug 28, 2018
    Philip Prins, Ohanian, iCAB and 5 others like this.
  2. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    This is an update if you want to amply the the Mic:

    Code:
    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
     
    Philip Prins, Ohanian, iCAB and 3 others like this.
  3. iCAB

    iCAB Well-Known Member Licensed 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
     
  4. Jmu5667

    Jmu5667 Well-Known Member Licensed 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.
     
  5. iCAB

    iCAB Well-Known Member Licensed User

    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
     
  6. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    Yep, best try that, good luck :)
     
    iCAB likes this.
  7. iCAB

    iCAB Well-Known Member Licensed 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
     
  8. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice