最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to convert Blob to Int16Array - Stack Overflow

matteradmin6PV0评论

I have a WAV file in Blob and in order to convert it to MP3 I need to convert it to Int16Array first (to follow the example from here: ).

E.g.

var mp3encoder = new lamejs.Mp3Encoder(2, 44100, 128);
// instead of `var samples = new Int16Array(44100);` I want something like `var samples = new Int16Array(blob);`
var mp3Tmp = mp3encoder.encodeBuffer(samples);

Is this possible?

I have a WAV file in Blob and in order to convert it to MP3 I need to convert it to Int16Array first (to follow the example from here: https://github./zhuker/lamejs).

E.g.

var mp3encoder = new lamejs.Mp3Encoder(2, 44100, 128);
// instead of `var samples = new Int16Array(44100);` I want something like `var samples = new Int16Array(blob);`
var mp3Tmp = mp3encoder.encodeBuffer(samples);

Is this possible?

Share Improve this question asked May 23, 2016 at 15:08 Alex NetkachovAlex Netkachov 13.6k7 gold badges55 silver badges90 bronze badges 2
  • You have to employ the FileReader API: stackoverflow./questions/15341912/… – lipp Commented May 23, 2016 at 15:17
  • Did you manage to sort this out? – Mac_W Commented Jun 24, 2019 at 14:39
Add a ment  | 

1 Answer 1

Reset to default 3

Provided you know the data is actually a blob of 16-bit ints, then yes, it's possible:

  1. Read the Blob into an ArrayBuffer via its arrayBuffer method (the original answer had to use FileReader, but now Blob has an arrayBuffer method; see the edit history if for some reason you have to support old environments without it):

    samples.arrayBuffer()
    .then(buffer => {
        // ...
    })
    .catch(error => {
        // ...handle/report error...
    });
    
  2. View the ArrayBuffer as an Int16Array via the Int16Array constructor:

    const data = new Int16Array(buffer);
    

Now data is the array buffer from the blob viewed as an Int16Array.

Something along these lines:

samples.arrayBuffer()
.then(buffer => {
    const data = new Int16Array(buffer);
    // ...use `data` here...
})
.catch(error => {
    // ...handle/report error...
});
Post a comment

comment list (0)

  1. No comments so far