Skip to content

Commit

Permalink
main websocket 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
BangProx committed Aug 16, 2024
1 parent e679117 commit 6e5673c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
path = './templates/index.html'
audio_path = './templates/audio.html'
audio_file_path = './templates/audio_file.html'
text_to_audio_path = './templates/texttoaudio.html'
@app.get("/")
async def get():
return FileResponse(audio_file_path)
return FileResponse(text_to_audio_path)

@app.get("/items/{item_id}")
def read_item(item_id : int, q:Union[str, None] = None):
Expand Down
20 changes: 14 additions & 6 deletions routers/websocket.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import APIRouter, WebSocket
import asyncio
import os
import uuid
import json
from queue import Queue
Expand Down Expand Up @@ -106,7 +107,7 @@ async def websocket_task(websocket: WebSocket):

#======= WebSocket ========#
@router.websocket("/tts/ws")
async def websocket_endpoint(websocket: WebSocket):
async def tts_endpoint(websocket: WebSocket):
print("Configuring BE Socket")
await websocket.accept()
print("BE Socket Accepted")
Expand All @@ -115,7 +116,7 @@ async def websocket_endpoint(websocket: WebSocket):


@router.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
async def websocket_test_endpoint(websocket: WebSocket):
print("Configuring BE Socket")
await websocket.accept()
print("BE Socket Accepted")
Expand All @@ -125,7 +126,7 @@ async def websocket_endpoint(websocket: WebSocket):
await websocket.send_text(f"Echo: {data}")

@router.websocket("/audio/ws")
async def websocket_endpoint(websocket: WebSocket):
async def websocket_audio_test_endpoint(websocket: WebSocket):
await websocket.accept()
print("WebSocket connection accepted.")

Expand All @@ -145,15 +146,22 @@ async def websocket_endpoint(websocket: WebSocket):

#======= WebSocket ========#
@router.websocket("/tts")
async def websocket_endpoint(websocket: WebSocket):
async def websocket_tts_test_endpoint(websocket: WebSocket):
print("Configuring BE Socket")
await websocket.accept()
print("BE Socket Accepted")
try:
while True:
data = await websocket.receive_text()
print("data:\n",data)
processed_result = read_wav("../received_audio.wav")
print(f"Received text data: {data}")
audio_file_path = "../received_audio.wav"
if os.path.exists(audio_file_path):
with open(audio_file_path, "rb") as audio_file:
while chunk := audio_file.read(1024):
await websocket.send_bytes(chunk)
else:
await websocket.send_text("Audio file not found.")
processed_result = read_wav(audio_file_path)
await websocket.send_bytes(processed_result)
except Exception as e:
print("Exception as {e}")
Expand Down
40 changes: 40 additions & 0 deletions templates/texttoaudio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Audio Stream</title>
</head>
<body>
<h1>WebSocket Audio Stream</h1>
<button onclick="connectWebSocket()">Connect WebSocket</button>
<script>
let socket;

function connectWebSocket() {
socket = new WebSocket("ws://localhost:8000/ws");
socket.onopen = function(event) {
console.log("Connected to WebSocket server.");
socket.send("start");
};

socket.onmessage = function(event) {
console.log("Received audio data:", event.data);
// Blob으로 변환하여 오디오 재생
let audioBlob = new Blob([event.data], { type: 'audio/wav' });
let audioUrl = URL.createObjectURL(audioBlob);
let audio = new Audio(audioUrl);
audio.play();
};

socket.onclose = function(event) {
console.log("Disconnected from WebSocket server.");
};

socket.onerror = function(error) {
console.log("WebSocket error:", error);
};
}
</script>
</body>
</html>
"""

0 comments on commit 6e5673c

Please sign in to comment.