2

I want to record the audio at 16000Hz and get the spectrogram of it. My model takes input of [null.1998.101]. I am unable to achieve it in javascript

    const mic = await tf.data.microphone({
    fftSize: 256,
    columnTruncateLength: 101,
    numFramesPerSpectrogram: 1998 ,
    sampleRateHz:16000,
    includeSpectrogram: true,
    includeWaveform: true
});

const audioData = await mic.capture();
console.log(audioData)
const spectrogramTensor = audioData.spectrogram;
console.log(spectrogramTensor)
spectrogramTensor.print();
const waveformTensor = audioData.waveform;
waveformTensor.print();
mic.stop(); 

My model is trigger word detection. In Python I have used the following code.

def graph_spectrogram(wav_file):
rate, data = get_wav_info(wav_file)
print(data)
print(len(data))
nfft = 200 # Length of each window segment
fs = 8000 # Sampling frequencies
noverlap = 120 # Overlap between windows
nchannels = data.ndim
if nchannels == 1:
    pxx, freqs, bins, im = plt.specgram(data, nfft, fs, noverlap = noverlap)
elif nchannels == 2:
    pxx, freqs, bins, im = plt.specgram(data[:,0], nfft, fs, noverlap = noverlap)
return pxx
Anmol Duggal
  • 51
  • 1
  • 6

1 Answers1

2

The browser has a default and fixed value of the sampling rate of audio recording. The following will output the frequency rate of the browser.

new window.AudioContext().sampleRate

The error is thrown because 16000 does not match the browser sampling rate. It is currently not possible to change the sampling rate of an audio recording from the browser. What can be done is

  • to train the model using the frequency rate
  • reshape (or slice ) the tensor to the model inputShape
  • record the audio and resample it (using this answer) and create a tensor from an audio recording (using this answer
  • Though I haven't tried, it seems that the value of the sample rate comes from the settings of the operating system. Changing it will allow the recording to have the right sample rate. On linux, the recording frequency can be set in the /etc/pulse/daemon.conf file
edkeveked
  • 17,989
  • 10
  • 55
  • 93