summaryrefslogtreecommitdiff
path: root/app/client/modules/pix2wav/views/spectrogram.upload.js
blob: 40e61592c994016a0b11cd6e03eb85fca20d429d (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
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
159
160
161
162
163
164
165
166
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, Slider,
} from '../../../common'

import * as datasetActions from '../../../dataset/dataset.actions'
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: [],
      max: 3000,
      frame_step: wav2pixActions.FRAME_STEP,
    }
    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) {
      this.props.wav2pix.renderFrames(file, {})
      .then(frames => {
        console.log('got frames', frames.length)
        this.setState({
          ...this.state, frames
        })
      })
    }
  }
  buildZip(){
    const { file, max, frame_step } = this.state
    this.props.wav2pix.buildZip(this.state.name, file, {})
    .then(({ zip, filename, count }) => {
      this.props.datasetActions.uploadFile(
        this.props.module,
        this.props.folder,
        zip,
        filename,
        { count, max, frame_step }
      )
    })
  }
  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)}
            />
            {file && this.renderMetadata(file)}
          </Group>
        </div>
        <div ref={(c) => { this.canvases = c }} className='thumbs' id='pix2wav_canvases' />
      </div>
    )
  }
  renderMetadata(file){
    const { duration } = this.state
    const size = util.hush_size(file.size)
    const total_frame_count = (duration * 44100 - wav2pixActions.FRAME_LENGTH) / this.state.frame_step
    const frame_size = Math.round(wav2pixActions.FRAME_LENGTH / 44100 * 1000) + ' ms.'
    const frame_step = Math.round(this.state.frame_step / 44100 * 1000) + ' ms.'
    return (
      <div className='fileMetadata'>
        {file.size > 2 << 20 &&
          <p>
            <i>Careful, your file is larger than 2 MB.</i>
          </p>}
        <Group title='Metadata'>
          <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>
          <Param title='Frames'>{Math.floor(total_frame_count)}</Param>
          <Param title='Frame Size'>{frame_size}</Param>
          <Param title='Frame Step'>{frame_step}</Param>
          <br />
          <Param title='Status'>{this.props.pix2wav.status}</Param>
          <br />
        </Group>
        <Group title='Data settings'>
          <TextInput
            title='Dataset name'
            onChange={e => this.setState({ name: e.target.value })}
            value={this.state.name}
          />
          <Slider
            name='Frame cutoff'
            min={10} max={1000} type='int'
            value={this.state.max}
            onChange={max => this.setState({ max })}
          />
          <Slider
            name='frame step'
            min={10} max={20000} type='int'
            value={this.state.frame_step}
            onChange={frame_step => this.setState({ frame_step })}
          />
          <Button
            onClick={() => this.buildZip()}
          >Build Zip</Button>
        </Group>
        <Progress />
      </div>
    )
  }
  componentDidUpdate(){
    const canvases = (this.state.frames || []).map(c => {
      this.canvases.append(c.canvas)
    })
  }
}

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

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

export default connect(mapStateToProps, mapDispatchToProps)(SpectrogramUpload)