audio-delegate.ts 4.22 KB
Newer Older
Chen Jiping's avatar
Chen Jiping committed
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
import { EventEmitter } from '@angular/core';

export class AudioDelegate {
  audioObj = new Audio();
  public audioPlayTimestamp = new EventEmitter();
  public audioDataLoaded = new EventEmitter();
  public audioPlayBarPosition = new EventEmitter();
  public audioPlayEnd = new EventEmitter();
  syncAudioCurrentTimeId: number = null;
  private arrayBuffer = null;
  formatter = new Intl.NumberFormat('en', {
    minimumIntegerDigits: 2,
    minimumFractionDigits: 3,
    maximumFractionDigits: 3,
    useGrouping: false,
  });
  constructor() {
    this.audioObj.onloadeddata = this.onAudioDataLoaded.bind(this);
    this.audioObj.onplay = this.onAudioPlay.bind(this);
    this.audioObj.onpause = this.onAudioPause.bind(this);
    this.audioObj.ontimeupdate = this.onAudioTimeUpdate.bind(this);
    this.audioObj.onratechange = this.onAudioRateChange.bind(this);
    this.audioObj.onended = this.onAudioEnded.bind(this);
    this.audioObj.onerror = () => {
    };
  }
  set playbackRate(val) {
    this.audioObj.playbackRate = val;
  }

  set src(val) {
    this.audioObj.src = val;
  }
  get src() {
    return this.audioObj.src;
  }
  convertTagToTime(tag) {
    tag = tag.replace('[', '');
    tag = tag.replace(']', '');
    const parts = tag.split(':');
    let h = 0;
    let m = 0;
    let s = 0;
    if (parts.length === 3) {
      h = parseInt(parts[0], 10);
      m = parseInt(parts[1], 10);
      s = parseInt(parts[2], 10);
    } else if (parts.length === 2) {
      m = parseInt(parts[0], 10);
      s = parseFloat(parts[1] );
    }

    return h * 60 * 60 + m * 60 + s;
  }
  convertTimeToTag(time, withBracket = true): string {
    if (time === undefined) {
      return '';
    }
    const hh = Math.floor(time / 60 / 60)
      .toString()
      .padStart(2, '0');
    const mm = Math.floor(time / 60)
      .toString()
      .padStart(2, '0');
    const ss = this.formatter.format(time % 60);

    return withBracket ? `[${hh}:${mm}:${ss}]` : `${hh}:${mm}:${ss}`;
  }
  setSource(ab) {
    this.arrayBuffer = ab;
    const blob = new Blob([ab], { type: 'audio/wav' });
    this.audioObj.src = URL.createObjectURL(blob);
  }
  getDataBuffer() {
    return this.arrayBuffer;
  }

  getBufferClip(start, end) {
    return this.arrayBuffer.slice(start * this.arrayBuffer.length, end * this.arrayBuffer.length);
  }
  load() {
    this.audioObj.load();
  }
  syncAudioCurrentTime() {
    this.audioPlayTimestamp.emit({
      timeFormat: this.convertTimeToTag(this.audioObj.currentTime, false),
      time: this.audioObj.currentTime
    });
    this.syncAudioCurrentTimeId = requestAnimationFrame(() => {
      this.syncAudioCurrentTime();
    });
  }
  onAudioDataLoaded(evt) {
    console.log('onAudioDataLoaded', evt);
    this.audioDataLoaded.emit(this.arrayBuffer);
  }
  onAudioPlay() {
    this.syncAudioCurrentTimeId = requestAnimationFrame(() => {
      this.syncAudioCurrentTime();
    });
    console.log('onAudioPlay');
  }
  onAudioPause() {
    console.log('onAudioPause');
    cancelAnimationFrame(this.syncAudioCurrentTimeId);
  }
  onAudioEnded() {
    console.log('onAudioEnded');
    this.audioPlayEnd.emit()
    cancelAnimationFrame(this.syncAudioCurrentTimeId);
  }
  onAudioTimeUpdate() {
    // console.log('onAudioTimeUpdate', this.convertTimeToTag(this.audioObj.currentTime));
    // this.audioPlayTimestamp.emit(this.convertTimeToTag(this.audioObj.currentTime));
  }
  onAudioRateChange() {
    console.log('onAudioRateChange');
  }
  get isPlaying() {
    return !!(this.audioObj.currentTime > 0
      && !this.audioObj.paused
      && !this.audioObj.ended
      && this.audioObj.readyState > 2);
  }

  currentTimeFormatted(time?) {
    let t = this.audioObj.currentTime;
    if (typeof time !== 'undefined') {
      t = time;
    }
    return this.convertTimeToTag(t);
  }

  get currentTime() {
    return this.audioObj.currentTime;
  }
  set currentTime(val) {
    this.audioPlayBarPosition.emit({
      time: val
    });
    this.audioObj.currentTime = val;
  }
  get duration() {
    return this.audioObj.duration;
  }
  get durationFormatted() {
    return this.convertTimeToTag(this.audioObj.duration, false);
  }
  get currentSrc() {
    return this.audioObj.currentSrc;
  }
  pause() {
    this.audioObj.pause();
  }
  async play() {
    return this.audioObj.play();
  }
}