diff --git a/audio/audio.go b/audio/audio.go index a6dac5e53..7d926005f 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -84,10 +84,8 @@ var ( // Other values might not work. // For example, 22050 causes error on Safari when decoding MP3. // -// Error returned by NewContext is always nil as of 1.5.0-alpha. -// // NewContext panics when an audio context is already created. -func NewContext(sampleRate int) (*Context, error) { +func NewContext(sampleRate int) *Context { theContextLock.Lock() defer theContextLock.Unlock() if theContext != nil { @@ -127,7 +125,7 @@ func NewContext(sampleRate int) (*Context, error) { return err }) - return c, nil + return c } // CurrentContext returns the current context or nil if there is no context. diff --git a/audio/audio_test.go b/audio/audio_test.go index 910a3d266..f368d17d9 100644 --- a/audio/audio_test.go +++ b/audio/audio_test.go @@ -25,11 +25,7 @@ import ( var context *Context func setup() { - var err error - context, err = NewContext(44100) - if err != nil { - panic(err) - } + context = NewContext(44100) } func teardown() { diff --git a/audio/vorbis/vorbis_test.go b/audio/vorbis/vorbis_test.go index fe143cbee..2f41a0260 100644 --- a/audio/vorbis/vorbis_test.go +++ b/audio/vorbis/vorbis_test.go @@ -23,15 +23,7 @@ import ( . "github.com/hajimehoshi/ebiten/v2/audio/vorbis" ) -var audioContext *audio.Context - -func init() { - var err error - audioContext, err = audio.NewContext(44100) - if err != nil { - panic(err) - } -} +var audioContext = audio.NewContext(44100) func TestMono(t *testing.T) { bs := test_mono_ogg diff --git a/examples/audio/main.go b/examples/audio/main.go index b2c96a7f1..3b59219da 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -267,10 +267,7 @@ type Game struct { } func NewGame() (*Game, error) { - audioContext, err := audio.NewContext(sampleRate) - if err != nil { - return nil, err - } + audioContext := audio.NewContext(sampleRate) m, err := NewPlayer(audioContext, typeOgg) if err != nil { diff --git a/examples/audioinfiniteloop/main.go b/examples/audioinfiniteloop/main.go index d688ff3ee..3ae332503 100644 --- a/examples/audioinfiniteloop/main.go +++ b/examples/audioinfiniteloop/main.go @@ -37,15 +37,7 @@ const ( loopLengthInSecond = 4 ) -var audioContext *audio.Context - -func init() { - var err error - audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } -} +var audioContext = audio.NewContext(sampleRate) type Game struct { player *audio.Player diff --git a/examples/audiopanning/main.go b/examples/audiopanning/main.go index f195ecd07..dc504f5f0 100644 --- a/examples/audiopanning/main.go +++ b/examples/audiopanning/main.go @@ -41,15 +41,7 @@ const ( var img *ebiten.Image -var audioContext *audio.Context - -func init() { - var err error - audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } -} +var audioContext = audio.NewContext(sampleRate) type Game struct { player *audio.Player diff --git a/examples/flappy/main.go b/examples/flappy/main.go index 1beeb548d..845ca3f34 100644 --- a/examples/flappy/main.go +++ b/examples/flappy/main.go @@ -116,14 +116,12 @@ func init() { } var ( - audioContext *audio.Context + audioContext = audio.NewContext(44100) jumpPlayer *audio.Player hitPlayer *audio.Player ) func init() { - audioContext, _ = audio.NewContext(44100) - jumpD, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Jump_ogg)) if err != nil { log.Fatal(err) diff --git a/examples/pcm/main.go b/examples/pcm/main.go index 8a9ff14a5..f163ba8f7 100644 --- a/examples/pcm/main.go +++ b/examples/pcm/main.go @@ -32,15 +32,7 @@ const ( sampleRate = 44100 ) -var audioContext *audio.Context - -func init() { - var err error - audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } -} +var audioContext = audio.NewContext(sampleRate) const ( freqA = 440.0 diff --git a/examples/piano/main.go b/examples/piano/main.go index 16dfc013f..b7c596c93 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -64,15 +64,7 @@ const ( baseFreq = 220 ) -var audioContext *audio.Context - -func init() { - var err error - audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } -} +var audioContext = audio.NewContext(sampleRate) // pianoAt returns an i-th sample of piano with the given frequency. func pianoAt(i int, freq float64) float64 { diff --git a/examples/sinewave/main.go b/examples/sinewave/main.go index 407907ee0..35580336d 100644 --- a/examples/sinewave/main.go +++ b/examples/sinewave/main.go @@ -33,15 +33,7 @@ const ( frequency = 440 ) -var audioContext *audio.Context - -func init() { - var err error - audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } -} +var audioContext = audio.NewContext(sampleRate) // stream is an infinite stream of 440 Hz sine wave. type stream struct { diff --git a/examples/wav/main.go b/examples/wav/main.go index 1f992df69..5829e943b 100644 --- a/examples/wav/main.go +++ b/examples/wav/main.go @@ -42,10 +42,7 @@ var g Game func init() { var err error // Initialize audio context. - g.audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } + g.audioContext = audio.NewContext(sampleRate) // In this example, embedded resource "Jab_wav" is used. //