Skip to content

Commit

Permalink
Allow Blob to be passed directly when sending audio buffer (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnemitz authored Sep 5, 2023
1 parent 4573233 commit f8da8ab
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/realtime/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class WebSocketWrapper implements ISocketWrapper {
});
}

sendAudioBuffer(buffer: ArrayBufferLike): void {
sendAudioBuffer(buffer: ArrayBufferLike | Blob): void {
if (this.socket && this.isOpen()) {
this.socket.send(buffer);
} else console.error('Tried to send audio when socket was closed');
Expand Down
9 changes: 7 additions & 2 deletions src/realtime/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ export class NodeWebSocketWrapper implements ISocketWrapper {
});
}

sendAudioBuffer(buffer: ArrayBufferLike): void {
sendAudioBuffer(data: ArrayBufferLike | Blob): void {
if (this.socket && this.isOpen()) {
this.socket.send(buffer);
if (data instanceof Blob) {
// NOTE: Maybe we should add a log message about this potentially being poorer performance in Node
data.arrayBuffer().then((buf) => this.socket?.send(buf));
} else {
this.socket.send(data);
}
} else console.error('tried to send audio when socket was closed');
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/socket-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface ISocketWrapper {
onDisconnect?: (event: CloseEvent) => void;
connect(url: string, authToken?: string, appId?: string): Promise<void>;
disconnect(): Promise<void>;
sendAudioBuffer(buffer: ArrayBufferLike): void;
sendAudioBuffer(buffer: ArrayBufferLike | Blob): void;
sendMessage(message: string): void;
isOpen(): boolean;
}

0 comments on commit f8da8ab

Please sign in to comment.