Question

I'm getting PCM audio samples in Float32Arrays. But my application can only support data with a 16 bit length so I need to convert.

I'm not sure how to do this since there is no typed array for a Float16Array and I've never worked with data on the bit level. Could someone explain or show how this could be done?

Était-ce utile?

La solution

Firstoff, JavaScript is dynamically typed and has no concept of type length / size.

It is a matter of the runtime what type sizes may be used internally on the native platform, but that does not necessarily limit the possible values i.e the emulated type length if you will, inside the JavaScript runtime environment.

Also, probably you need 16 bit int to pass it to an audio device. Would make more sense.

So, I would narrow down your question to: how to convert float to int in JavaScript and the answer is here: How do I convert a float number to a whole number in JavaScript?

BUT if you really want to convert the precision of your values: What you could do is multiply by X, and round that ("convert to int", see link above), and then divide again by X. However this is not like converting a float of one size to another size because you dont take into account the mantisse/exponent representation of a float inside the actual memory. My solution only chops off some precision (quantization if you will).

Anyway, JavaScript is not a platform where you handle memory representation of types; it is already messy enough to handle the difference between float and int ;)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top