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, pcm: null, name: "", frames: [], frame_start: 0, max: 3000, frame_step: wav2pixActions.FRAME_STEP, } const audioElement = document.createElement('audio') audioElement.addEventListener('loadedmetadata', () => { const duration = audioElement.duration const total_frame_count = Math.floor((duration * 44100 - wav2pixActions.FRAME_LENGTH) / this.state.frame_step) this.setState({ duration, max: Math.min(this.state.max, total_frame_count), }) }) this.audioElement = audioElement } pickFile(file){ let name = file.name.split('.')[0] .replace(/\s+/g, '_') .replace(/-/g, '_') .replace(/_+/g, '_') this.setState({ file, name, pcm: '' }, () => { if (file.size < 2 << 20) { this.rebuildFrames() } }) this.audioElement.src = URL.createObjectURL(file) console.log(file.size) } rebuildFrames(){ const { file, pcm, frame_step, frame_start } = this.state this.props.wav2pix.renderFrames(pcm || file, { frame_start, frame_step }) .then(data => { console.log('got frames', data.frames.length) this.setState({ ...this.state, frames: data.frames, pcm: data.pcm, }) }) } buildZip(){ const { pcm, file, max, frame_step, frame_start } = this.state this.props.wav2pix.buildZip(this.state.name, pcm || file, { frame_start, frame_step, max }) .then(({ zip, filename, count }) => { this.props.datasetActions.uploadFile( this.props.module, this.props.folder, zip, filename, { count, max, frame_step, frame_size: wav2pixActions.FRAME_LENGTH / 44100 } ) }) } render(){ // loading={pix2wav.loading} // progress={pix2wav.progress} // id={pix2wav.folder_id} // module={pix2wavModule} // data={pix2wav.data} // folder={folder} const { file, frames } = this.state return (

{"Convert your sounds into spectrograms. "} {"Sound files can be WAV, MP3, AIFF, or FLAC. "} 2 minutes max.

this.pickFile(file)} /> {file && this.renderMetadata(file)}
{ this.canvases = c }} className='thumbs' id='pix2wav_canvases' />
) } renderMetadata(file){ const { duration } = this.state const size = util.hush_size(file.size) const total_frame_count = Math.floor((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 (
{file.size > 2 << 20 &&

Careful, your file is larger than 2 MB.

} {file.name} {file.type} {size[1]} {moment(file.lastModifiedDate).format("YYYY-MM-DD h:mm a")} {Math.floor(duration) + ' s.'}
{total_frame_count} {frame_size} {frame_step} {wav2pixActions.spectrum.fft_size}
{this.props.pix2wav.status}
this.setState({ name: e.target.value })} value={this.state.name} /> { this.setState({ frame_start }, () => { this.rebuildFrames() }) }} /> this.setState({ max })} /> { const total_frame_count = Math.floor((duration * 44100 - wav2pixActions.FRAME_LENGTH) / frame_step) this.setState({ frame_step, max: Math.min(this.state.max, total_frame_count) }, () => { this.rebuildFrames() }) }} />
) } componentDidUpdate(){ this.canvases.innerHTML = '' 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)