summaryrefslogtreecommitdiff
path: root/app/client/modules/pix2wav/views/spectrogram.upload.js
blob: a0a2708c0c5963bd54064931d69a7ea7dbffd283 (plain)
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
import { h, Component } from 'preact'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import moment from 'moment'
import util from '../../../util'

import * as pix2wavActions from '../pix2wav.actions'
import * as pix2wavTasks from '../pix2wav.tasks'

import {
  Loading, Progress,
  Group, Param, FileUpload, TextInput, Button
} from '../../../common'

import * as wav2pixActions from '../../../audio/wav2pix'

import pix2wavModule from '../pix2wav.module'

class SpectrogramUpload extends Component {
  constructor(props){
    super(props)
    this.state = {
      file: null,
      name: "",
      frames: [],
    }
    const audioElement = document.createElement('audio')
    audioElement.addEventListener('loadedmetadata', () => {
      this.setState({ duration: audioElement.duration })
    })
    this.audioElement = audioElement
  }
  pickFile(file){
    let name = file.name.split('.')[0]
      .replace(/\s+/g, '_')
      .replace(/-/g, '_')
      .replace(/_+/g, '_')
    this.setState({ file, name })
    this.audioElement.src = URL.createObjectURL(file)
    console.log(file.size)
    if (file.size < 2 << 20) {
      console.log('booooooooo')
      this.props.wav2pix.renderFrames(file, {})
      .then(frames => {
        console.log(frames)
        this.setState({
          ...this.state, frames
        })
      })
    }
    console.log(file)
    // get info on the file... size, etc
  }
  buildZip(){
    const { file } = this.state

  }
  render(){
    // loading={pix2wav.loading}
    // progress={pix2wav.progress}
    // id={pix2wav.folder_id}
    // module={pix2wavModule}
    // data={pix2wav.data}
    // folder={folder}
    const { file, frames } = this.state
    return (
      <div className='row'>
        <div className='col spectrogramBuilder'>
          <Group title='Spectrogram Builder'>
            <p>
              {"Convert your sounds into spectrograms. "}
              {"Sound files can be WAV, MP3, AIFF, or FLAC. "}
              <b>2 minutes max.</b>
            </p>
            <FileUpload
              title='Choose a sound file'
              mime='image.*'
              onUpload={file => this.pickFile(file)}
            />
            <TextInput
              title='Dataset name'
              onChange={e => this.setState({ name: e.target.value })}
              value={this.state.name}
            />
            <Button
              onClick={() => this.buildZip()}
            >Build Zip</Button>
            <Progress />
            {file && this.renderMetadata(file)}
          </Group>
        </div>
        <div ref={(c) => { this.canvases = c }} id='pix2wav_canvases' />
      </div>
    )
  }
  renderMetadata(file){
    const { duration } = this.state
    const size = util.hush_size(file.size)
    return (
      <div className='fileMetadata'>
        {file.size > 2 << 20 &&
          <p>
            <i>Careful, your file is larger than 2 MB.</i>
          </p>}
        <Param title='Name'>{file.name}</Param>
        <Param title='Type'>{file.type}</Param>
        <Param title='Size'><span className={size[0]}>{size[1]}</span></Param>
        <Param title='Date'>{moment(file.lastModifiedDate).format("YYYY-MM-DD h:mm a")}</Param>
        <Param title='Duration'>{Math.floor(duration) + ' s.'}</Param>
        <br />
        <Param title='Status'>{this.props.pix2wav.status}</Param>
      </div>
    )
  }
  componentDidUpdate(){
    const canvases = (this.state.frames || []).map(c => {
      this.canvases.append(c.canvas)
    })
  }
}

const mapStateToProps = state => ({
  pix2wav: state.module.pix2wav,
})

const mapDispatchToProps = (dispatch, ownProps) => ({
  actions: bindActionCreators(pix2wavActions, dispatch),
  remote: bindActionCreators(pix2wavTasks, dispatch),
  wav2pix: bindActionCreators(wav2pixActions, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(SpectrogramUpload)