audio/internal/readerdriver: Revert 2 commits

This reverts commit 43789c72a9.
This reverts commit 100b42513e.

Updates #1652
Updates #1653

Reason: This causes noises on examples/piano when a lot of notes played at the same time
This commit is contained in:
Hajime Hoshi 2021-05-28 22:18:49 +09:00
parent 43789c72a9
commit 3e113e69e8

View File

@ -18,7 +18,6 @@ import (
"io" "io"
"runtime" "runtime"
"sync" "sync"
"sync/atomic"
"unsafe" "unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
@ -98,32 +97,32 @@ type players struct {
context *context context *context
players map[*playerImpl]struct{} players map[*playerImpl]struct{}
buf []byte buf []byte
err atomic.Value err error
waveOut uintptr waveOut uintptr
headers []*header headers []*header
// cond protects only the member 'players' and waveOut functions.
// The other members don't have to be protected.
cond *sync.Cond cond *sync.Cond
} }
func (p *players) setContext(context *context) { func (p *players) setContext(context *context) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
p.context = context p.context = context
} }
func (p *players) add(player *playerImpl) error { func (p *players) add(player *playerImpl) error {
if err := p.err.Load(); err != nil { p.cond.L.Lock()
return err.(error) defer p.cond.L.Unlock()
if p.err != nil {
return p.err
} }
p.cond.L.Lock()
if p.players == nil { if p.players == nil {
p.players = map[*playerImpl]struct{}{} p.players = map[*playerImpl]struct{}{}
} }
p.players[player] = struct{}{} p.players[player] = struct{}{}
p.cond.L.Unlock()
p.cond.Signal() p.cond.Signal()
if p.waveOut != 0 { if p.waveOut != 0 {
@ -161,8 +160,7 @@ func (p *players) add(player *playerImpl) error {
p.headers = append(p.headers, h) p.headers = append(p.headers, h)
} }
// There should be only one players in total. if err := p.readAndWriteBuffers(); err != nil {
if err := p.readAndWriteBuffers([]*playerImpl{player}); err != nil {
return err return err
} }
@ -172,14 +170,16 @@ func (p *players) add(player *playerImpl) error {
} }
func (p *players) remove(player *playerImpl) error { func (p *players) remove(player *playerImpl) error {
if err := p.err.Load(); err != nil { p.cond.L.Lock()
return err.(error) defer p.cond.L.Unlock()
return p.removeImpl(player)
} }
p.cond.L.Lock() func (p *players) removeImpl(player *playerImpl) error {
if p.err != nil {
return p.err
}
delete(p.players, player) delete(p.players, player)
p.cond.L.Unlock()
return nil return nil
} }
@ -206,36 +206,31 @@ func (p *players) shouldWait() bool {
} }
func (p *players) loop() { func (p *players) loop() {
var players []*playerImpl
for { for {
p.cond.L.Lock() p.cond.L.Lock()
for p.shouldWait() { for p.shouldWait() {
p.cond.Wait() p.cond.Wait()
} }
players = players[:0]
for pl := range p.players {
players = append(players, pl)
}
p.cond.L.Unlock()
if p.waveOut == 0 { if p.waveOut == 0 {
p.cond.L.Unlock()
return return
} }
if err := p.readAndWriteBuffers(players); err != nil { if err := p.readAndWriteBuffers(); err != nil {
p.err.Store(err) p.err = err
p.cond.L.Unlock()
break break
} }
p.cond.L.Unlock()
} }
} }
func (p *players) suspend() error { func (p *players) suspend() error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
if p.waveOut == 0 { if p.waveOut == 0 {
return nil return nil
} }
p.cond.L.Lock()
defer p.cond.L.Unlock()
if err := waveOutPause(p.waveOut); err != nil { if err := waveOutPause(p.waveOut); err != nil {
return err return err
} }
@ -243,12 +238,12 @@ func (p *players) suspend() error {
} }
func (p *players) resume() error { func (p *players) resume() error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
if p.waveOut == 0 { if p.waveOut == 0 {
return nil return nil
} }
p.cond.L.Lock()
defer p.cond.L.Unlock()
if err := waveOutRestart(p.waveOut); err != nil { if err := waveOutRestart(p.waveOut); err != nil {
return err return err
} }
@ -265,8 +260,8 @@ var waveOutOpenCallback = windows.NewCallbackCDecl(func(hwo, uMsg, dwInstance, d
return 0 return 0
}) })
func (p *players) readAndWriteBuffers(players []*playerImpl) error { func (p *players) readAndWriteBuffers() error {
if len(players) == 0 { if len(p.players) == 0 {
return nil return nil
} }
@ -288,7 +283,7 @@ func (p *players) readAndWriteBuffers(players []*playerImpl) error {
// waveOutSetVolume is not used since it doesn't work correctly in some environments. // waveOutSetVolume is not used since it doesn't work correctly in some environments.
var volumes []float64 var volumes []float64
var bufs [][]byte var bufs [][]byte
for _, pl := range players { for pl := range p.players {
buf := make([]byte, n) buf := make([]byte, n)
n := pl.read(buf) n := pl.read(buf)
bufs = append(bufs, buf[:n]) bufs = append(bufs, buf[:n])
@ -347,8 +342,6 @@ func (p *players) readAndWriteBuffers(players []*playerImpl) error {
p.buf = append(p.buf, buf...) p.buf = append(p.buf, buf...)
} }
p.cond.L.Lock()
defer p.cond.L.Unlock()
for _, h := range p.headers { for _, h := range p.headers {
if len(p.buf) < headerBufferSize { if len(p.buf) < headerBufferSize {
break break