Skip to content

Commit

Permalink
audio/internal/readerdriver: Remove goroutines for Windows
Browse files Browse the repository at this point in the history
Updates #1768
  • Loading branch information
hajimehoshi committed Aug 18, 2021
1 parent ded679c commit cc5847f
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions audio/internal/readerdriver/driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package readerdriver

import (
"fmt"
"sync"
"unsafe"

"golang.org/x/sys/windows"
Expand Down Expand Up @@ -73,7 +72,7 @@ type context struct {
waveOut uintptr
headers []*header

cond *sync.Cond
buf32 []float32

players *players
}
Expand All @@ -88,7 +87,6 @@ func NewContext(sampleRate, channelNum, bitDepthInBytes int) (Context, chan stru
sampleRate: sampleRate,
channelNum: channelNum,
bitDepthInBytes: bitDepthInBytes,
cond: sync.NewCond(&sync.Mutex{}),
players: newPlayers(),
}
theContext = c
Expand Down Expand Up @@ -126,7 +124,10 @@ func NewContext(sampleRate, channelNum, bitDepthInBytes int) (Context, chan stru
c.headers = append(c.headers, h)
}

go c.loop()
c.buf32 = make([]float32, headerBufferSize/4)
for range c.headers {
c.appendBuffers()
}

return c, ready, nil
}
Expand Down Expand Up @@ -161,36 +162,22 @@ var waveOutOpenCallback = windows.NewCallbackCDecl(func(hwo, uMsg, dwInstance, d
if uMsg != womDone {
return 0
}
theContext.cond.Signal()
theContext.appendBuffers()
return 0
})

func (c *context) loop() {
buf32 := make([]float32, headerBufferSize/4)
for {
c.appendBuffer(buf32)
}
}

func (c *context) appendBuffer(buf32 []float32) {
c.cond.L.Lock()
defer c.cond.L.Unlock()

for !c.isHeaderAvailable() {
c.cond.Wait()
}

for i := range buf32 {
buf32[i] = 0
func (c *context) appendBuffers() {
for i := range c.buf32 {
c.buf32[i] = 0
}
c.players.read(buf32)
c.players.read(c.buf32)

for _, h := range c.headers {
if h.IsQueued() {
continue
}

if err := h.Write(buf32); err != nil {
if err := h.Write(c.buf32); err != nil {
// This error can happen when e.g. a new HDMI connection is detected (hajimehoshi/oto#51).
const errorNotFound = 1168
if werr := err.(*winmmError); werr.fname == "waveOutWrite" {
Expand All @@ -204,6 +191,5 @@ func (c *context) appendBuffer(buf32 []float32) {
// TODO: Treat the error corretly
panic(fmt.Errorf("readerdriver: Queueing the header failed: %v", err))
}
return
}
}

0 comments on commit cc5847f

Please sign in to comment.