<commit_before>import React from 'react'

import Player from 'components/Player'
import CPULoad from 'components/CPULoad'
import Editor from 'components/Editor'
import { Icon, IconStack } from 'components/Icon'

import compile from 'compile'
import {Int16Stereo, makeWAVURL} from 'PCM'

import EXAMPLE_SCRIPTS from 'examples'

const DEFAULT_SCRIPT = require('!raw!examples/default')

function ButtonWithPanel({panel, children, ...other}) {
  return <div className={`panel-container${panel ? ' open' : ''}`}>
    {panel ? <div className='panel'>{panel}</div> : null}
    <button {...other}>{children}</button>
  </div>
}

ButtonWithPanel.propType = {
  panel: React.PropTypes.node,
}

export default class App extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      fn: () => [0, 0],
      length: 0,
      renderTime: undefined,
      newConfirming: false,
      loadConfirming: false,
      examplesOpen: false,
    }

    this.audioCtx = new (window.AudioContext || window.webkitAudioContext)()
  }

  componentDidMount() {
    this.handleUpdate()
  }

  componentWillUnmount() {
    this.audioCtx.close()
  }

  handleUpdate() {
    let editor = this.refs.editor.editor
    let source = editor.getValue()

    let {fn, length, error} = compile(source, this.audioCtx.sampleRate)

    this.refs.editor.maybeAddError(error) // Throws
    this.setState({fn, length})
  }

  handleRender() {
    this.refs.player.pause()

    this.handleUpdate()

    let source = this.refs.editor.editor.getValue()
    let sampleRate = this.refs.renderSampleRate.value

    let {fn, length, error: compileError} = compile(source, sampleRate)
    this.refs.editor.maybeAddError(compileError) // Throws

    let {buffer, error: runtimeError} = Int16Stereo(sampleRate, length, fn)
    this.refs.editor.maybeAddError(runtimeError) // Throws

    let link = document.createElement('a')
    link.download = 'render.wav'
    link.href = makeWAVURL(buffer, 2, sampleRate)
    link.click()

    URL.revokeObjectURL(link.href)
  }

  handlePlayingChange(playing) {
    this.setState({renderTime: undefined})
  }

  handleRenderTime(renderTime) {
    this.setState({renderTime})
  }

  handleError(error) {
    try {
      this.refs.editor.maybeAddError(error)
    } catch (e) {/* IGNORE EXCEPTION! Let the player try to recover */}
  }

  handleTogglePlay() {
    this.refs.player.togglePlay()
  }

  handleKeyDown(e) {
    let {ctrlKey, shiftKey, altKey, keyCode} = e

    // Prevent CTRL-S from opening the webpage save dialog
    if (ctrlKey && !shiftKey && !altKey && keyCode === 83 /* S */) {
      e.preventDefault()
    }
    // Escape closes panels
    else if (!ctrlKey && !shiftKey && !altKey && keyCode === 27 /* Escape */) {
      this.closePanels()
    }
  }

  handleKeyUp({ctrlKey, shiftKey, altKey, keyCode}) {
    if (ctrlKey && !shiftKey && !altKey) {
      if (keyCode === 83 /* S */ ) {
        this.handleUpdate()
      } else if (keyCode === 32 /* SPACE */) {
        this.handleTogglePlay()
      }
    }
  }

  /* Panels */

  handleNew() {
    this.setState({
      newConfirming: true,
      loadConfirming: false,
    })
  }

  handleNewConfirmed() {
    this.refs.editor.new()
    this.handleUpdate()
    this.closePanels()
  }

  handleLoad() {
    let input = document.createElement('input')
    input.type = 'file'
    input.accept = '.musika,application/javascript'
    input.onchange = () => {
      let f = input.files[0]

      if (!f) return

      let r = new FileReader()
      r.onload = ({target: {result}}) => {
        this.setState({
          newConfirming: false,
          loadConfirming: {
            name: f.name,
            content: result,
          }
        })
      }
      r.readAsText(f)
    }

    input.click()
  }

  handleLoadConfirmed() {
    this.refs.editor.new(this.state.loadConfirming.content)
    this.handleUpdate()
    this.closePanels()
  }

  handleSave() {
    let script = this.refs.editor.editor.getValue()
    let blob = new Blob([script], {type: 'application/javascript;charset=utf-8'})
    let url = URL.createObjectURL(blob)

    let link = document.createElement('a')
    link.download = 'script.musika'
    link.href = url
    link.click()

    URL.revokeObjectURL(url)
  }

  handleExamples() {
    this.setState({
      examplesOpen: true,
      examplesConfirming: false
    })
  }

  handleExamplesConfirmed() {<commit_msg>Convert \r\n to \n<commit_after>import React from 'react'

import Player from 'components/Player'
import CPULoad from 'components/CPULoad'
import Editor from 'components/Editor'
import { Icon, IconStack } from 'components/Icon'

import compile from 'compile'
import {Int16Stereo, makeWAVURL} from 'PCM'

import EXAMPLE_SCRIPTS from 'examples'

const DEFAULT_SCRIPT = require('!raw!examples/default')

function ButtonWithPanel({panel, children, ...other}) {
  return <div className={`panel-container${panel ? ' open' : ''}`}>
    {panel ? <div className='panel'>{panel}</div> : null}
    <button {...other}>{children}</button>
  </div>
}

ButtonWithPanel.propType = {
  panel: React.PropTypes.node,
}

export default class App extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      fn: () => [0, 0],
      length: 0,
      renderTime: undefined,
      newConfirming: false,
      loadConfirming: false,
      examplesOpen: false,
    }

    this.audioCtx = new (window.AudioContext || window.webkitAudioContext)()
  }

  componentDidMount() {
    this.handleUpdate()
  }

  componentWillUnmount() {
    this.audioCtx.close()
  }

  handleUpdate() {
    let editor = this.refs.editor.editor
    let source = editor.getValue()

    let {fn, length, error} = compile(source, this.audioCtx.sampleRate)

    this.refs.editor.maybeAddError(error) // Throws
    this.setState({fn, length})
  }

  handleRender() {
    this.refs.player.pause()

    this.handleUpdate()

    let source = this.refs.editor.editor.getValue()
    let sampleRate = this.refs.renderSampleRate.value

    let {fn, length, error: compileError} = compile(source, sampleRate)
    this.refs.editor.maybeAddError(compileError) // Throws

    let {buffer, error: runtimeError} = Int16Stereo(sampleRate, length, fn)
    this.refs.editor.maybeAddError(runtimeError) // Throws

    let link = document.createElement('a')
    link.download = 'render.wav'
    link.href = makeWAVURL(buffer, 2, sampleRate)
    link.click()

    URL.revokeObjectURL(link.href)
  }

  handlePlayingChange(playing) {
    this.setState({renderTime: undefined})
  }

  handleRenderTime(renderTime) {
    this.setState({renderTime})
  }

  handleError(error) {
    try {
      this.refs.editor.maybeAddError(error)
    } catch (e) {/* IGNORE EXCEPTION! Let the player try to recover */}
  }

  handleTogglePlay() {
    this.refs.player.togglePlay()
  }

  handleKeyDown(e) {
    let {ctrlKey, shiftKey, altKey, keyCode} = e

    // Prevent CTRL-S from opening the webpage save dialog
    if (ctrlKey && !shiftKey && !altKey && keyCode === 83 /* S */) {
      e.preventDefault()
    }
    // Escape closes panels
    else if (!ctrlKey && !shiftKey && !altKey && keyCode === 27 /* Escape */) {
      this.closePanels()
    }
  }

  handleKeyUp({ctrlKey, shiftKey, altKey, keyCode}) {
    if (ctrlKey && !shiftKey && !altKey) {
      if (keyCode === 83 /* S */ ) {
        this.handleUpdate()
      } else if (keyCode === 32 /* SPACE */) {
        this.handleTogglePlay()
      }
    }
  }

  /* Panels */

  handleNew() {
    this.setState({
      newConfirming: true,
      loadConfirming: false,
    })
  }

  handleNewConfirmed() {
    this.refs.editor.new()
    this.handleUpdate()
    this.closePanels()
  }

  handleLoad() {
    let input = document.createElement('input')
    input.type = 'file'
    input.accept = '.musika,application/javascript'
    input.onchange = () => {
      let f = input.files[0]

      if (!f) return

      let r = new FileReader()
      r.onload = ({target: {result}}) => {
        this.setState({
          newConfirming: false,
          loadConfirming: {
            name: f.name,
            content: result,
          }
        })
      }
      r.readAsText(f)
    }

    input.click()
  }

  handleLoadConfirmed() {
    this.refs.editor.new(this.state.loadConfirming.content.replace(/\r\n/g, '\n'))
    this.handleUpdate()
    this.closePanels()
  }

  handleSave() {
    let script = this.refs.editor.editor.getValue()
    let blob = new Blob([script], {type: 'application/javascript;charset=utf-8'})
    let url = URL.createObjectURL(blob)

    let link = document.createElement('a')
    link.download = 'script.musika'
    link.href = url
    link.click()

    URL.revokeObjectURL(url)
  }

  handleExamples() {
    this.setState({
      examplesOpen: true,
      examplesConfirming: false
    })
  }

  handleExamplesConfirmed() {
{'fraction_non_alphanumeric': 0.14018058690744922, 'fraction_numerical': 0.004063205417607223, 'mean_word_length': 2.8226919758412423, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3672559', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2823, 'n_words': 584}