how to connect audioAnalyser #520
davidalexandercurrie
started this conversation in
General
Replies: 2 comments 7 replies
-
never used it before, but you use it i the same way you would use it in plain threejs. if you show me a very reduced working plain threejs demo as a codesandbox that would be helpful. btw you have side effects in your render function, this is not good. sound.current.play/pause() should be inside a useEffect |
Beta Was this translation helpful? Give feedback.
7 replies
-
For future-proofing, here's the correct answer and example |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Would someone mind showing me how to wire up an audio analyser? I want to be able to change the colour of the meshPhysicalMaterial based on audio level. So I would like to know how to attach the positionalAudio to an audioAnalyzer and read the data. Thanks so much.
import React, { useRef, useState, useEffect } from 'react'
import * as THREE from 'three'
import { useThree, useLoader } from 'react-three-fiber'
import { AudioAnalyser } from 'three'
export default function Sound({ url, node, playAudio }) {
const sound = useRef()
const analyzer = useRef()
const ref = useRef()
const { camera } = useThree()
const [listener] = useState(() => new THREE.AudioListener())
const buffer = useLoader(THREE.AudioLoader, url)
if (playAudio) {
sound.current.play()
}
if (sound.current !== undefined && sound.current.isPlaying && !playAudio) {
sound.current.pause()
}
useEffect(() => {
sound.current.setBuffer(buffer)
sound.current.setRefDistance(1)
sound.current.setLoop(false)
}, [])
return (
<mesh castShadow rotation={[0, node.rotation, 0]} key={node.id} position={[node.x, 0, node.z]}>
{/* <AudioAnalyser ref={analyzer} args={[sound.current, 128]} /> */}
)
}
Beta Was this translation helpful? Give feedback.
All reactions