示例#1
0
文件: audio.go 项目: Miaque/mojo
// NewPlayer returns a new Player.
// It initializes the underlying audio devices and the related resources.
func NewPlayer(src io.ReadSeeker, format Format, sampleRate int64) (*Player, error) {
	device.Lock()
	defer device.Unlock()

	if device.d == nil {
		device.d = alc.Open("")
		c := device.d.CreateContext(nil)
		if !alc.MakeContextCurrent(c) {
			return nil, fmt.Errorf("player: cannot initiate a new player")
		}
	}
	s := al.GenSources(1)
	if code := al.Error(); code != 0 {
		return nil, fmt.Errorf("player: cannot generate an audio source [err=%x]", code)
	}
	bufs := al.GenBuffers(2)
	if err := lastErr(); err != nil {
		return nil, err
	}
	return &Player{
		t:      &track{format: format, src: src, rate: sampleRate},
		source: s[0],
		bufs:   bufs,
	}, nil
}
示例#2
0
文件: audio.go 项目: Miaque/mojo
// lastErr returns the last error or nil if the last operation
// has been succesful.
func lastErr() error {
	if code := al.Error(); code != 0 {
		return fmt.Errorf("audio: openal failed with %x", code)
	}
	return nil
}