Skip to content

Commit

Permalink
Add volume option to run-bot.js
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirox committed Aug 5, 2020
1 parent 4c9f884 commit 7300748
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
17 changes: 11 additions & 6 deletions scripts/bot/run-bot.js
Expand Up @@ -3,12 +3,13 @@ const doc = `
Usage:
./run-bot.js [options]
Options:
-h --help Show this screen
-u --url=<url> URL
-o --host=<host> Hubs host if URL is not specified [default: localhost:8080]
-r --room=<room> Room id
-a --audio=<file> File to replay for the bot's outgoing audio
-d --data=<file> File to replay for the bot's data channel
-h --help Show this screen
-u --url=<url> URL
-o --host=<host> Hubs host if URL is not specified [default: localhost:8080]
-r --room=<room> Room id
-a --audio=<file> File to replay for the bot's outgoing audio
-v --volume=<number> Audio volume (default: 1.0)
-d --data=<file> File to replay for the bot's data channel
`;

const docopt = require("docopt").docopt;
Expand Down Expand Up @@ -42,6 +43,10 @@ function log(...objs) {
if (roomOption) {
params.hub_id = roomOption;
}
const volumeOption = options["--volume"];
if (volumeOption !== null && options["--audio"]) {
params.audio_volume = volumeOption;
}

const url = `${baseUrl}?${querystring.stringify(params)}`;
log(url);
Expand Down
27 changes: 21 additions & 6 deletions src/scene-entry-manager.js
Expand Up @@ -617,13 +617,28 @@ export default class SceneEntryManager {
}

await new Promise(resolve => audioEl.addEventListener("canplay", resolve));
mediaStream.addTrack(
audioEl.captureStream
? audioEl.captureStream().getAudioTracks()[0]

const audioStream = audioEl.captureStream
? audioEl.captureStream()
: audioEl.mozCaptureStream
? audioEl.mozCaptureStream().getAudioTracks()[0]
: null
);
? audioEl.mozCaptureStream()
: null;

if (audioStream) {
let audioVolume = Number(qs.get("audio_volume") || "1.0");
if (isNaN(audioVolume)) {
audioVolume = 1.0;
}
const audioContext = THREE.AudioContext.getContext();
const audioSource = audioContext.createMediaStreamSource(audioStream);
const audioDestination = audioContext.createMediaStreamDestination();
const gainNode = audioContext.createGain();
audioSource.connect(gainNode);
gainNode.connect(audioDestination);
gainNode.gain.value = audioVolume;
mediaStream.addTrack(audioDestination.stream.getAudioTracks()[0]);
}

await NAF.connection.adapter.setLocalMediaStream(mediaStream);
audioEl.play();
};
Expand Down

0 comments on commit 7300748

Please sign in to comment.