Comments (8)
Also, there is this approach which is a bit different: https://stackoverflow.com/a/35248852/3405291
from opus-recorder.
Test indicates that this code works too:
var maxPossible = 2**16/2; // Signed 16 bit is the data resolution.
... = (totalFile[i + j]) / maxPossible;
Not sure about the precision difference.
from opus-recorder.
@Micrufun Indeed there are many different ways to convert Int to float. The additional 0.5 is to convert an asymmetric space (-32768, 32767) into a symmetric space(-1,1) by shifting the 0 crossing point
from opus-recorder.
If by any chance the example is going to be extended to include any channel count and bit depth, this is a tested modification which might help:
function chunkBuffers(arrayBuffer, chunkLength, channelCount, bitDepth){
var chunkedBuffers = [];
var maxPossible = 2**bitDepth/2; // Bit depth is the data resolution.
console.log('MAX POSSIBlE DATA VALUE', maxPossible);
var totalFile = new Int16Array(arrayBuffer);
// Skip wave header; 44 bytes
for (i = 22; i < totalFile.length; i += chunkLength) {
// Convert 16 bit signed int to 32bit float
var bufferChunk = new Float32Array(chunkLength/channelCount); // Just keep 1st channel. So, divide length.
var idx = 0; // To just keep 1st channel and skip the rest.
for (j = 0 ; j < chunkLength ; j+=channelCount) { // Just keep 1st channel by `j+=`.
// UPDATE: Not desirable:
// bufferChunk[idx] = (totalFile[i + j]) / maxPossible;
// UPDATE: Shifting the 0 crossing and keeping the two halves of the waveform symmetric,
// by `+-0.5` statements:
bufferChunk[idx] = (totalFile[i + j]+ 0.5) / (maxPossible-0.5);
idx++;
}
chunkedBuffers.push([bufferChunk]) ;
};
return chunkedBuffers
};
Then, calling it by:
chunkBuffers(arrayBuffer, bufferLength, 2, 16).forEach(...)
Or:
chunkBuffers(arrayBuffer, bufferLength, 1, 16).forEach(...)
from opus-recorder.
@Micrufun By shifting the 0 crossing point, we distribute the conversion precision across the whole range [-1.1]. In the other implementation you shared, the negative values and positive values are treated separately. the 0 crossing point is maintained, but the negative values [-1. 0] will have slightly more precision than the positive values [0,1].
Since we are dealing with audio, shifting the 0 crossing is acceptable, and keeping the two halves of the waveform symmetric is desirable. For other applications, this implementation of int to float might not be acceptable.
from opus-recorder.
Right. Finally, I considered the desirable symmetry:
// Shifting the 0 crossing and keeping the two halves of the waveform symmetric,
// by `+-0.5` statements:
bufferChunk[idx] = (totalFile[i + j]+ 0.5) / (maxPossible-0.5);
from opus-recorder.
@Micrufun Watch out for 8 bit wavs which use unsigned Integers
from opus-recorder.
I referenced dsprelated.com forums frequently when I was in search of different algorithms / tradeoffs.
from opus-recorder.
Related Issues (20)
- How to use with Vite? HOT 5
- The first recording is not saved in Safari on IOS 15.1 HOT 1
- Robotic, torn audio on IOS 15.1 HOT 3
- Issue while using decoder with React application HOT 11
- Recorder output file size too small - Vue.js HOT 4
- building and running on windows 10 HOT 4
- Error running waveRecorder.html example HOT 3
- using waveform-playlist with opus-recorder HOT 1
- Uncaught TypeError: Cannot read properties of undefined (reading 'length')
- Not working with Vite + Vue 3
- wasm streaming compile failed: TypeError: Could not download wasm module falling back to ArrayBuffer instantiation how can i resolve this issue ? HOT 3
- Audio distorted after 30 seconds HOT 6
- few seconds of recording are missing at the beginning (especially on IOS)
- Play pages real-time HOT 2
- ondataavailable not fired with waveWorker
- TypeScript definitions HOT 1
- Opus Player
- Is it possible to Decode two audio sources HOT 4
- Error webassembly.compile HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opus-recorder.