From 54f0e30fe661a2c727a7a46668729c826984f922 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 3 Apr 2016 02:43:39 +0900 Subject: [PATCH] audio: Remove returning value from startPlaying --- exp/audio/audio.go | 13 +++++-------- exp/audio/audio_openal.go | 11 ++++------- exp/audio/audio_windows.go | 11 +++++------ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/exp/audio/audio.go b/exp/audio/audio.go index db6270671..717052611 100644 --- a/exp/audio/audio.go +++ b/exp/audio/audio.go @@ -105,11 +105,10 @@ func (s *mixedPlayersStream) Read(b []byte) (int, error) { // TODO: Enable to specify the format like Mono8? type Context struct { - sampleRate int - stream *mixedPlayersStream - players map[*Player]struct{} - innerPlayer *player - frames int + sampleRate int + stream *mixedPlayersStream + players map[*Player]struct{} + frames int sync.Mutex } @@ -122,11 +121,9 @@ func NewContext(sampleRate int) *Context { c.stream = &mixedPlayersStream{ context: c, } - p, err := startPlaying(c.stream, c.sampleRate) - if err != nil { + if err := startPlaying(c.stream, c.sampleRate); err != nil { panic(fmt.Sprintf("audio: NewContext error: %v", err)) } - c.innerPlayer = p return c } diff --git a/exp/audio/audio_openal.go b/exp/audio/audio_openal.go index e03139c91..3ba249802 100644 --- a/exp/audio/audio_openal.go +++ b/exp/audio/audio_openal.go @@ -40,13 +40,13 @@ type player struct { isClosed bool } -func startPlaying(src io.Reader, sampleRate int) (*player, error) { +func startPlaying(src io.Reader, sampleRate int) error { if e := al.OpenDevice(); e != nil { - return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e) + return fmt.Errorf("audio: OpenAL initialization failed: %v", e) } s := al.GenSources(1) if err := al.Error(); err != 0 { - panic(fmt.Sprintf("audio: al.GenSources error: %d", err)) + return fmt.Errorf("audio: al.GenSources error: %d", err) } p := &player{ alSource: s[0], @@ -55,10 +55,7 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) { sampleRate: sampleRate, } runtime.SetFinalizer(p, (*player).close) - if err := p.start(); err != nil { - return nil, err - } - return p, nil + return p.start() } const ( diff --git a/exp/audio/audio_windows.go b/exp/audio/audio_windows.go index 1b48b20c1..8d1b55de3 100644 --- a/exp/audio/audio_windows.go +++ b/exp/audio/audio_windows.go @@ -37,6 +37,8 @@ type header struct { waveHdr C.WAVEHDR } +// TODO: Reduce panics and use errors instead + func newHeader(waveOut C.HWAVEOUT, bufferSize int) header { // NOTE: This is never freed so far. buf := C.malloc(C.size_t(bufferSize)) @@ -74,10 +76,7 @@ func releaseSemaphore() { <-sem } -type player struct { -} - -func startPlaying(src io.Reader, sampleRate int) (*player, error) { +func startPlaying(src io.Reader, sampleRate int) error { const numBlockAlign = channelNum * bitsPerSample / 8 f := C.WAVEFORMATEX{ wFormatTag: C.WAVE_FORMAT_PCM, @@ -89,7 +88,7 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) { } var w C.HWAVEOUT if err := C.waveOutOpen2(&w, &f); err != C.MMSYSERR_NOERROR { - panic(fmt.Sprintf("audio: waveOutOpen error: %d", err)) + return fmt.Errorf("audio: waveOutOpen error: %d", err) } go func() { const bufferSize = 1024 @@ -123,5 +122,5 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) { } // TODO: Finalize the wave handler }() - return &player{}, nil + return nil }