-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hooks): nodes - into factory audioNodes createAudioNode
- Loading branch information
1 parent
d21e69b
commit cb2e0c1
Showing
14 changed files
with
95 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { setValueByType, } from './util'; | ||
|
||
export default async function createAudioNode( | ||
audioCtx, | ||
nodeType, | ||
properties, | ||
mediaStreamRef, | ||
) { | ||
const config = { | ||
microphone: { | ||
create: async () => { | ||
try { | ||
mediaStreamRef.current = await navigator.mediaDevices.getUserMedia({ | ||
audio: true, | ||
},); | ||
return audioCtx.createMediaStreamSource(mediaStreamRef.current,); | ||
} catch (err) { | ||
console.warn('Microphone not found', err,); | ||
return false; | ||
} | ||
}, | ||
}, | ||
oscillator: { | ||
create: () => audioCtx.createOscillator(), | ||
properties: ['type', 'detune', 'frequency', 'type', 'duration',], | ||
}, | ||
analyser: { | ||
create: () => audioCtx.createAnalyser(), | ||
properties: ['fftSize',], | ||
}, | ||
gain: { | ||
create: () => audioCtx.createGain(), | ||
properties: ['gain',], | ||
}, | ||
biquadFilter: { | ||
create: () => audioCtx.createBiquadFilter(), | ||
properties: ['type', 'detune', 'gain', 'Q', 'frequency',], | ||
}, | ||
convolver: { | ||
create: () => audioCtx.createConvolver(), | ||
properties: ['normalize', 'buffer',], | ||
}, | ||
dynamicsCompressor: { | ||
create: () => audioCtx.createDynamicsCompressor(), | ||
properties: ['threshold', 'knee', 'ratio', 'attack', 'release',], | ||
}, | ||
buffer: { | ||
create: () => audioCtx.createBufferSource(), | ||
properties: ['loop', 'detune', 'playbackRate', 'loopStart', 'loopEnd', 'buffer',], | ||
}, | ||
delay: { | ||
create: () => audioCtx.createDelay(), | ||
properties: ['delayTime',], | ||
}, | ||
pan: { | ||
create: () => audioCtx.createStereoPanner(), | ||
properties: ['pan',], | ||
}, | ||
}; | ||
|
||
const nodeConfig = config[nodeType]; | ||
if (!nodeConfig) { | ||
throw new Error(`Unsupported node type: ${nodeType}`,); | ||
} | ||
|
||
const node = await nodeConfig.create(); | ||
|
||
if (typeof node.start === 'function') { | ||
node.start(); | ||
} | ||
|
||
nodeConfig?.properties?.forEach((property,) => { | ||
if (Object.prototype.hasOwnProperty.call(properties, property,)) { | ||
if (node[property] instanceof AudioParam) { | ||
setValueByType(audioCtx, property, node[property], properties,); | ||
} else if (property === 'duration' && Number(properties.duration,)) { | ||
node.stop(audioCtx.currentTime + Number(properties.duration,),); | ||
} else { | ||
node[property] = properties[property]; | ||
} | ||
} | ||
},); | ||
|
||
return node; | ||
} | ||
|
||
export const noop = () => {}; |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters