Skip to content

Commit

Permalink
audio: add an additional error information when creating a context
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi authored and superloach committed Aug 30, 2022
1 parent fd8e6e5 commit 4db151f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions audio/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
53 changes: 53 additions & 0 deletions audio/error_ios.go
Original file line number Diff line number Diff line change
@@ -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 <UIKit/UIKit.h>
//
// 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)
}
22 changes: 22 additions & 0 deletions audio/error_notios.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4db151f

Please sign in to comment.