// @flow

const ctx: AudioContext = new window.AudioContext()

type RecordingOptions = {
  mimeType: string,
  audioBitsPerSecond: number,
}

const defaultRecordingOptions: RecordingOptions = {
  mimeType: 'video/webm;codecs=vp8',
  audioBitsPerSecond: 128000,
}

export default class Recorder {
  chunks: Array<any> = []
  options: RecordingOptions
  mediaRecorder: MediaRecorder
  stream: MediaStream
  monitor: MediaStreamAudioSourceNode
  onAudioBufferCaptured: (buffer: AudioBuffer) => void = buffer => console.log('onAudioBufferCaptured', buffer)

  constructor() {
    this.options = { ...defaultRecordingOptions }
    const { mimeType } = this.options
    if (!MediaRecorder.isTypeSupported(mimeType)) {
      throw new Error(`MIME Type ${mimeType} is not supported by Recorder`)
    }
  }

  prepare() {
    navigator.mediaDevices.getUserMedia({ audio: true, video: false })
      .then((stream: MediaStream) => this.initMediaRecorder(stream))
  }

  startRecording(enableMonitor: boolean = false) {
    if (enableMonitor) {
      this.monitor = ctx.createMediaStreamSource(this.stream)
      this.monitor.connect(ctx.destination)
    }
    this.chunks = []
    this.mediaRecorder.start()
  }

  stopRecording() {
    console.log('stop recording')
    if (this.monitor) {
      this.monitor.disconnect(ctx.destination)
      delete this.monitor
    }
    this.mediaRecorder.stop()
  }

  destroy() {
    delete this.mediaRecorder
    if (this.stream) {
      this.stream.getAudioTracks().forEach((track: MediaStreamTrack) => {
        track.stop()
      })
    }
  }

  initMediaRecorder(stream: MediaStream) {
    this.stream = stream
    console.log('initMediaRecorder', stream)

    this.mediaRecorder = new MediaRecorder(this.stream, this.options)
    this.mediaRecorder.addEventListener('dataavailable', (e) => {
      if (e.data.size > 0) {
        console.log('recoring', e.data.size, e.data)
        this.chunks.push(e.data)
      }
    })

    this.mediaRecorder.addEventListener('stop', () => {
      console.log('recorder stopped.')
      const blob: Blob = this.chunks.length === 1 ? this.chunks[0] : new Blob(this.chunks)
      console.log(this.chunks)

      const fileReader = new FileReader()
      fileReader.onload = (e) => {
        const arrayBuffer = e.target.result
        console.log('arrayBuffer', arrayBuffer)
        ctx.decodeAudioData(arrayBuffer)
          .then((decodeAudioBuffer: AudioBuffer) => {
            console.log('AudioBuffer decoded', decodeAudioBuffer)
            this.onAudioBufferCaptured(decodeAudioBuffer)
            /* preview
            const source = ctx.createBufferSource()
            source.buffer = decodedData
            source.connect(ctx.destination)
            source.start(0)
            */
          })
      }

      fileReader.readAsArrayBuffer(blob)
    })

    this.startRecording()
  }
}

@@ -1220,42 +1220,8 @@
 ) %7B%0A
-    console.log('stop recording')%0A
     
@@ -1232,32 +1232,32 @@
 this.monitor) %7B%0A
+
       this.monit
@@ -1607,53 +1607,8 @@
 ream
-%0A    console.log('initMediaRecorder', stream)
 %0A%0A  
@@ -1774,61 +1774,8 @@
 ) %7B%0A
-        console.log('recoring', e.data.size, e.data)%0A
     
@@ -1879,47 +1879,8 @@
 %3E %7B%0A
-      console.log('recorder stopped.')%0A
     
@@ -1969,39 +1969,8 @@
 nks)
-%0A      console.log(this.chunks)
 %0A%0A  
@@ -2092,56 +2092,8 @@
 ult%0A
-        console.log('arrayBuffer', arrayBuffer)%0A
     
@@ -2199,321 +2199,53 @@
     
-console.log('AudioBuffer decoded', decodeAudioBuffer)%0A            this.onAudioBufferCaptured(decodeAudioBuffer)%0A            /* preview%0A            const source = ctx.createBufferSource()%0A            source.buffer = decodedData%0A            source.connect(ctx.destination)%0A            source.start(0)%0A            */
+this.onAudioBufferCaptured(decodeAudioBuffer)
 %0A   

remove comments