-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
598 additions
and
30 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
Binary file not shown.
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,86 @@ | ||
import React from 'react'; | ||
import VisualDemo from './Visualizer'; | ||
import { Button } from '@material-ui/core'; | ||
|
||
var theStream | ||
var theSource | ||
var theAnalyser | ||
var theGain | ||
|
||
class AudioDataContainer extends React.Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = {} | ||
this.frequencyBandArray = [...Array(48).keys()] | ||
} | ||
|
||
initializeAudioAnalyser = () => { | ||
const audioContext = new AudioContext(); | ||
this.getMedia().then(stream => { | ||
theStream = stream | ||
if (!audioContext || audioContext.state === 'closed') { | ||
return | ||
} | ||
const source = audioContext.createMediaStreamSource(stream); | ||
theSource = source | ||
const analyser = audioContext.createAnalyser(); | ||
theAnalyser = analyser | ||
analyser.fftSize = 4096; | ||
// source.connect(audioContext.destination); | ||
const gain = audioContext.createGain() | ||
theGain = gain.gain | ||
source.connect(gain) | ||
gain.connect(analyser) | ||
this.setState({ | ||
audioData: analyser | ||
}) | ||
}) | ||
} | ||
|
||
getFrequencyData = (styleAdjuster) => { | ||
const bufferLength = this.state.audioData && this.state.audioData.frequencyBinCount; | ||
const amplitudeArray = new Uint8Array(bufferLength); | ||
this.state.audioData && this.state.audioData.getByteFrequencyData(amplitudeArray) | ||
styleAdjuster(amplitudeArray) | ||
} | ||
getMedia = async () => { | ||
try { | ||
return await navigator.mediaDevices.getUserMedia({ | ||
// waiting for state-management | ||
// audio: navigator.mediaDevices.enumerateDevices() | ||
// .then(function (devices) { | ||
// (clientDevice === null || devices.indexOf(clientDevice === -1)) ? true : { deviceId: { exact: clientDevice } } | ||
// }), | ||
audio: true, | ||
video: false, | ||
}) | ||
} catch (err) { | ||
console.log('Error:', err) | ||
} | ||
} | ||
render() { | ||
|
||
return ( | ||
<div style={{ height: 255 }}> | ||
<VisualDemo | ||
initializeAudioAnalyser={this.initializeAudioAnalyser} | ||
frequencyBandArray={this.frequencyBandArray} | ||
getFrequencyData={this.getFrequencyData} | ||
stop={() => { | ||
theGain.value = 0 | ||
setTimeout(() => { | ||
theStream.getTracks().forEach(track => track.stop()) | ||
this.setState({ | ||
audioData: null | ||
}) | ||
}, 1000); | ||
|
||
}} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default AudioDataContainer; |
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,122 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import VisualDemo from './Visualizer'; | ||
// import soundFile from '../audio/GummyBearz.mp3' | ||
import { Button } from '@material-ui/core'; | ||
|
||
|
||
|
||
|
||
const AudioDataContainer = (props) => { | ||
|
||
const frequencyBandArray = [...Array(48).keys()] | ||
const [state, setState] = useState({}) | ||
const [dataPlaying, setDataPlaying] = useState(false); | ||
const audioContextRef = useRef(); | ||
|
||
let s | ||
|
||
useEffect(() => { | ||
const audioContext = new AudioContext(); | ||
|
||
getMedia().then(stream => { | ||
if (!audioContext || audioContext.state === 'closed') { | ||
return | ||
} | ||
const source = audioContext.createMediaStreamSource(stream); | ||
const analyser = audioContext.createAnalyser(); | ||
analyser.fftSize = 4096; | ||
source.connect(analyser); | ||
setState({ | ||
audioData: analyser | ||
}) | ||
audioContextRef.current = audioContext; | ||
audioContext.suspend(); | ||
}) | ||
|
||
|
||
return () => source.disconnect(analyser); | ||
}, []); | ||
|
||
const toggleAnalyser = () => { | ||
if (dataPlaying) { | ||
audioContextRef.current.suspend(); | ||
} else { | ||
audioContextRef.current.resume(); | ||
} | ||
setDataPlaying((play) => !play); | ||
}; | ||
|
||
const initializeAudioAnalyser = () => { | ||
if (audioContext) { | ||
|
||
getMedia().then(stream => { | ||
|
||
s = stream | ||
if (!audioContext || audioContext.state === 'closed') { | ||
return | ||
} | ||
setActiveAudio(true) | ||
console.log(audioContext) | ||
const source = audioContext.createMediaStreamSource(stream); | ||
const analyser = audioContext.createAnalyser(); | ||
analyser.fftSize = 4096; | ||
// source.connect(audioContext.destination); | ||
source.connect(analyser); | ||
console.log(analyser) | ||
setState({ | ||
audioData: analyser | ||
}) | ||
}) | ||
} | ||
|
||
} | ||
|
||
const getFrequencyData = (styleAdjuster) => { | ||
const bufferLength = state.audioData && state.audioData.frequencyBinCount; | ||
const amplitudeArray = new Uint8Array(bufferLength); | ||
state.audioData && state.audioData.getByteFrequencyData(amplitudeArray) | ||
styleAdjuster(amplitudeArray) | ||
} | ||
const getMedia = async () => { | ||
try { | ||
return await navigator.mediaDevices.getUserMedia({ | ||
audio: true, | ||
video: false, | ||
}) | ||
} catch (err) { | ||
console.log('Error:', err) | ||
} | ||
} | ||
|
||
|
||
return ( | ||
<div style={{ height: 255 }}> | ||
<button onClick={toggleAnalyser} data-playing={dataPlaying}> | ||
<span>{dataPlaying ? "Pause" : "Play"}</span> | ||
</button> | ||
<VisualDemo | ||
initializeAudioAnalyser={initializeAudioAnalyser} | ||
frequencyBandArray={frequencyBandArray} | ||
getFrequencyData={getFrequencyData} | ||
audioData={state.audioData} | ||
stop={() => { | ||
console.log("Stopping") | ||
if (audioContext) { | ||
console.log("Stopping2") | ||
s.getTracks().forEach(track => track.stop()) | ||
console.log("Stopping3") | ||
audioContext.close() | ||
console.log("Stopping4") | ||
} | ||
console.log("Stopping5") | ||
setState({ | ||
audioData: null | ||
}) | ||
}} | ||
/> | ||
</div> | ||
); | ||
|
||
} | ||
|
||
export default AudioDataContainer; |
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,43 @@ | ||
import React, { useCallback, useRef, useState } from "react"; | ||
import { RgbColorPicker } from "react-colorful"; | ||
|
||
import useClickOutside from "../lib/useClickOutside"; | ||
|
||
const ColorPicker = ({ color, onChange }) => { | ||
const popover = useRef(); | ||
const [isOpen, toggle] = useState(false); | ||
|
||
const close = useCallback(() => toggle(false), []); | ||
useClickOutside(popover, close); | ||
|
||
return ( | ||
<div style={{ position: 'relative' }}> | ||
<div | ||
style={{ | ||
width: '56px', | ||
height: '56px', | ||
borderRadius: '8px', | ||
border: '2px solid #555', | ||
boxShadow: '0 0 0 1px rgba(0, 0, 0, 0.1), inset 0 0 0 1px rgba(0, 0, 0, 0.1)', | ||
cursor: 'pointer', | ||
backgroundColor: `rgb(${color.r}, ${color.g}, ${color.b})` | ||
}} | ||
onClick={() => toggle(true)} | ||
/> | ||
|
||
{isOpen && ( | ||
<div style={{ | ||
position: 'absolute', | ||
top: '50%', | ||
right: '100%', | ||
borderRadius: '9px', | ||
boxShadow: '0 6px 12px rgba(0, 0, 0, 0.15)', | ||
} | ||
} ref={popover}> | ||
<RgbColorPicker color={color} onChange={onChange} /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
export default ColorPicker |
Oops, something went wrong.