From d472430b21459d4403fc4bc9c49a80844013d725 Mon Sep 17 00:00:00 2001 From: Mark Kremer Date: Thu, 13 Oct 2022 17:47:09 +0200 Subject: [PATCH 1/4] Upgrade to Oto v2 & let Oto pull samples instead of pushing them to Oto --- go.mod | 2 +- go.sum | 13 +++--- speaker/speaker.go | 103 ++++++++++++++++++++++++++------------------- 3 files changed, 67 insertions(+), 51 deletions(-) diff --git a/go.mod b/go.mod index 44b4e87..69051c9 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.14 require ( github.com/gdamore/tcell v1.3.0 github.com/hajimehoshi/go-mp3 v0.3.0 - github.com/hajimehoshi/oto v0.7.1 + github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4 github.com/jfreymuth/oggvorbis v1.0.1 github.com/mewkiz/flac v1.0.7 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index fcc03dc..c6176d4 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFDnH08= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo= +github.com/ebitengine/purego v0.0.0-20220907032450-cf3e27c364c7 h1:tmSauY5l3s/Cp5n+cEiG1epUR2AejmdHeMJMycMFxb0= +github.com/ebitengine/purego v0.0.0-20220907032450-cf3e27c364c7/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg= github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM= @@ -10,9 +12,10 @@ github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38r github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE= github.com/hajimehoshi/go-mp3 v0.3.0 h1:fTM5DXjp/DL2G74HHAs/aBGiS9Tg7wnp+jkU38bHy4g= github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM= +github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM= github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI= -github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk= -github.com/hajimehoshi/oto v0.7.1/go.mod h1:wovJ8WWMfFKvP587mhHgot/MBr4DnNy9m6EepeVGnos= +github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4 h1:m29xzbn3Pv5MgvgjMPs7m28uhUgVt3B3AIGjQLgkqUI= +github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4/go.mod h1:OdGUICBjy7upAjvqqacbB63XIuYR3fqXZ7kYtlVYJgQ= github.com/icza/bitio v1.0.0 h1:squ/m1SHyFeCA6+6Gyol1AxV9nmPPlJFT8c2vKdj3U8= github.com/icza/bitio v1.0.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A= github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k= @@ -32,17 +35,15 @@ github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2/go.mod h1:3E2FUC/qYUfM8 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 h1:idBdZTd9UioThJp8KpM/rTSinK/ChZFBE43/WtIy8zg= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/image v0.0.0-20190220214146-31aff87c08e9/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/image v0.0.0-20190227222117-0694c2d4d067 h1:KYGJGHOQy8oSi1fDlSpcZF0+juKwk/hEMv5SiwHogR0= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6 h1:vyLBGJPIl9ZYbcQFM2USFmJBK6KI+t+z6jL0lbwjrnc= golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10= golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/speaker/speaker.go b/speaker/speaker.go index a982d08..a574821 100644 --- a/speaker/speaker.go +++ b/speaker/speaker.go @@ -2,21 +2,22 @@ package speaker import ( - "sync" - "github.com/gopxl/beep" - "github.com/hajimehoshi/oto" + "github.com/hajimehoshi/oto/v2" "github.com/pkg/errors" + "io" + "sync" ) +const channelCount = 2 +const bitDepthInBytes = 2 +const bytesPerSample = bitDepthInBytes * channelCount + var ( mu sync.Mutex mixer beep.Mixer - samples [][2]float64 - buf []byte context *oto.Context - player *oto.Player - done chan struct{} + player oto.Player ) // Init initializes audio playback through speaker. Must be called before using this package. @@ -25,36 +26,23 @@ var ( // bufferSize means lower CPU usage and more reliable playback. Lower bufferSize means better // responsiveness and less delay. func Init(sampleRate beep.SampleRate, bufferSize int) error { - mu.Lock() - defer mu.Unlock() - - Close() + if context != nil { + return errors.New("speaker cannot be initialized more than once") + } mixer = beep.Mixer{} - numBytes := bufferSize * 4 - samples = make([][2]float64, bufferSize) - buf = make([]byte, numBytes) - var err error - context, err = oto.NewContext(int(sampleRate), 2, 2, numBytes) + var readyChan chan struct{} + context, readyChan, err = oto.NewContext(int(sampleRate), channelCount, bitDepthInBytes) if err != nil { return errors.Wrap(err, "failed to initialize speaker") } - player = context.NewPlayer() - - done = make(chan struct{}) + <-readyChan - go func() { - for { - select { - default: - update() - case <-done: - return - } - } - }() + player = context.NewPlayer(newReaderFromStreamer(&mixer)) + player.(oto.BufferSizeSetter).SetBufferSize(bufferSize * bytesPerSample) + player.Play() return nil } @@ -63,14 +51,11 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error { // even when the program doesn't play anymore, because in properly set systems, the default mixer // handles multiple concurrent processes. It's only when the default device is not a virtual but hardware // device, that you'll probably want to manually manage the device from your application. +// +// TODO: investigate what happens now that oto.Context doesn't have a Close method. func Close() { if player != nil { - if done != nil { - done <- struct{}{} - done = nil - } player.Close() - context.Close() player = nil } } @@ -102,16 +87,46 @@ func Clear() { mu.Unlock() } -// update pulls new data from the playing Streamers and sends it to the speaker. Blocks until the -// data is sent and started playing. -func update() { +// sampleReader is a wrapper for beep.Streamer to implement io.Reader. +type sampleReader struct { + s beep.Streamer + buf [][2]float64 +} + +func newReaderFromStreamer(s beep.Streamer) *sampleReader { + return &sampleReader{ + s: s, + } +} + +// Read pulls samples from the reader and fills buf with the encoded +// samples. Read expects the size of buf be divisible by the length +// of a sample (= channel count * bit depth in bytes). +func (s *sampleReader) Read(buf []byte) (n int, err error) { + // Read samples from streamer + if len(buf)%bytesPerSample != 0 { + return 0, errors.New("requested number of bytes do not align with the samples") + } + ns := len(buf) / bytesPerSample + if len(s.buf) < ns { + s.buf = make([][2]float64, ns) + } mu.Lock() - mixer.Stream(samples) + ns, ok := s.s.Stream(s.buf[:ns]) mu.Unlock() + if !ok { + if s.s.Err() != nil { + return 0, errors.Wrap(s.s.Err(), "streamer returned error when requesting samples") + } + if ns == 0 { + return 0, io.EOF + } + } - for i := range samples { - for c := range samples[i] { - val := samples[i][c] + // Convert samples to bytes + for i := range s.buf[:ns] { + for c := range s.buf[i] { + val := s.buf[i][c] if val < -1 { val = -1 } @@ -121,10 +136,10 @@ func update() { valInt16 := int16(val * (1<<15 - 1)) low := byte(valInt16) high := byte(valInt16 >> 8) - buf[i*4+c*2+0] = low - buf[i*4+c*2+1] = high + buf[i*bytesPerSample+c*bitDepthInBytes+0] = low + buf[i*bytesPerSample+c*bitDepthInBytes+1] = high } } - player.Write(buf) + return ns * bytesPerSample, nil } From 7f1d962d06a5f6f70200e7b29f7deeec4c43cb2a Mon Sep 17 00:00:00 2001 From: Mark Kremer Date: Thu, 13 Oct 2022 20:03:03 +0200 Subject: [PATCH 2/4] Clear the internal mixer when closing the speaker --- speaker/speaker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/speaker/speaker.go b/speaker/speaker.go index a574821..16692bc 100644 --- a/speaker/speaker.go +++ b/speaker/speaker.go @@ -57,6 +57,7 @@ func Close() { if player != nil { player.Close() player = nil + mixer.Clear() } } From 4a36a6400db86287704f92f99508337af1551dc0 Mon Sep 17 00:00:00 2001 From: Mark Kremer Date: Thu, 20 Oct 2022 14:24:47 +0200 Subject: [PATCH 3/4] Improve locking/panicking behaviour a bit & update docs --- speaker/speaker.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/speaker/speaker.go b/speaker/speaker.go index 16692bc..a6a48d0 100644 --- a/speaker/speaker.go +++ b/speaker/speaker.go @@ -47,17 +47,15 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error { return nil } -// Close closes the playback and the driver. In most cases, there is certainly no need to call Close -// even when the program doesn't play anymore, because in properly set systems, the default mixer -// handles multiple concurrent processes. It's only when the default device is not a virtual but hardware -// device, that you'll probably want to manually manage the device from your application. -// -// TODO: investigate what happens now that oto.Context doesn't have a Close method. +// Close closes audio playback. However, the underlying driver context keeps existing, because +// closing it isn't supported (https://github.com/hajimehoshi/oto/issues/149). In most cases, +// there is certainly no need to call Close even when the program doesn't play anymore, because +// in properly set systems, the default mixer handles multiple concurrent processes. func Close() { if player != nil { player.Close() player = nil - mixer.Clear() + Clear() } } @@ -82,6 +80,7 @@ func Play(s ...beep.Streamer) { } // Clear removes all currently playing Streamers from the speaker. +// Previously buffered samples may still be played. func Clear() { mu.Lock() mixer.Clear() @@ -100,7 +99,7 @@ func newReaderFromStreamer(s beep.Streamer) *sampleReader { } } -// Read pulls samples from the reader and fills buf with the encoded +// Read pulls samples from the streamer and fills buf with the encoded // samples. Read expects the size of buf be divisible by the length // of a sample (= channel count * bit depth in bytes). func (s *sampleReader) Read(buf []byte) (n int, err error) { @@ -112,9 +111,7 @@ func (s *sampleReader) Read(buf []byte) (n int, err error) { if len(s.buf) < ns { s.buf = make([][2]float64, ns) } - mu.Lock() - ns, ok := s.s.Stream(s.buf[:ns]) - mu.Unlock() + ns, ok := s.stream(s.buf[:ns]) if !ok { if s.s.Err() != nil { return 0, errors.Wrap(s.s.Err(), "streamer returned error when requesting samples") @@ -144,3 +141,11 @@ func (s *sampleReader) Read(buf []byte) (n int, err error) { return ns * bytesPerSample, nil } + +// stream pull samples from the streamer while preventing concurrency +// problems by locking the global mixer. +func (s *sampleReader) stream(samples [][2]float64) (n int, ok bool) { + mu.Lock() + defer mu.Unlock() + return s.s.Stream(samples) +} From 5fefa491fdb0ba5eba5854f06fbd3cce91f4f9f2 Mon Sep 17 00:00:00 2001 From: Mark Kremer Date: Sat, 7 Oct 2023 19:16:28 +0200 Subject: [PATCH 4/4] Upgrade to Oto v3 --- go.mod | 2 +- go.sum | 14 +++++++------- speaker/speaker.go | 13 +++++++++---- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 69051c9..26eeabc 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/gopxl/beep go 1.14 require ( + github.com/ebitengine/oto/v3 v3.1.0 github.com/gdamore/tcell v1.3.0 github.com/hajimehoshi/go-mp3 v0.3.0 - github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4 github.com/jfreymuth/oggvorbis v1.0.1 github.com/mewkiz/flac v1.0.7 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index c6176d4..b323c6f 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,10 @@ github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFDnH08= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo= -github.com/ebitengine/purego v0.0.0-20220907032450-cf3e27c364c7 h1:tmSauY5l3s/Cp5n+cEiG1epUR2AejmdHeMJMycMFxb0= -github.com/ebitengine/purego v0.0.0-20220907032450-cf3e27c364c7/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg= +github.com/ebitengine/oto/v3 v3.1.0 h1:9tChG6rizyeR2w3vsygTTTVVJ9QMMyu00m2yBOCch6U= +github.com/ebitengine/oto/v3 v3.1.0/go.mod h1:IK1QTnlfZK2GIB6ziyECm433hAdTaPpOsGMLhEyEGTg= +github.com/ebitengine/purego v0.5.0 h1:JrMGKfRIAM4/QVKaesIIT7m/UVjTj5GYhRSQYwfVdpo= +github.com/ebitengine/purego v0.5.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM= @@ -12,10 +14,7 @@ github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38r github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE= github.com/hajimehoshi/go-mp3 v0.3.0 h1:fTM5DXjp/DL2G74HHAs/aBGiS9Tg7wnp+jkU38bHy4g= github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM= -github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM= github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI= -github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4 h1:m29xzbn3Pv5MgvgjMPs7m28uhUgVt3B3AIGjQLgkqUI= -github.com/hajimehoshi/oto/v2 v2.4.0-alpha.4/go.mod h1:OdGUICBjy7upAjvqqacbB63XIuYR3fqXZ7kYtlVYJgQ= github.com/icza/bitio v1.0.0 h1:squ/m1SHyFeCA6+6Gyol1AxV9nmPPlJFT8c2vKdj3U8= github.com/icza/bitio v1.0.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A= github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k= @@ -43,7 +42,8 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew= -golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/speaker/speaker.go b/speaker/speaker.go index a6a48d0..fc83208 100644 --- a/speaker/speaker.go +++ b/speaker/speaker.go @@ -2,8 +2,8 @@ package speaker import ( + "github.com/ebitengine/oto/v3" "github.com/gopxl/beep" - "github.com/hajimehoshi/oto/v2" "github.com/pkg/errors" "io" "sync" @@ -12,12 +12,13 @@ import ( const channelCount = 2 const bitDepthInBytes = 2 const bytesPerSample = bitDepthInBytes * channelCount +const otoFormat = oto.FormatSignedInt16LE var ( mu sync.Mutex mixer beep.Mixer context *oto.Context - player oto.Player + player *oto.Player ) // Init initializes audio playback through speaker. Must be called before using this package. @@ -34,14 +35,18 @@ func Init(sampleRate beep.SampleRate, bufferSize int) error { var err error var readyChan chan struct{} - context, readyChan, err = oto.NewContext(int(sampleRate), channelCount, bitDepthInBytes) + context, readyChan, err := oto.NewContext(&oto.NewContextOptions{ + SampleRate: int(sampleRate), + ChannelCount: channelCount, + Format: otoFormat, + BufferSize: sampleRate.D(bufferSize), + }) if err != nil { return errors.Wrap(err, "failed to initialize speaker") } <-readyChan player = context.NewPlayer(newReaderFromStreamer(&mixer)) - player.(oto.BufferSizeSetter).SetBufferSize(bufferSize * bytesPerSample) player.Play() return nil