diff --git a/main.go b/main.go index 11637643..51a36d18 100644 --- a/main.go +++ b/main.go @@ -329,21 +329,23 @@ func fanoutAudio(audioChannel chan float32, roomID string) { return } + var count byte = 0 + for { pcm[idx] = <- audioChannel idx ++ - if idx >= len(pcm) { + if idx == len(pcm) { data := make([]byte, 640) n, err := enc.EncodeFloat32(pcm, data) - // n := opus.EncodeFloat(enc, pcm, 120, data, 1000) if err != nil { log.Println("[!] Failed to decode") continue } data = data[:n] + // data = append(data, count) isRoomRunning := false @@ -368,6 +370,7 @@ func fanoutAudio(audioChannel chan float32, roomID string) { return } idx = 0 + count = (count + 1) & 0xff } } diff --git a/static/js/ws.js b/static/js/ws.js index 881c2a95..7bcc11d6 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -72,8 +72,13 @@ function startGame() { var audioStack = []; var nextTime = 0; + var isRun = false; + function scheduleBuffers() { + if (isRun) return; + console.log("daaaa"); while ( audioStack.length) { + isRun = true; var buffer = audioStack.shift(); var source = context.createBufferSource(); source.buffer = buffer; @@ -83,6 +88,8 @@ function startGame() { source.start(nextTime); nextTime+=source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played }; + console.log("diii"); + isRun = false; } sampleRate = 16000; @@ -93,9 +100,7 @@ function startGame() { pcmChunk = decoder.decode_float(opusChunk); myBuffer = context.createBuffer(channels, pcmChunk.length, sampleRate); nowBuffering = myBuffer.getChannelData(0, bitDepth, sampleRate); - for (var i = 0; i < pcmChunk.length; i++) { - nowBuffering[i] = pcmChunk[i]; - } + nowBuffering.set(pcmChunk); return myBuffer; } @@ -105,6 +110,11 @@ function startGame() { ev.channel.onclose = () => log('channelX has closed'); ev.channel.onmessage = (e) => { + // source = context.createBufferSource(); + // source.buffer = buf; + // source.connect(context.destination); + // source.start(0); + audioStack.push(damn(e.data)); if ((init!=0) || (audioStack.length > 10)) { // make sure we put at least 10 chunks in the buffer before starting init++; diff --git a/ui/gameview.go b/ui/gameview.go index e5514387..06c41406 100644 --- a/ui/gameview.go +++ b/ui/gameview.go @@ -29,7 +29,7 @@ const ( const NumKeys = 8 const SampleRate = 16000 const Channels = 1 -const TimeFrame = 40 +const TimeFrame = 60 type GameView struct { console *nes.Console diff --git a/webrtc/webrtc.go b/webrtc/webrtc.go index 7fe5843e..a1dc26dc 100644 --- a/webrtc/webrtc.go +++ b/webrtc/webrtc.go @@ -153,6 +153,8 @@ func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, e } + // dd := false + // audioTrack, err := w.connection.CreateDataChannel("foo2", &webrtc.DataChannelInit{Ordered: &dd}) audioTrack, err := w.connection.CreateDataChannel("foo2", nil) // WebRTC state callback @@ -270,6 +272,8 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.Track, audioTrack *webrtc.DataC go func() { for w.isConnected { data := <-w.AudioChannel + // time.Sleep() + // time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) audioTrack.Send(data) } }()