Skip to content

Commit bc8245c

Browse files
committed
feat(voice): reduce branching in send path
1 parent fa36f0b commit bc8245c

File tree

2 files changed

+5
-20
lines changed

2 files changed

+5
-20
lines changed

lib/voice/SharedStream.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,13 @@ class SharedStream extends EventEmitter {
182182

183183
_incrementSequences() {
184184
for(const vc of this.voiceConnections.values()) {
185-
vc.sequence++;
186-
if(vc.sequence >= 65536) {
187-
vc.sequence -= 65536;
188-
}
185+
vc.sequence = (vc.sequence + 1) & 0xFFFF;
189186
}
190187
}
191188

192189
_incrementTimestamps(val) {
193190
for(const vc of this.voiceConnections.values()) {
194-
vc.timestamp += val;
195-
if(vc.timestamp >= 4294967295) {
196-
vc.timestamp -= 4294967295;
197-
}
191+
vc.timestamp = (vc.timestamp + val) & 0xFFFFFFFF;
198192
}
199193
}
200194

lib/voice/VoiceConnection.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,8 @@ class VoiceConnection extends EventEmitter {
641641
* @arg {Number} [frameSize] The size (in samples) of the Opus audio frame
642642
*/
643643
sendAudioFrame(frame, frameSize = this.frameSize) {
644-
this.timestamp += frameSize;
645-
if(this.timestamp >= 4294967295) {
646-
this.timestamp -= 4294967295;
647-
}
648-
649-
if(++this.sequence >= 65536) {
650-
this.sequence -= 65536;
651-
}
644+
this.timestamp = (this.timestamp + frameSize) & 0xFFFFFFFF;
645+
this.sequence = (this.sequence + 1) & 0xFFFF;
652646

653647
return this._sendAudioFrame(frame);
654648
}
@@ -796,10 +790,7 @@ class VoiceConnection extends EventEmitter {
796790
this.setSpeaking(0);
797791
}
798792
this.current.pausedTime += 4 * this.current.options.frameDuration;
799-
this.timestamp += 3 * this.current.options.frameSize;
800-
if(this.timestamp >= 4294967295) {
801-
this.timestamp -= 4294967295;
802-
}
793+
this.timestamp = (this.timestamp + 3 * this.current.options.frameSize) & 0xFFFFFFFF;
803794
this.current.timeout = setTimeout(this._send, 4 * this.current.options.frameDuration);
804795
return;
805796
} else {

0 commit comments

Comments
 (0)