All files / addon/utils decode-base64.js

100% Statements 6/6
100% Branches 0/0
100% Functions 3/3
100% Lines 6/6

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                            2x     24130x                             1x 1x 1x         1x    
/**
 * @public
 * @class utils
 */
 
/**
 * Converts a base64 string into a Uint8Array of binary data.
 *
 * @private
 * @method base64ToUint8
 * @param  {string} base64String The base64 string that you'd like to be converted into a Uint8Array.
 * @return {Uint8Array} A Uint8Array of converted binary audio data.
 */
export function base64ToUint8(base64String) {
  return new Uint8Array(
    atob(base64String)
      .split('')
      .map((char) => char.charCodeAt(0))
  );
}
 
/**
 * Strips extraneous stuff from a soundfont and splits the soundfont into a JSON
 * object. Keys are note names and values are base64 encoded strings.
 *
 * @private
 * @method mungeSoundFont
 * @param {string} soundfont A soundfont as a long base64 string
 * @return {object} A JSON representation of all the notes in the font
 */
export function mungeSoundFont(soundfont) {
  const begin =
    soundfont.indexOf('=', soundfont.indexOf('MIDI.Soundfont.')) + 2;
  const end = soundfont.lastIndexOf('"') + 1;
  const string = `${soundfont.slice(begin, end)}}`
    .replace(new RegExp('data:audio/mp3;base64,', 'g'), '')
    .replace(new RegExp('data:audio/mpeg;base64,', 'g'), '')
    .replace(new RegExp('data:audio/ogg;base64,', 'g'), '');
 
  return JSON.parse(string);
}