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 sampleRate = 22050
const bytesPerSample = 4 // TODO: This should be defined in audio package 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() { go func() {
s, err := wav.Decode(audioContext, wavF) s, err := wav.Decode(audioContext, wavF)
if err != nil { if err != nil {

View File

@ -34,7 +34,11 @@ const (
var audioContext *audio.Context var audioContext *audio.Context
func init() { func init() {
audioContext = audio.NewContext(sampleRate) var err error
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
} }
var frames = 0 var frames = 0

View File

@ -36,7 +36,11 @@ const (
var audioContext *audio.Context var audioContext *audio.Context
func init() { 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) var pcm = make([]float64, 4*sampleRate)

View File

@ -35,7 +35,11 @@ const (
var audioContext *audio.Context var audioContext *audio.Context
func init() { func init() {
audioContext = audio.NewContext(sampleRate) var err error
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
} }
type stream struct { type stream struct {

View File

@ -112,7 +112,7 @@ type Context struct {
sync.Mutex sync.Mutex
} }
func NewContext(sampleRate int) *Context { func NewContext(sampleRate int) (*Context, error) {
// TODO: Panic if one context exists. // TODO: Panic if one context exists.
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
@ -122,9 +122,9 @@ func NewContext(sampleRate int) *Context {
context: c, context: c,
} }
if err := startPlaying(c.stream, c.sampleRate); err != nil { 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. // Update proceeds the inner (logical) time of the context by 1/60 second.