File: encode.js

package info (click to toggle)
shine 3.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,500 kB
  • sloc: ansic: 2,264; javascript: 221; makefile: 51; sh: 2
file content (58 lines) | stat: -rw-r--r-- 1,547 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function runEncodeTest(log, callback) {
  log("Executing encoding test");

  var samplerate = 44100;
  var channels = 2;
  var bitrate = 128;
  var mode = Shine.STEREO;

  var shine = new Shine({
    samplerate: samplerate,
    channels: channels,
    bitrate: bitrate,
    mode: mode
  });

  var started = new Date;
  var duration = 0.0;
  var encoded = [];

  var xhr = new XMLHttpRequest();
  xhr.open("GET", "encode.wav", true);
  xhr.responseType = "arraybuffer";
  xhr.onload = function() {
    var samples = xhr.response.byteLength / (2*channels);
    var data = new Int16Array(xhr.response);
    var buf = convertInterleavedBuffer(data, channels, samples);
    duration += parseFloat(samples) / samplerate;
    encoded.push(shine.encode(buf));
  };

  log("Got WAV file.");
  log("Encoding..");
  xhr.addEventListener("load", function () {
    encoded.push(shine.close());

    var ended = new Date
    var encodingTime = (ended.getTime() - started.getTime())/1000
    log("Done encoding.");
    log("File duration: " + duration.toFixed(2) + " seconds");
    log("Encoding time: " + encodingTime.toFixed(2) + " seconds");
    log("Encoding rate: " + (duration/encodingTime).toFixed(2) + "X");
    callback(new Blob(encoded));
  });

  xhr.send();
}

function convertInterleavedBuffer(buf, channels, samples) {
  var chan, i;
  ret = new Array(channels);
  for (chan=0; chan<channels; chan++) {
    ret[chan] = new Int16Array(samples);
    for (i=0; i<samples; i++) {
      ret[chan][i] = buf[i*channels + chan];
    }
  }
  return ret;
}