Skip to content

Commit

Permalink
Merge pull request faiface#134 from gopxl/fix-speaker-buffer-size
Browse files Browse the repository at this point in the history
Fix speaker buffer size
  • Loading branch information
MarkKremer authored Nov 15, 2023
2 parents 12f8f93 + 3de5607 commit 817ca13
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion speaker/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,29 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error {

mixer = beep.Mixer{}

// We split the total amount of buffer size between the driver and the player.
// This seems to be a decent ratio on my machine, but it may have different
// results on other OS's because of different underlying implementations.
// Both buffers try to keep themselves filled, so the total buffered
// number of samples should be some number less than bufferSize.
driverBufferSize := bufferSize / 2
playerBufferSize := bufferSize / 2

var err error
var readyChan chan struct{}
context, readyChan, err := oto.NewContext(&oto.NewContextOptions{
SampleRate: int(sampleRate),
ChannelCount: channelCount,
Format: otoFormat,
BufferSize: sampleRate.D(bufferSize),
BufferSize: sampleRate.D(driverBufferSize),
})
if err != nil {
return errors.Wrap(err, "failed to initialize speaker")
}
<-readyChan

player = context.NewPlayer(newReaderFromStreamer(&mixer))
player.SetBufferSize(playerBufferSize * bytesPerSample)
player.Play()

return nil
Expand Down

0 comments on commit 817ca13

Please sign in to comment.