Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions src/components/DeviceSelector.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
import React, { useCallback, useContext, useEffect, useState } from 'react'
import React, { useCallback, useContext } from 'react'
import styled from 'styled-components'
import { getMIDIDevices } from '../state'
import { getCurrentMIDIDevice, getMIDIDevices } from '../state'
import { SynthInstrumentContext } from './Engine'

export const DeviceSelector = () => {
const [state, dispatch] = useContext(SynthInstrumentContext)
const [devicesValues, setDevicesValues] = useState([])
const devices = getMIDIDevices(state)
const current = getCurrentMIDIDevice(state)

const onDeviceChange = useCallback(
event => {
const device = state.midi.devices.find(device => device.id.toString() === event.target.value.toString())

if (!device) {
return
}

dispatch({ type: 'midi_switch_device', device })
dispatch({ type: 'midi_switch_device', device: event.target.value })
},
[state.midi.devices, dispatch]
[dispatch]
)

useEffect(() => {
setDevicesValues(
devices
.filter(device => device.state === 'connected')
.map(device => ({ label: device.name, value: device.id }) )
)
}, [devices])

return (
<Container>
<StyledSelector onChange={ onDeviceChange }>
<option defaultValue disabled>Choose one MIDI device</option>
{devicesValues.map(deviceValue =>
<option key={ deviceValue.value } value={ deviceValue.value }>{deviceValue.label}</option>
<StyledSelector value={current} onChange={onDeviceChange} placeholder="No MIDI devices found">
{devices.map(([id, name]) =>
<option key={ id} value={id}>{name}</option>
)}
</StyledSelector>
</Container>
Expand Down
124 changes: 73 additions & 51 deletions src/components/Engine.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import * as Tone from 'tone'
import styled from 'styled-components'
import React, { createContext, useReducer, useEffect } from 'react'
import { FLT_FREQ_MAX, FLT_FREQ_MIN, getParams, initialState, reducer } from '../state'

let engine = {
oscillator1: null,
oscillator2: null,
filter: {
unit: null,
envelope: null,
},
volume: null,
distortion: null,
delay: null,
analyzer: null,
reverb: null,
shifter: null,
}
import React, { createContext, useReducer, useEffect, useRef } from 'react'
import { config, FLT_FREQ_MAX, FLT_FREQ_MIN } from '../config'
import { getParams, initialState, reducer } from '../state'
import { mapRangeControl } from '../utils/math'

export const SynthInstrumentContext = createContext([initialState, () => null])

export const Engine = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState)
const engine = useRef({
oscillator1: null,
oscillator2: null,
filter: {
unit: null,
envelope: null,
},
volume: null,
distortion: null,
delay: null,
analyzer: null,
reverb: null,
shifter: null,
})
const { initialized } = state
const params = getParams(state)

Expand Down Expand Up @@ -51,7 +52,7 @@ export const Engine = ({ children }) => {

delay.chain(distortion, reverb, filter, shifter, analyzer, volume, Tone.Destination)

engine = {
engine.current = {
oscillator1,
oscillator2,
filter: {
Expand All @@ -70,15 +71,36 @@ export const Engine = ({ children }) => {
dispatch({ type: 'init_engine' })

Tone.start()
}, [])
}, [engine])

useEffect(() => {
if (!initialized) {
return
}

Object.entries(state.midi_map).forEach(([param, control]) => {
if (config[param].mappable) {
dispatch({
type: 'set_parameter',
name: param,
value: mapRangeControl(
state.cc[control] || config[param].default,
config[param].min,
config[param].max
)
})
}
})

}, [initialized, dispatch, state.cc, state.midi_map])

useEffect(() => {
if (!initialized) {
return
}

if (state.analyzer.requesting) {
dispatch({ type: 'set_analyzer', values: engine.analyzer.getValue() })
dispatch({ type: 'set_analyzer', values: engine.current.analyzer.getValue() })
}

}, [initialized, dispatch, state.analyzer])
Expand All @@ -95,19 +117,19 @@ export const Engine = ({ children }) => {
const parseFrequencies = notes => notes.map(({ note }) => Tone.Midi(note).toFrequency())

toPlays.forEach(note => {
engine.oscillator1.triggerAttack(
engine.current.oscillator1.triggerAttack(
Tone.Midi(note.note).toFrequency(), Tone.now(), note.velocity
)})

toPlays.forEach(note => engine.oscillator2.triggerAttack(
toPlays.forEach(note => engine.current.oscillator2.triggerAttack(
Tone.Midi(note.note).toFrequency(), Tone.now(), note.velocity
))

engine.filter.envelope.triggerAttack()
engine.current.filter.envelope.triggerAttack()

engine.oscillator1.triggerRelease(parseFrequencies(toRelease))
engine.oscillator2.triggerRelease(parseFrequencies(toRelease))
engine.filter.envelope.triggerRelease()
engine.current.oscillator1.triggerRelease(parseFrequencies(toRelease))
engine.current.oscillator2.triggerRelease(parseFrequencies(toRelease))
engine.current.filter.envelope.triggerRelease()

toPlays.map(({ note }) => dispatch({ type: 'note_triggered', note }))
}, [state.notes, dispatch, initialized])
Expand All @@ -118,23 +140,23 @@ export const Engine = ({ children }) => {
}

const decibels = Tone.gainToDb(params.master_vol)
engine.volume.set({ volume: decibels })
engine.current.volume.set({ volume: decibels })
}, [params.master_vol, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.oscillator1.set({
engine.current.oscillator1.set({
envelope: {
attack: params.osc1_env_atk,
decay: params.osc1_env_dec,
sustain: params.osc1_env_sus,
release: params.osc1_env_rel,
},
})
engine.oscillator2.set({
engine.current.oscillator2.set({
envelope: {
attack: params.osc2_env_atk,
decay: params.osc2_env_dec,
Expand All @@ -159,7 +181,7 @@ export const Engine = ({ children }) => {
return
}

engine.filter.envelope.set({
engine.current.filter.envelope.set({
attack: params.flt_env_atk,
decay: params.flt_env_dec,
sustain: params.flt_env_sus,
Expand All @@ -173,16 +195,16 @@ export const Engine = ({ children }) => {
return
}

engine.oscillator1.set({ oscillator: { type: params.osc1_type } })
engine.oscillator2.set({ oscillator: { type: params.osc2_type } })
engine.current.oscillator1.set({ oscillator: { type: params.osc1_type } })
engine.current.oscillator2.set({ oscillator: { type: params.osc2_type } })
}, [params.osc1_type, params.osc2_type, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.filter.unit.set({ type: params.flt_type })
engine.current.filter.unit.set({ type: params.flt_type })
}, [params.flt_type, params.flt_freq, params.flt_res, initialized])

useEffect(() => {
Expand All @@ -191,11 +213,11 @@ export const Engine = ({ children }) => {
}

if (params.flt_env_mix > 0) {
engine.filter.scale.min = params.flt_freq
engine.filter.scale.max = params.flt_freq + (FLT_FREQ_MAX - params.flt_freq) * params.flt_env_mix
engine.current.filter.scale.min = params.flt_freq
engine.current.filter.scale.max = params.flt_freq + (FLT_FREQ_MAX - params.flt_freq) * params.flt_env_mix
} else {
engine.filter.scale.min = FLT_FREQ_MIN + (params.flt_freq - FLT_FREQ_MIN) * (1 - Math.abs(params.flt_env_mix))
engine.filter.scale.max = params.flt_freq
engine.current.filter.scale.min = FLT_FREQ_MIN + (params.flt_freq - FLT_FREQ_MIN) * (1 - Math.abs(params.flt_env_mix))
engine.current.filter.scale.max = params.flt_freq
}

}, [params.flt_env_mix, params.flt_freq, initialized])
Expand All @@ -205,62 +227,62 @@ export const Engine = ({ children }) => {
return
}

engine.oscillator1.set({ oscillator: { detune: params.osc1_detune + params.osc1_pitch * 100 } })
engine.oscillator2.set({ oscillator: { detune: params.osc2_detune + params.osc2_pitch * 100 } })
engine.current.oscillator1.set({ oscillator: { detune: params.osc1_detune + params.osc1_pitch * 100 } })
engine.current.oscillator2.set({ oscillator: { detune: params.osc2_detune + params.osc2_pitch * 100 } })
}, [params.osc1_detune, params.osc1_pitch, params.osc2_detune, params.osc2_pitch, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.oscillator1.set({ oscillator: { volume: Tone.gainToDb(params.osc1_vol) } })
engine.oscillator2.set({ oscillator: { volume: Tone.gainToDb(params.osc2_vol) } })
engine.current.oscillator1.set({ oscillator: { volume: Tone.gainToDb(params.osc1_vol) } })
engine.current.oscillator2.set({ oscillator: { volume: Tone.gainToDb(params.osc2_vol) } })
}, [params.osc1_vol, params.osc2_vol, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.oscillator1.set({ oscillator: { phase: params.osc1_phase } })
engine.oscillator2.set({ oscillator: { phase: params.osc2_phase } })
engine.current.oscillator1.set({ oscillator: { phase: params.osc1_phase } })
engine.current.oscillator2.set({ oscillator: { phase: params.osc2_phase } })
}, [params.osc1_phase, params.osc2_phase, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.distortion.set({ distortion: params.dist_amt })
engine.current.distortion.set({ distortion: params.dist_amt })
}, [params.dist_amt, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.delay.set({ wet: params.delay_wet })
engine.delay.set({ delayTime: params.delay_time })
engine.delay.set({ feedback: params.delay_feed })
engine.current.delay.set({ wet: params.delay_wet })
engine.current.delay.set({ delayTime: params.delay_time })
engine.current.delay.set({ feedback: params.delay_feed })
}, [params.delay_wet, params.delay_time, params.delay_feed, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.reverb.set({ wet: params.verb_wet })
engine.reverb.set({ decay: params.verb_time })
engine.current.reverb.set({ wet: params.verb_wet })
engine.current.reverb.set({ decay: params.verb_time })
}, [params.verb_wet, params.verb_time, initialized])

useEffect(() => {
if (!initialized) {
return
}

engine.shifter.set({ wet: params.shft_wet })
engine.shifter.set({ frequency: params.shft_freq })
engine.current.shifter.set({ wet: params.shft_wet })
engine.current.shifter.set({ frequency: params.shft_freq })
}, [params.shft_wet, params.shft_freq, initialized])

return (
Expand Down
Loading