Skip to content

Commit

Permalink
Add OggOpusPassthroughTransformer, revert FFmpegOggTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Brainicism committed Apr 11, 2021
1 parent f3a8702 commit cd3d054
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/voice/Piper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const util = require("util");
const Base = require("../structures/Base");
const DCAOpusTransformer = require("./streams/DCAOpusTransformer");
const FFmpegOggTransformer = require("./streams/FFmpegOggTransformer");
const OggOpusPassthroughTransformer = require("./streams/OggOpusPassthroughTransformer");
const FFmpegPCMTransformer = require("./streams/FFmpegPCMTransformer");
const FS = require("fs");
const HTTP = require("http");
Expand Down Expand Up @@ -146,7 +147,15 @@ class Piper extends EventEmitter {
this._dataPacketMin = 4;
} else {
if(this.libopus) {
if(typeof source === "string") {
if(options.opusPassthrough) {
this.streams.push(source = source.pipe(new OggOpusPassthroughTransformer({
command: this.converterCommand,
encoderArgs: options.encoderArgs,
inputArgs: options.inputArgs,
format: options.format,
frameDuration: options.frameDuration
})).once("error", (e) => this.stop(e)));
} else if(typeof source === "string") {
this.streams.push(source = new FFmpegOggTransformer({
command: this.converterCommand,
input: source,
Expand Down
2 changes: 1 addition & 1 deletion lib/voice/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class VoiceConnection extends EventEmitter {
options.frameDuration = options.frameDuration || this.frameDuration;
options.frameSize = options.frameSize || options.samplingRate * options.frameDuration / 1000;
options.pcmSize = options.pcmSize || options.frameSize * 2 * this.channels;

options.opusPassthrough = options.opusPassthrough || true;
if(!this.piper.encode(source, options)) {
this.emit("error", new Error("Unable to encode source"));
return;
Expand Down
5 changes: 4 additions & 1 deletion lib/voice/streams/FFmpegOggTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = function(options = {}) {
options.frameDuration = 60;
}
let inputArgs = [
"-analyzeduration", "0",
"-loglevel", "24"
].concat(options.inputArgs || []);
if(options.format === "pcm") {
Expand All @@ -24,7 +25,9 @@ module.exports = function(options = {}) {
"-vn"
);
const outputArgs = [
"-c", "copy",
"-c:a", "libopus",
"-vbr", "on",
"-frame_duration", "" + options.frameDuration,
"-f", "ogg",
"-"
];
Expand Down
32 changes: 32 additions & 0 deletions lib/voice/streams/OggOpusPassthroughTransformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

const FFmpegDuplex = require("./FFmpegDuplex");

module.exports = function(options = {}) {
if(!options.command) {
throw new Error("Invalid converter command");
}
if(options.frameDuration === undefined) {
options.frameDuration = 60;
}
let inputArgs = [
"-loglevel", "24"
].concat(options.inputArgs || []);
if(options.format === "pcm") {
inputArgs = inputArgs.concat(
"-f", "s16le",
"-ar", "48000",
"-ac", "2"
);
}
inputArgs = inputArgs.concat(
"-i", options.input || "-",
"-vn"
);
const outputArgs = [
"-c", "copy",
"-f", "ogg",
"-"
];
return FFmpegDuplex.spawn(options.command, inputArgs.concat(options.encoderArgs || [], outputArgs));
};

0 comments on commit cd3d054

Please sign in to comment.