audio: Remove the error returning value from NewContext

Updates #1380
This commit is contained in:
Hajime Hoshi 2020-10-05 02:25:11 +09:00
parent 29b4087ebf
commit 944a19c6f7
11 changed files with 12 additions and 74 deletions

View File

@ -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.

View File

@ -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() {

View File

@ -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

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -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.
//