Skip to content

Commit

Permalink
Document usage with Float32Array / AudioBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
cdauth committed Apr 25, 2021
1 parent 564612b commit e18447f
Showing 1 changed file with 12 additions and 0 deletions.
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;
}
```

1 comment on commit e18447f

@guest271314
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

Please sign in to comment.