3

I need to get the Sample Rate of Audio file, loaded by standard input element. I tried to use this function

function func(e) {
    let reader = new FileReader();
    reader.readAsArrayBuffer(e.files[0]);
    reader.onload = function(event) {
            let buffer = reader.result;
            let AudioContext = window.AudioContext || window.webkitAudioContext;
            let context = new AudioContext();
            context.decodeAudioData(buffer, function(buffer1) {
                    console.log(buffer1.sampleRate);
                });
            }
        }

But it always displays one incorrect answer 48000 regardless of file.

So I try to calculate the Sample Rate with formula

SampleRate = File.size/(NumberOfChannels*BitDepth*File.duration);

But I dont know how to get the value of audio BitDepth. Please, help.

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Vera
  • 51
  • 1
  • 6
  • did you try to see if sampleRate is an attribute on your reader object ? – Scott Stensland Jul 24 '19 at 13:35
  • Sorry, I dont see sampleRate as attribute of reader. Is it? – Vera Jul 24 '19 at 14:09
  • Web Audio API cannot change sample rate ... does your audio render sounding too fast or too slow ? a typical bit depth for CD quality audio is 16 ... baked into web audio api is notion of channels ... try using a known mono audio file to simplify ... what audio codec is your input file ? – Scott Stensland Jul 24 '19 at 14:50
  • 1
    @ScottStensland I dont need to change sample rate and to render sound, I need to display sample rate on web site after file loading. The difficulty is that the file can be any. – Vera Jul 25 '19 at 06:21

0 Answers0