Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document usage with Float32Array / AudioBuffer #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,15 @@ if (mp3buf.length > 0) {
console.log(mp3Data);
</script>
```

# Usage with Float32Array / AudioBuffer

Some libraries (such as `AudioBuffer.getChannelData()` from the WebAudio API) provide audio data as a `Float32Array` with values between `-1.0` and `1.0`. lamejs expects values between `-32768` and `32767`, so the values have to be mapped:

```javascript
var floatSamples = new Float32Array(44100); // Float sample from an external source
var samples = new Int32Array(floatSamples.length);
for (var i = 0; i < floatSamples.length; i++) {
samples[i] = floatSamples[i] < 0 ? floatSamples[i] * 32768 : floatSamples[i] * 32767;
}
```