audio: audio.NewContext now returns error

This commit is contained in:
Hajime Hoshi 2016-04-03 02:59:44 +09:00
parent e631714a43
commit 1ec786f83a
5 changed files with 22 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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

View File

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