diff --git a/examples/audio/main.go b/examples/audio/main.go index a9c3a11cc..d6cd4129d 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -253,7 +253,10 @@ func main() { } const sampleRate = 22050 const bytesPerSample = 4 // TODO: This should be defined in audio package - audioContext = audio.NewContext(sampleRate) + audioContext, err = audio.NewContext(sampleRate) + if err != nil { + log.Fatal(err) + } go func() { s, err := wav.Decode(audioContext, wavF) if err != nil { diff --git a/examples/pcm/main.go b/examples/pcm/main.go index 59d3713b9..044beef0e 100644 --- a/examples/pcm/main.go +++ b/examples/pcm/main.go @@ -34,7 +34,11 @@ const ( var audioContext *audio.Context func init() { - audioContext = audio.NewContext(sampleRate) + var err error + audioContext, err = audio.NewContext(sampleRate) + if err != nil { + log.Fatal(err) + } } var frames = 0 diff --git a/examples/piano/main.go b/examples/piano/main.go index a50660159..7b3664622 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -36,7 +36,11 @@ const ( var audioContext *audio.Context func init() { - audioContext = audio.NewContext(sampleRate) + var err error + audioContext, err = audio.NewContext(sampleRate) + if err != nil { + log.Fatal(err) + } } var pcm = make([]float64, 4*sampleRate) diff --git a/examples/sinewave/main.go b/examples/sinewave/main.go index d7d6217b0..53766beb5 100644 --- a/examples/sinewave/main.go +++ b/examples/sinewave/main.go @@ -35,7 +35,11 @@ const ( var audioContext *audio.Context func init() { - audioContext = audio.NewContext(sampleRate) + var err error + audioContext, err = audio.NewContext(sampleRate) + if err != nil { + log.Fatal(err) + } } type stream struct { diff --git a/exp/audio/audio.go b/exp/audio/audio.go index 7b95d5afc..2800cccd8 100644 --- a/exp/audio/audio.go +++ b/exp/audio/audio.go @@ -112,7 +112,7 @@ type Context struct { sync.Mutex } -func NewContext(sampleRate int) *Context { +func NewContext(sampleRate int) (*Context, error) { // TODO: Panic if one context exists. c := &Context{ sampleRate: sampleRate, @@ -122,9 +122,9 @@ func NewContext(sampleRate int) *Context { context: c, } if err := startPlaying(c.stream, c.sampleRate); err != nil { - panic(fmt.Sprintf("audio: NewContext error: %v", err)) + return nil, fmt.Errorf("audio: NewContext error: %v", err) } - return c + return c, nil } // Update proceeds the inner (logical) time of the context by 1/60 second.