Skip to content

Commit

Permalink
Cleanup when Piper#encode() errors
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed May 20, 2020
1 parent d32cab8 commit f19e7f7
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/voice/Piper.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Piper extends EventEmitter {
} else {
this.emit("error", new Error("An error occured trying to access that file."));
}
this.reset();
return false;
}
source = FS.createReadStream(source);
Expand Down Expand Up @@ -97,6 +98,7 @@ class Piper extends EventEmitter {
if(!options.format) {
if(!this.converterCommand) {
this.emit("error", new Error("FFmpeg/avconv was not found on this system. Playback of this audio format is impossible"));
this.reset();
return false;
}
if(typeof source === "string") {
Expand Down Expand Up @@ -168,6 +170,7 @@ class Piper extends EventEmitter {
}
} else {
this.emit("error", new Error("Unrecognized format"));
this.reset();
return false;
}

Expand Down Expand Up @@ -206,14 +209,6 @@ class Piper extends EventEmitter {
this._endStream.removeAllListeners("data");
}

for(const stream of this.streams) {
if(typeof stream.destroy === "function") {
stream.destroy();
} else {
stream.unpipe();
}
}

this.reset();
if(this.encoding) {
this.encoding = false;
Expand All @@ -222,6 +217,16 @@ class Piper extends EventEmitter {
}

reset() {
if(this.streams) {
for(const stream of this.streams) {
if(typeof stream.destroy === "function") {
stream.destroy();
} else {
stream.unpipe();
}
}
}

this.streams = [];
this._endStream = null;
this.volume = null;
Expand Down

0 comments on commit f19e7f7

Please sign in to comment.