audio - Generate a sound - PCM (Android - Java) -
i've found here on stackoverflow.com a great example works playing sounds. works smoothly i'd know happen in pcm generation. here code:
int idx = 0; (final double dval : sample) { final short val = (short) ((dval * 32767)); generatedsnd[idx++] = (byte) (val & 0x00ff); generatedsnd[idx++] = (byte) ((val & 0xff00) >>> 8); }
where sample double array holds sine computed required parameters in case (frequency, hertz, on , forth), , generatedsnd byte array. know val & 0xff translates int byte, here precisely done? why there shift >>> 8?
you don't mention in question input function is, i'm going guess elements in sample
have range of -1.0 +1.0.
16-bit signed pcm data has range of -32768 +32767. what's happening in method each floating point sample scaled 32767 value in range -32767 +32767, truncated short
.
this short
stored in generatedsnd
(which assume byte[]
) first writing low byte
of short
(the least significant 8 bits), followed high byte (shifting short 8 bits right takes high byte , places in low byte).
Comments
Post a Comment