-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic audio support * update * update * tts
- Loading branch information
Showing
7 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func handleTTSRequest(w http.ResponseWriter, r *http.Request) { | ||
// Create a new HTTP request with the same method, URL, and body as the original request | ||
targetURL := r.URL | ||
hostEnvVarName := "TTS_HOST" | ||
portEnvVarName := "TTS_PORT" | ||
realHost := fmt.Sprintf("http://%s:%s/api", os.Getenv(hostEnvVarName), os.Getenv(portEnvVarName)) | ||
fullURL := realHost + targetURL.String() | ||
print(fullURL) | ||
proxyReq, err := http.NewRequest(r.Method, fullURL, r.Body) | ||
if err != nil { | ||
http.Error(w, "Error creating proxy request", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Copy the headers from the original request to the proxy request | ||
for name, values := range r.Header { | ||
for _, value := range values { | ||
proxyReq.Header.Add(name, value) | ||
} | ||
} | ||
var customTransport = http.DefaultTransport | ||
|
||
// Send the proxy request using the custom transport | ||
resp, err := customTransport.RoundTrip(proxyReq) | ||
if err != nil { | ||
http.Error(w, "Error sending proxy request", http.StatusInternalServerError) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Copy the headers from the proxy response to the original response | ||
for name, values := range resp.Header { | ||
for _, value := range values { | ||
w.Header().Add(name, value) | ||
} | ||
} | ||
|
||
// Set the status code of the original response to the status code of the proxy response | ||
w.WriteHeader(resp.StatusCode) | ||
|
||
// Copy the body of the proxy response to the original response | ||
io.Copy(w, resp.Body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<script lang="ts" setup> | ||
import { ref } from "vue"; | ||
import { HoverButton, SvgIcon } from '@/components/common' | ||
interface Props { | ||
text: string | ||
} | ||
const props = defineProps<Props>() | ||
const source = ref('') | ||
const soundPlayer = ref(); | ||
const isActive = ref(false); | ||
// const speaker_id = ref('') | ||
// const style_wav = ref('') | ||
// const language_id = ref('') | ||
// Add a method called 'playAudio' to handle sending the request to the backend. | ||
async function playAudio() { | ||
console.log(props.text) | ||
if (isActive.value) { | ||
isActive.value = false | ||
} else { | ||
let text = encodeURIComponent(props.text) | ||
try { | ||
// Perform the HTTP request to send the request to the backend. | ||
const response = await fetch(`/api/tts?text=${text}`, | ||
{ cache: 'no-cache' }); | ||
if (response.ok) { | ||
// If the HTTP response is successful, parse the body into an object and play the sound. | ||
const blob = await response.blob(); | ||
source.value = URL.createObjectURL(blob); | ||
console.log(source.value); | ||
isActive.value = true; | ||
} else { | ||
console.log("request failed") | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
|
||
<template> | ||
<HoverButton :tooltip="$t('chat.playAudio')" @click="playAudio"> | ||
<span class="text-xl text-[#4f555e] dark:text-white"> | ||
<SvgIcon icon="system-uicons:audio-wave" /> | ||
</span> | ||
</HoverButton> | ||
<audio ref="soundPlayer" id="audio" autoplay :src="source" v-if="isActive" controls></audio> | ||
</template> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters