audio: Remove returning value from startPlaying

This commit is contained in:
Hajime Hoshi 2016-04-03 02:43:39 +09:00
parent 4492c0dad0
commit 54f0e30fe6
3 changed files with 14 additions and 21 deletions

View File

@ -105,11 +105,10 @@ func (s *mixedPlayersStream) Read(b []byte) (int, error) {
// TODO: Enable to specify the format like Mono8? // TODO: Enable to specify the format like Mono8?
type Context struct { type Context struct {
sampleRate int sampleRate int
stream *mixedPlayersStream stream *mixedPlayersStream
players map[*Player]struct{} players map[*Player]struct{}
innerPlayer *player frames int
frames int
sync.Mutex sync.Mutex
} }
@ -122,11 +121,9 @@ func NewContext(sampleRate int) *Context {
c.stream = &mixedPlayersStream{ c.stream = &mixedPlayersStream{
context: c, context: c,
} }
p, err := startPlaying(c.stream, c.sampleRate) if err := startPlaying(c.stream, c.sampleRate); err != nil {
if err != nil {
panic(fmt.Sprintf("audio: NewContext error: %v", err)) panic(fmt.Sprintf("audio: NewContext error: %v", err))
} }
c.innerPlayer = p
return c return c
} }

View File

@ -40,13 +40,13 @@ type player struct {
isClosed bool isClosed bool
} }
func startPlaying(src io.Reader, sampleRate int) (*player, error) { func startPlaying(src io.Reader, sampleRate int) error {
if e := al.OpenDevice(); e != nil { if e := al.OpenDevice(); e != nil {
return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e) return fmt.Errorf("audio: OpenAL initialization failed: %v", e)
} }
s := al.GenSources(1) s := al.GenSources(1)
if err := al.Error(); err != 0 { if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: al.GenSources error: %d", err)) return fmt.Errorf("audio: al.GenSources error: %d", err)
} }
p := &player{ p := &player{
alSource: s[0], alSource: s[0],
@ -55,10 +55,7 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) {
sampleRate: sampleRate, sampleRate: sampleRate,
} }
runtime.SetFinalizer(p, (*player).close) runtime.SetFinalizer(p, (*player).close)
if err := p.start(); err != nil { return p.start()
return nil, err
}
return p, nil
} }
const ( const (

View File

@ -37,6 +37,8 @@ type header struct {
waveHdr C.WAVEHDR waveHdr C.WAVEHDR
} }
// TODO: Reduce panics and use errors instead
func newHeader(waveOut C.HWAVEOUT, bufferSize int) header { func newHeader(waveOut C.HWAVEOUT, bufferSize int) header {
// NOTE: This is never freed so far. // NOTE: This is never freed so far.
buf := C.malloc(C.size_t(bufferSize)) buf := C.malloc(C.size_t(bufferSize))
@ -74,10 +76,7 @@ func releaseSemaphore() {
<-sem <-sem
} }
type player struct { func startPlaying(src io.Reader, sampleRate int) error {
}
func startPlaying(src io.Reader, sampleRate int) (*player, error) {
const numBlockAlign = channelNum * bitsPerSample / 8 const numBlockAlign = channelNum * bitsPerSample / 8
f := C.WAVEFORMATEX{ f := C.WAVEFORMATEX{
wFormatTag: C.WAVE_FORMAT_PCM, wFormatTag: C.WAVE_FORMAT_PCM,
@ -89,7 +88,7 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) {
} }
var w C.HWAVEOUT var w C.HWAVEOUT
if err := C.waveOutOpen2(&w, &f); err != C.MMSYSERR_NOERROR { if err := C.waveOutOpen2(&w, &f); err != C.MMSYSERR_NOERROR {
panic(fmt.Sprintf("audio: waveOutOpen error: %d", err)) return fmt.Errorf("audio: waveOutOpen error: %d", err)
} }
go func() { go func() {
const bufferSize = 1024 const bufferSize = 1024
@ -123,5 +122,5 @@ func startPlaying(src io.Reader, sampleRate int) (*player, error) {
} }
// TODO: Finalize the wave handler // TODO: Finalize the wave handler
}() }()
return &player{}, nil return nil
} }