All files / src/tasks audioCalibrator.js

58.99% Statements 82/139
100% Branches 2/2
20% Functions 2/10
58.99% Lines 82/139

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 1401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x     1x 1x                     1x 1x 1x 1x 1x         1x 1x 1x  
/* eslint-disable no-await-in-loop */
import AudioRecorder from './audioRecorder';
import PythonServerInterface from '../server/PythonServerInterface';
import {sleep, saveToCSV} from '../utils';
 
/**
 * Provides methods for calibrating the user's speakers
 * @extends AudioRecorder
 */
class AudioCalibrator extends AudioRecorder {
  /**
   *
   */
  constructor(numCalibrationRounds = 1, numCalibrationNodes = 1) {
    super();
    this.numCalibratingRounds = numCalibrationRounds;
    this.numCalibrationNodes = numCalibrationNodes;
    this.pyServer = new PythonServerInterface();
  }
 
  /** @private */
  #isCalibrating = false;
 
  /** @private */
  #sourceAudioContext;
 
  /** @protected */
  numCalibratingRounds;
 
  /** @private */
  sourceSamplingRate;
 
  /** @protected */
  numCalibrationNodes;
 
  /** @protected */
  calibrationNodes = [];
 
  /** @protected */
  localAudio;
 
  /**
   * Called when a call is received.
   * Creates a local audio DOM element and attaches it to the page.
   */
  createLocalAudio = targetElement => {
    this.localAudio = document.createElement('audio');
    this.localAudio.setAttribute('id', 'localAudio');
    targetElement.appendChild(this.localAudio);
  };
 
  /**
   *
   * @param {MediaStream} stream
   * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
   * @param {*} beforeRecord - (async) function that is called before recording
   * @param {*} afterRecord  - (async) function that is called after recording
   */
  calibrationSteps = async (
    stream,
    playCalibrationAudio,
    beforeRecord = () => {},
    afterRecord = () => {}
  ) => {
    let numRounds = 0;

    // calibration loop
    while (!this.#isCalibrating && numRounds < this.numCalibratingRounds) {
      // before recording
      await beforeRecord();

      // start recording
      await this.startRecording(stream);

      // play calibration audio
      console.log(`Calibration Round ${numRounds}`);
      await playCalibrationAudio();

      // when done, stop recording
      console.log('Calibration Round Complete');
      await this.stopRecording();

      // after recording
      await afterRecord();

      this.calibrationNodes = [];

      // eslint-disable-next-line no-await-in-loop
      await sleep(2);
      numRounds += 1;
    }
  };
 
  /**
   * Getter for the isCalibrating property.
   * @public
   * @returns {Boolean} - True if the audio is being calibrated, false otherwise.
   */
  getCalibrationStatus = () => this.#isCalibrating;
 
  /**
   * Set the sampling rate to the value received from the listener
   * @param {*} sinkSamplingRate
   */
  setSamplingRates = samplingRate => {
    this.sinkSamplingRate = samplingRate;
    this.sourceSamplingRate = samplingRate;
    console.log('sampling rate', samplingRate);
  };
 
  sampleRatesSet = () => this.sourceSamplingRate && this.sinkSamplingRate;
 
  addCalibrationNode = node => {
    this.calibrationNodes.push(node);
  };
 
  makeNewSourceAudioContext = () => {
    const options = {
      sampleRate: this.sourceSamplingRate,
    };

    this.#sourceAudioContext = new (window.AudioContext ||
      window.webkitAudioContext ||
      window.audioContext)(options);

    return this.#sourceAudioContext;
  };
 
  /**
   * Download the result of the calibration roudns
   */
  downloadData = () => {
    this.getAllRecordedSignals().forEach((signal, i) => {
      saveToCSV(signal, `recordedMLSignal_${i}.csv`);
    });
  };
}
 
export default AudioCalibrator;