Exemple #1
0
// Stop stops the player.
func (p *Player) Stop() error {
	if p == nil {
		return nil
	}
	al.StopSources(p.source)
	return lastErr()
}
Exemple #2
0
// StopSound has two modes of operating.
// First mode is where it detects that the coordinates are on that key but telling it to stop.
// This usually indicates a tap where the user has lifted up their finger.
// Second mode is where if the coordinates are not on the key and you want to force the key to stop.
// This usually is when the user is sliding from key to key.
func (k *PianoKey) StopSound(x float32, y float32, sliding bool, frameData util.FrameData) bool {
	if (k.DoesCoordsOverlapKey(x, y, frameData) && !sliding) || (sliding && k.pressed && !k.DoesCoordsOverlapKey(x, y, frameData)) {
		k.pressed = false
		al.StopSources(k.soundSources...)
		return true
	}
	return false
}
Exemple #3
0
func (c *Context) Close() {
	c.Lock()
	defer c.Unlock()
	al.StopSources(c.source)
	c.source.UnqueueBuffers(c.queue...)
	al.DeleteBuffers(c.queue...)
	c.queue = nil
	al.DeleteSources(c.source)
}
Exemple #4
0
func (as *AudioSystem) Update(entity *ecs.Entity, dt float32) {
	var ac *AudioComponent
	var ok bool
	if ac, ok = entity.ComponentFast(ac).(*AudioComponent); !ok {
		return
	}

	if ac.player == nil {
		f := Files.Sound(ac.File)
		if f == nil {
			return
		}

		var err error
		ac.player, err = NewPlayer(f, 0, 0)
		if err != nil {
			log.Println("Error initializing AudioSystem:", err)
			return
		}
	}

	if ac.player.State() != Playing {
		if ac.player.State() == Stopped {
			if !ac.Repeat {
				al.RewindSources(ac.player.source)
				al.StopSources(ac.player.source)
				entity.RemoveComponent(ac)
				return
			}
		}

		// Prepares if the track hasn't been buffered before.
		if err := ac.player.prepare(ac.Background, 0, false); err != nil {
			log.Println("Error initializing AudioSystem:", err)
			return
		}

		al.PlaySources(ac.player.source)

		if !ac.Background {
			var space *SpaceComponent
			var ok bool
			if space, ok = entity.ComponentFast(space).(*SpaceComponent); !ok {
				return
			}

			ac.player.source.SetPosition(al.Vector{
				(space.Position.X + space.Width/2) / Width(),
				(space.Position.Y + space.Height/2) / Height(),
				0})
		}
	}
}
Exemple #5
0
func (a *AudioSystem) Update(dt float32) {
	for _, e := range a.entities {
		if e.AudioComponent.player == nil {
			f := Files.Sound(e.AudioComponent.File)
			if f == nil {
				log.Println("Audio file not loaded:", e.AudioComponent.File)
				continue
			}

			var err error
			e.AudioComponent.player, err = NewPlayer(f, 0, 0)
			if err != nil {
				log.Println("Error initializing AudioComponent:", err)
				continue
			}
		}

		if MasterVolume != a.cachedVolume {
			e.AudioComponent.SetVolume(e.AudioComponent.RawVolume)
		}

		if e.AudioComponent.player.State() != Playing {
			if e.AudioComponent.player.State() == Stopped {
				if !e.AudioComponent.Repeat {
					al.RewindSources(e.AudioComponent.player.source)
					al.StopSources(e.AudioComponent.player.source)
					// Remove it from this system, defer because we want to be sure it doesn't interfere with
					// looping over a.entities
					defer a.Remove(*e.BasicEntity)
					continue
				}
			}

			// Prepares if the track hasn't been buffered before.
			if err := e.AudioComponent.player.prepare(e.AudioComponent.Background, 0, false); err != nil {
				log.Println("Error initializing AudioComponent:", err)
				continue
			}

			al.PlaySources(e.AudioComponent.player.source)

			if !e.AudioComponent.Background {
				e.AudioComponent.player.source.SetPosition(al.Vector{
					(e.SpaceComponent.Position.X + e.SpaceComponent.Width/2) / GameWidth(), // TODO: ensure we're using correct Width/Height()
					(e.SpaceComponent.Position.Y + e.SpaceComponent.Height/2) / GameHeight(),
					0,
				})
			}
		}
	}
}
Exemple #6
0
func (p *Player) Close() error {
	if err := al.Error(); err != 0 {
		return fmt.Errorf("driver: error before closing: %d", err)
	}
	if p.isClosed {
		return nil
	}
	var bs []al.Buffer
	al.RewindSources(p.alSource)
	al.StopSources(p.alSource)
	if n := p.alSource.BuffersQueued(); 0 < n {
		bs = make([]al.Buffer, n)
		p.alSource.UnqueueBuffers(bs...)
		p.alBuffers = append(p.alBuffers, bs...)
	}
	p.isClosed = true
	if err := al.Error(); err != 0 {
		return fmt.Errorf("driver: error after closing: %d", err)
	}
	runtime.SetFinalizer(p, nil)
	return nil
}
Exemple #7
0
// StopSound stops the sound playing through the source i
func StopSound(i int) {
	al.StopSources(sources[i])
}