Search code examples
androidaudiobytepcm

Generate a sound - PCM (Android - Java)


I've found here on stackoverflow.com a great example that actually works for playing sounds. Everything works smoothly but I'd like to know what happen in PCM generation. Here is the code:

int idx = 0;
for (final double dVal : sample) {
    final short val = (short) ((dVal * 32767));

    generatedSnd[idx++] = (byte) (val & 0x00ff);
    generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}

where sample is a double array that holds a sine computed with all required parameters in this case (frequency, hertz, so on and so forth), and generatedSnd is a byte array. Everything I know is that val & 0xff translates an int into a byte, but here what is precisely done? Why do there is the shift >>> 8?


Solution

  • You don't mention in your question what the input to this function is, but I'm going to guess that the elements in sample have a range of -1.0 to +1.0.

    16-bit signed PCM data has a range of -32768 to +32767. So what's happening in this method is that each floating point sample is scaled by 32767 to get a value in the range -32767 to +32767, which is then truncated to a short.

    This short is then stored in generatedSnd (which I assume to be a byte[]) by first writing the low byteof the short (the least significant 8 bits), followed by the high byte (shifting the short 8 bits to the right takes what originally was the high byte and places it in the low byte).