From 48110eb5186bd84db6709546a2035f6a1aa04064 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 31 Aug 2022 00:58:18 +0900 Subject: [PATCH] audio: add an additional error information when creating a context Updates hajimehoshi/oto#93 --- audio/context.go | 1 + audio/error_ios.go | 53 +++++++++++++++++++++++++++++++++++++++++++ audio/error_notios.go | 22 ++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 audio/error_ios.go create mode 100644 audio/error_notios.go diff --git a/audio/context.go b/audio/context.go index e25e75e118de..fbb8a9679bd0 100644 --- a/audio/context.go +++ b/audio/context.go @@ -22,6 +22,7 @@ import ( func newContext(sampleRate, channelCount, bitDepthInBytes int) (context, chan struct{}, error) { ctx, ready, err := oto.NewContext(sampleRate, channelCount, bitDepthInBytes) + err = addErrorInfoForContextCreation(err) return &contextProxy{ctx}, ready, err } diff --git a/audio/error_ios.go b/audio/error_ios.go new file mode 100644 index 000000000000..da8e0a21a2d4 --- /dev/null +++ b/audio/error_ios.go @@ -0,0 +1,53 @@ +// Copyright 2022 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build ios +// +build ios + +package audio + +// #cgo CFLAGS: -x objective-c +// #cgo LDFLAGS: -framework UIKit +// +// #import +// +// static UIApplicationState applicationState() { +// return [[UIApplication sharedApplication] applicationState]; +// } +import "C" + +import ( + "fmt" +) + +// addErrorInfoForContextCreation adds an additional infomation to the error when creating an audio context. +// See also hajimehoshi/oto#93. +func addErrorInfoForContextCreation(err error) error { + if err == nil { + return nil + } + + var state string + switch s := C.applicationState(); s { + case C.UIApplicationStateActive: + state = "active" + case C.UIApplicationStateInactive: + state = "inactive" + case C.UIApplicationStateBackground: + state = "background" + default: + state = fmt.Sprintf("UIApplicationState(%d)", s) + } + return fmt.Errorf("%w, application state: %s", err, state) +} diff --git a/audio/error_notios.go b/audio/error_notios.go new file mode 100644 index 000000000000..da4da0b57e32 --- /dev/null +++ b/audio/error_notios.go @@ -0,0 +1,22 @@ +// Copyright 2022 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !ios +// +build !ios + +package audio + +func addErrorInfoForContextCreation(err error) error { + return err +}