Android Question [RESOLVED -STUPID MISTAKE !!] Adjust PCM Volume on the Fly

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I want to adjust the volume of the PCM audio the comes from the Audio Stream Record buffer. I found a java routine on the web:

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

My implementation is :

B4X:
Sub AudioStream_RecordBuffer (Data() As Byte)
   If sendingAudio Then

       Dim jo As JavaObject, volume As Float = 0.5
       jo.InitializeContext

       Dim p As UDPPacket   
'       p.Initialize(Data,Main.ip,17001) ' <- Existing Method works perfectly   
       p.Initialize(jo.RunMethod("adjustVolume", Array As Object(Data,volume)),Main.ip,17001)   ' <- New method, adjust audio

       udpSckTalk.Send(p)
   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


when I compile I get the following:

B4X:
B4A Version: 8.30
Parsing code.    (0.01s)
Compiling code.    (0.29s)
Compiling layouts code.    (0.01s)
Organizing libraries.    (0.00s)
Generating R file.    (0.26s)
Compiling debugger engine code.    (0.98s)
Compiling generated Java code.    Error
B4A line: 123
Log(Data)
javac 1.8.0_40-ea
src\b4a\example\walkietalkie\connector.java:235: error: no suitable method found for NumberToString(byte[])
anywheresoftware.b4a.keywords.Common.Log(BA.NumberToString(_data));
                                           ^
    method BA.NumberToString(double) is not applicable
      (argument mismatch; byte[] cannot be converted to double)
    method BA.NumberToString(float) is not applicable
      (argument mismatch; byte[] cannot be converted to float)
    method BA.NumberToString(int) is not applicable
      (argument mismatch; byte[] cannot be converted to int)
    method BA.NumberToString(long) is not applicable
      (argument mismatch; byte[] cannot be converted to long)
    method BA.NumberToString(Number) is not applicable
      (argument mismatch; byte[] cannot be converted to Number)


any ideas ?

Regards

John.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
If you resolved the issue, it might be useful to post what the resolution is. That way if other members are having the same issue they can get an idea of how to fix it. :)

- Colin.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, so this is the code I got to work with the java routine:
B4X:
Dim volume As Float = 4.0
Dim jo As JavaObject
Dim B() As Byte

jo.InitializeContext
B = jo.RunMethod("adjustVolume", Array As Object(Buffer,volume))          
RecOutput.WriteBytes(B, 0, B.Length)
 
Last edited:
Upvote 0
Top