From 54b968a831d84ab4c049493b680d719396d6921a Mon Sep 17 00:00:00 2001 From: Dmitry Frank Date: Sat, 16 Feb 2019 11:48:35 +0200 Subject: [PATCH] Allow creation of multiple contexts This is a dirty workaround for an issue of consuming CPU when Context is idle, see: https://github.com/hajimehoshi/go-mp3/issues/28 --- context.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/context.go b/context.go index a8e227c..d33e0fd 100644 --- a/context.go +++ b/context.go @@ -30,8 +30,7 @@ type Context struct { } var ( - theContext *Context - contextM sync.Mutex + contextM sync.Mutex ) var errClosed = errors.New("closed") @@ -56,10 +55,6 @@ func NewContext(sampleRate, channelNum, bitDepthInBytes, bufferSizeInBytes int) contextM.Lock() defer contextM.Unlock() - if theContext != nil { - panic("oto: NewContext can be called only once") - } - d, err := newDriver(sampleRate, channelNum, bitDepthInBytes, bufferSizeInBytes) if err != nil { return nil, err @@ -74,7 +69,6 @@ func NewContext(sampleRate, channelNum, bitDepthInBytes, bufferSizeInBytes int) mux: mux.New(channelNum, bitDepthInBytes), errCh: make(chan error), } - theContext = c go func() { if _, err := io.Copy(c.driverWriter, c.mux); err != nil { c.errCh <- err