Код: Выделить всё
import pyaudiowpatch as pyaudio
import asyncio
from websockets.server import serve
CHUNK_SIZE = 512
async def main():
with pyaudio.PyAudio() as p:
# 23 is the index of the device i'm trying to get the data from
audio_device = p.get_device_info_by_index(23)
with p.open(
format=pyaudio.paInt16,
channels=audio_device["maxInputChannels"],
rate=int(audio_device["defaultSampleRate"]),
frames_per_buffer=CHUNK_SIZE,
input=True,
input_device_index=audio_device["index"]
) as stream:
async def handler(ws):
print(f"Connection from {ws}")
while stream.is_active():
chunk = stream.read(CHUNK_SIZE)
await ws.send(chunk)
print(f"Sent chunk to {ws}")
print(f"Closing connection to {ws}")
async with serve(handler, host="localhost", port=8081):
print("Listening...")
await asyncio.Future()
asyncio.run(main())
Код: Выделить всё
const host = "ws://localhost:8081"
const ws = new WebSocket(host, "testProtocol")
const audioContext = new AudioContext()
const source = audioContext.createBufferSource()
source.connect(audioContext.destination)
document.getElementById("playButton").onclick = () => {
source.start()
}
ws.onmessage = (event) => {
event.data.arrayBuffer().then(data => {
console.log(new Uint8Array(data)) // Shows that the data being received matches that being sent
audioContext.decodeAudioData(new Uint8Array(data).buffer, (buffer) => {
source.buffer = buffer
})
})
}
Как правильно воспроизвести звук, передаваемый в потоковом режиме на веб-страницу?
Подробнее здесь: https://stackoverflow.com/questions/780 ... javascript