Skip to content

Commit

Permalink
Improved encoding handling in text to speech
Browse files Browse the repository at this point in the history
  • Loading branch information
codemakerai-dev committed Jun 27, 2024
1 parent 061f85c commit 1f8c4c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/assistant/speechServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as http from 'http';
import {parse} from 'url';

import CodemakerService from '../service/codemakerService';
import availablePort from '../net/socket';

Check failure on line 5 in src/assistant/speechServer.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot find module '../net/socket' or its corresponding type declarations.

Check failure on line 5 in src/assistant/speechServer.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot find module '../net/socket' or its corresponding type declarations.

export default class SpeechServer {

private readonly codemakerSerivce: CodemakerService;

private readonly host = 'localhost';

private port: number = 52010;
private port: number = 52020;

private server?: http.Server;

Expand All @@ -20,7 +21,9 @@ export default class SpeechServer {
start() {
const requestListener = (req: http.IncomingMessage, res: http.ServerResponse) => {
const url = parse(req.url!, true);
this.codemakerSerivce.assistantSpeech(url.query['input'] as string).then((result) => {
const input = Buffer.from(url.query['input'] as string, 'base64url').toString('utf-8');

this.codemakerSerivce.assistantSpeech(input).then((result) => {
res.setHeader('Content-Type', 'audio/mp3');
res.writeHead(200);
res.write(result.audio);
Expand Down
12 changes: 11 additions & 1 deletion webview/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ function createMessageElement(sender, message) {
let audio = null;
equalizerButtonElement.addEventListener('click', function(event) {
if (!audio) {
audio = new Audio(`${window.speachEndpoint}?input=${message.message}`);
const input = encodeBase64url(message.message);
audio = new Audio(`${window.speachEndpoint}?input=${input}`);
audio.autoplay = true;
['pause', 'ended'].forEach(event => {
audio.addEventListener(event, () => {
Expand Down Expand Up @@ -248,3 +249,12 @@ function addCopyButtonToCodeBlocks(element) {
function isAssistant(sender) {
return sender === 'Assistant';
}

function encodeBase64url(input) {
const encoded = new TextEncoder().encode(input);
const bytes = Array.from(encoded, (byte) =>
String.fromCodePoint(byte),
).join("");
const base64Encoded = btoa(bytes);
return base64Encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}

0 comments on commit 1f8c4c4

Please sign in to comment.