示例#1
0
文件: source.go 项目: tanema/amore
// NewSource creates a new Source from a file at the path provided. If you
// specify a static source it will all be buffered into a single buffer. If
// false then it will create many buffers a cycle through them with data chunks.
// This allows a smaller memory footprint while playing bigger music files. You
// may want a static file if the sound is less than 2 seconds. It allows for faster
// cleaning playing of shorter sounds like footsteps.
func NewSource(filepath string, static bool) (*Source, error) {
	if pool == nil {
		createPool()
	}

	decoder, err := decoding.Decode(filepath)
	if err != nil {
		return nil, err
	}

	new_source := &Source{
		decoder:           decoder,
		isStatic:          static,
		pitch:             1,
		volume:            1,
		maxVolume:         1,
		referenceDistance: 1,
		rolloffFactor:     1,
		maxDistance:       MAX_ATTENUATION_DISTANCE,
		cone:              al.Cone{0, 0, 0},
		position:          al.Vector{},
		velocity:          al.Vector{},
		direction:         al.Vector{},
	}

	if static {
		new_source.staticBuffer = al.GenBuffers(1)[0]
		new_source.staticBuffer.BufferData(decoder.GetFormat(), decoder.GetData(), decoder.GetSampleRate())
	} else {
		new_source.streamBuffers = []al.Buffer{} //al.GenBuffers(MAX_BUFFERS)
	}

	return new_source, nil
}
示例#2
0
文件: source.go 项目: tanema/amore
// Play starts playing the source.
func (s *Source) Play() bool {
	if s.IsPlaying() {
		return true
	}

	if s.IsPaused() {
		s.Resume()
		return true
	}

	//claim a source for ourselves and make sure it worked
	if !pool.claim(s) || !s.isValid() {
		return false
	}

	pool.mutex.Lock()
	defer pool.mutex.Unlock()

	if s.isStatic {
		s.source.SetBuffer(s.staticBuffer)
	} else {
		buffers := []al.Buffer{}
		for i := 0; i < MAX_BUFFERS; i++ {
			buffer := al.GenBuffers(1)[0]
			if s.stream(buffer) > 0 {
				buffers = append(buffers, buffer)
			}
			if s.decoder.IsFinished() {
				break
			}
		}
		if len(buffers) > 0 {
			s.source.QueueBuffers(buffers...)
		}
	}

	// This Source may now be associated with an OpenAL source that still has
	// the properties of another Source. Let's reset it to the settings
	// of the new one.
	s.reset()

	// Clear errors.
	al.Error()

	al.PlaySources(s.source)

	// alSourcePlay may fail if the system has reached its limit of simultaneous
	// playing sources.
	return al.Error() == al.NoError
}