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

119 lines
2.8 KiB
React
Raw Permalink Normal View History

import { Component } from "react";
import * as _VAD from "vad.js/lib/vad.js";
import Percentage from "./percentage";
import Kaburi from "./kaburi";
export default class extends Component {
state = {
audioContext: new AudioContext(),
audioStream: null,
left: null,
2018-11-15 16:41:30 +09:00
right: null,
activity: [],
kaburiActivity: []
};
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 putKaburi = () => {
if (this.state.left !== null && this.state.right !== null) {
this.setState({
kaburiActivity: [
...this.state.kaburiActivity,
{
startTime: Math.max(this.state.left, this.state.right),
endTime: new Date().getTime()
}
]
});
}
};
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: () => {
putKaburi();
2018-11-15 16:41:30 +09:00
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: () => {
putKaburi();
2018-11-15 16:41:30 +09:00
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>
<Percentage activity={this.state.activity} />
<Kaburi activity={this.state.kaburiActivity} />
2018-11-15 16:41:30 +09:00
</div>
);
}
}