diff --git a/scorch.py b/scorch.py index 38ec95b..9a78fe1 100644 --- a/scorch.py +++ b/scorch.py @@ -2,7 +2,9 @@ """ A music streaming application. """ -from aiohttp import web +import asyncio + +from aiohttp import web, WSMsgType import jinja2 import aiohttp_jinja2 from aiohttp_jinja2 import render_template @@ -14,12 +16,51 @@ import buckler_aiohttp uvloop.install() routes = web.RouteTableDef() +with open ('test.opus', 'rb') as file: + test_data = file.read() + +def chunker(seq, size): + return (seq[pos:pos + size] for pos in range(0, len(seq), size)) + +async def send_files(ws): + for n, chunk in enumerate(chunker(test_data, 200*1024)): + print(f"sending packet #{n}") + await ws.send_bytes(chunk) + await asyncio.sleep(5) + + @routes.get('/', name='index') async def index(request): """The index page.""" return render_template('index.html', request, {}) +@routes.get('/ws', name='ws') +async def websocket_handler(request): + """The websocket endpoint.""" + ws = web.WebSocketResponse(heartbeat=30) + ws_ready = ws.can_prepare(request) + if not ws_ready.ok: + return web.Response(text="Cannot start websocket.") + await ws.prepare(request) + + async for msg in ws: + if msg.type != WSMsgType.TEXT: + break + + if msg.data == "ping": + print('ping') + await ws.send_str("pong") + + if msg.data == 'test': + print('initiating test') + asyncio.create_task(send_files(ws)) + + + await ws.close() + return ws + + async def init_app(): """Initializes the application.""" app = web.Application(middlewares=[buckler_aiohttp.buckler_session]) diff --git a/static/scorch.js b/static/scorch.js index e69de29..ec33c4e 100644 --- a/static/scorch.js +++ b/static/scorch.js @@ -0,0 +1,73 @@ +var socket; +var audioCtx = new window.AudioContext(); +var source = audioCtx.createBufferSource(); +var total_raw; + +function appendBuffer(buffer1, buffer2) { + let numberOfChannels = Math.min( buffer1.numberOfChannels, buffer2.numberOfChannels ); + let tmp = audioCtx.createBuffer( numberOfChannels, (buffer1.length + buffer2.length), buffer1.sampleRate ); + for (let i=0; i Scorch + + @@ -12,6 +14,8 @@

Scorch

+ +