I'm using the Android AudioTrack object to stream a 16bit mono PCM file. My code reads a double from a DataInputStream, converts it to 8 bytes, and then saves that 8 bytes to a buffer to be written to the AudioTrack. This works fine. But now, I'm trying to adjust the gain by multiplying the double by a scalar (like 0.5). When I do this, it makes my audio terribly distorted. I have tried using floats instead of doubles, and I get the same result. Is there a better way to go about changing the gain of the stream? My ultimate goal is to create a simple echo effect, which is why I'm doing it this way.
ByteBuffer bb = ByteBuffer.allocate(8);
while(isPlaying){
try {
//fill buffer with bytes from file reader
for(int i=0; i < BUFFSIZE/8; i++){
//read double from DataInputStream
double temp = dis.readDouble();
//save double to ByteBuffer
bb.putDouble(0, temp * .5);
// save 8 bytes to array of bytes
bb.rewind();
bb.get(buff,i*8,8);
}
//write buffer to track to play
track.write(buff, 0, BUFFSIZE);
} catch (IOException e) {
break; //when eof is reached
}
}