1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-31 06:18:07 +00:00
_/components/vad.jsx

101 lines
2.3 KiB
React
Raw Normal View History

import { Component } from "react";
import * as _VAD from "vad.js/lib/vad.js";
export default class extends Component {
state = {
audioContext: new AudioContext(),
audioStream: null,
left: null,
2018-11-15 16:41:30 +09:00
right: null,
activity: []
};
componentDidMount() {
const audioContext = this.state.audioContext;
const leftNode = audioContext.createGain();
const rightNode = audioContext.createGain();
const splitter = audioContext.createChannelSplitter(2);
splitter.connect(
leftNode,
0
);
splitter.connect(
rightNode,
1
);
const start = stream => {
2018-11-15 16:41:30 +09:00
this.setState({ audioStream: stream });
audioContext.createMediaStreamSource(stream).connect(splitter);
VAD.bind({})({
source: leftNode,
2018-11-15 16:41:30 +09:00
voice_stop: () => {
this.setState({
left: null,
activity: [
...this.state.activity,
{
ch: "l",
startTime: this.state.left,
endTime: new Date().getTime()
}
]
});
},
2018-11-15 16:41:30 +09:00
voice_start: () => {
this.setState({ left: new Date().getTime() });
}
});
VAD.bind({})({
source: rightNode,
2018-11-15 16:41:30 +09:00
voice_stop: () => {
this.setState({
right: null,
activity: [
...this.state.activity,
{
ch: "r",
startTime: this.state.right,
endTime: new Date().getTime()
}
]
});
},
2018-11-15 16:41:30 +09:00
voice_start: () => {
this.setState({ right: new Date().getTime() });
}
});
};
navigator.mediaDevices
.getUserMedia({ audio: { echoCancellation: false } }) // モノラル防ぐ
.then(start)
.catch(console.error);
}
componentWillUnmount() {
this.state.audioStream.getTracks().forEach(t => t.stop());
}
render() {
return (
2018-11-15 16:41:30 +09:00
<div>
<dl>
<dt></dt>
<dd>{this.state.left && "発話中..."}</dd>
<dt></dt>
<dd>{this.state.right && "発話中..."}</dd>
</dl>
<ul>
{this.state.activity.map((l, i) => (
<li key={i}>{JSON.stringify(l)}</li>
))}
</ul>
</div>
);
}
}