Code Monkey home page Code Monkey logo

Comments (8)

 avatar commented on July 21, 2024

Also, there is this approach which is a bit different: https://stackoverflow.com/a/35248852/3405291

from opus-recorder.

 avatar commented on July 21, 2024

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.

chris-rudmin avatar chris-rudmin commented on July 21, 2024

@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.

 avatar commented on July 21, 2024

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.

chris-rudmin avatar chris-rudmin commented on July 21, 2024

@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.

 avatar commented on July 21, 2024

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.

chris-rudmin avatar chris-rudmin commented on July 21, 2024

@Micrufun Watch out for 8 bit wavs which use unsigned Integers

from opus-recorder.

chris-rudmin avatar chris-rudmin commented on July 21, 2024

I referenced dsprelated.com forums frequently when I was in search of different algorithms / tradeoffs.

from opus-recorder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.