Skip to content

Commit

Permalink
Refactor createOpus()
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Apr 3, 2021
1 parent 44f20b5 commit 88ae937
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/util/Opus.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

let NativeOpus;
let OpusScript;


module.exports.createOpus = function createOpus(samplingRate, channels, bitrate) {
if(!NativeOpus && !OpusScript) {
try {
Expand All @@ -14,21 +15,20 @@ module.exports.createOpus = function createOpus(samplingRate, channels, bitrate)
}
}

let opus;
if(NativeOpus) {
return new NativeOpus.OpusEncoder(samplingRate, channels);
opus = new NativeOpus.OpusEncoder(samplingRate, channels);
} else if(OpusScript) {
opus = new OpusScript(samplingRate, channels, OpusScript.Application.AUDIO);
} else {
throw new Error("No opus encoder found, playing non-opus audio will not work.");
}

if(OpusScript) {
const opus = new OpusScript(samplingRate, channels, OpusScript.Application.AUDIO);

if(opus.setBitrate) {
opus.setBitrate(bitrate);
} else if(opus.encoderCTL) {
opus.encoderCTL(4002, bitrate);
}

return opus;
if(opus.setBitrate) {
opus.setBitrate(bitrate);
} else if(opus.encoderCTL) {
opus.encoderCTL(4002, bitrate);
}

throw new Error("No opus encoder found, playing non-opus audio will not work.");
return opus;
};

0 comments on commit 88ae937

Please sign in to comment.