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
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Regards
John
			
			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 IfRegards
John
			
				Last edited: 
			
		
	
							 
				 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		