Skip to content

Commit ea085ed

Browse files
committed
WIP wavs to json ( needs noise ).
1 parent c31cc61 commit ea085ed

File tree

7 files changed

+28
-47
lines changed

7 files changed

+28
-47
lines changed

src/App.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ import HorizontalSlider from './views/Components/HorizontalSlider.js'
1616
import Keyboard from './views/Keyboard/Keyboard.js'
1717
import EventEmitter from 'event-emitter'
1818

19+
import WaveTableData from './data/WaveTableData.json'
20+
1921
const audioContext = new AudioContext()
2022
const eventEmitter = new EventEmitter()
2123

2224
class App extends React.Component {
2325
constructor (props) {
2426
super(props)
25-
let wavsURL = 'http://davedave.us/wavetable-synth/wavefiles.php'
26-
wavsURL += '?rand=' + Math.random().toString() // Bust annoying long cache on my server.
27-
axios.get(wavsURL, { responseType: 'json' })
28-
.then(function (response) {
29-
let action = Actions.waveFileListLoaded(response.data)
30-
props.dispatch(action)
31-
})
27+
axios.get(WaveTableData).then(function (response) {
28+
let action = Actions.waveFileListLoaded(response.data)
29+
props.dispatch(action)
30+
})
3231
}
3332

3433
render () {

src/data/Reducers.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let Keyboard = {}
1010

1111
// The list of waveforms should only be loaded once when the page loads,
1212
// not reloaded each time a preset changes.
13-
let OSC_WAV_FILES = []
13+
let OSC_WAV_FILES = {}
1414

1515
// LFOs
1616
const LFODestinations = [
@@ -357,8 +357,10 @@ function OscillatorsReducer (state, action) {
357357
case 'WAVE_FILE_LIST_LOADED':
358358
state = state.map(function (osc) {
359359
OSC_WAV_FILES = action.files
360-
if (!osc.files.length) {
361-
osc.files = [...action.files] // Only set this once per page load.
360+
let hasLoaded = Object.keys(osc.files).length > 0
361+
// Only set the wavetable data once per page load.
362+
if (!hasLoaded) {
363+
osc.files = Object.assign({}, action.files)
362364
}
363365
return osc
364366
})

src/data/WaveTableData.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/views/Oscillator/OscillatorView.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ OscillatorView.propTypes = {
109109
amount: React.PropTypes.number,
110110
// channelDataA: React.PropTypes.array, ?
111111
// channelDataB: React.PropTypes.array,
112-
files: React.PropTypes.array,
112+
files: React.PropTypes.object,
113113
fileA: React.PropTypes.string,
114114
fileB: React.PropTypes.string
115115
}

src/views/Oscillator/WaveFileLoader.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ Dropdown, shows a list of files.
33
On select, loads the file.
44
*/
55
import React from 'react'
6-
import axios from 'axios'
76
import Select from 'react-select'
87
import {connect} from 'react-redux'
98
import Actions from '../../data/Actions.js'
10-
const baseUrl = 'http://davedave.us/wavetable-synth/wavs/'
119

1210
class WaveFileLoader extends React.Component {
1311
constructor (props) {
@@ -18,16 +16,16 @@ class WaveFileLoader extends React.Component {
1816
}
1917

2018
componentWillUpdate (nextProps, nextState) {
21-
if (this.props.files.length !== nextProps.files.length) {
22-
let options = nextProps.files.map((file) => {
19+
if (this.props.files !== nextProps.files) {
20+
let options = Object.keys(nextProps.files).map((file) => {
2321
let fileName = file.split('.')[0]
2422
return { value: fileName, label: file.split('.')[0] }
2523
})
2624
this.setState({options})
2725
}
2826

2927
// Wait until the file list is ready.
30-
if (this.props.files.length) {
28+
if (Object.keys(this.props.files).length) {
3129
this.loadWaveFile(nextProps.selectedFile)
3230
}
3331
}
@@ -39,29 +37,25 @@ class WaveFileLoader extends React.Component {
3937
}
4038

4139
loadWaveFile (fileName) {
42-
const {id, side, audioContext, dispatch} = this.props
43-
const filePath = baseUrl + fileName + '.wav'
40+
const {id, side, dispatch} = this.props
4441

4542
if (fileName.toLowerCase().indexOf('noise') !== -1) {
46-
// Generate some noise, don't bother to load and parse a file.
47-
// The "noise.wav" is actually not noise, it's just another wavetable
48-
// file renamed to "noise.wav" so it shows up as an option in the dropdown.
43+
// 600 is too short for a white noise osc, so we generate 1 second
44+
// of noise here. All other waves are 600 samples long.
4945
let channelData = new Float32Array(44100)
5046
for (var i = 0; i < 44100; i++) {
5147
channelData[i] = Math.random()
5248
}
5349
let action = Actions.waveFileLoadCompleted(id, side, channelData)
5450
dispatch(action)
5551
} else {
56-
axios.get(filePath, { responseType: 'arraybuffer' })
57-
.then(function (response) {
58-
// Extract the data to draw each waveform.
59-
audioContext.decodeAudioData(response.data).then(function (buffer) {
60-
let channelData = buffer.getChannelData(0)
61-
let action = Actions.waveFileLoadCompleted(id, side, channelData)
62-
dispatch(action)
63-
})
64-
})
52+
// Convert regular array from JSON file to the format the osc needs:
53+
let channelData = new Float32Array(600)
54+
this.props.files[fileName].forEach((data, index) => {
55+
channelData[index] = data
56+
})
57+
let action = Actions.waveFileLoadCompleted(id, side, channelData)
58+
dispatch(action)
6559
}
6660
}
6761

@@ -85,7 +79,7 @@ class WaveFileLoader extends React.Component {
8579
}
8680
}
8781
WaveFileLoader.propTypes = {
88-
files: React.PropTypes.array,
82+
files: React.PropTypes.object,
8983
id: React.PropTypes.string
9084
}
9185
WaveFileLoader.defaultProps = {

src/wavefiles.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

webpack.config.babel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = function () {
2222
{test: /\.css$/, loaders: ['style-loader', 'css-loader']},
2323
{test: /\.scss$/, loaders: ['style-loader', 'css-loader', 'sass-loader']},
2424
{test: /\.png/, loaders: ['file-loader']},
25+
{test: /\.json/, loaders: ['file-loader']},
2526
{test: /\.worker\.js$/, loaders: ['worker-loader', 'babel-loader']}
2627
]
2728
}

0 commit comments

Comments
 (0)