mixer

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2021 License: MIT Imports: 5 Imported by: 1

README

mixer

See the documentation for an API overview.

Your main interface to use is the SoundSource:

type SoundSource interface {
    // PlayPaused adds a new one-time sound to the mixer. It is in paused state.
    PlayPaused() Sound
    // PlayOnce adds a new one-time sound to the mixer. It is started right away
    // and stopped when it finishes.
    PlayOnce() Sound

    // SetVolume sets the default volume for all sounds played in the future.
    // Changing the Sound's volume will simply overwrite this setting (instead
    // of combining the factors).
    // The range is [0..1] and it is clamped to that.
    SetVolume(float32)
    Volume() float32

    // SetPan sets the default pan for all sounds played in the future.
    // Changing the Sound's pan will simply overwrite this setting (instead
    // of combining the factors).
    SetPan(float32)
    Pan() float32

    // Length returns the duration of the sound data. Note that a played Sound
    // may have a different value for its Length function as it considers
    // looping.
    Length() time.Duration
}

Documentation

Overview

Package mixer provides an abstraction over the sound card to be able to play multiple sounds simultaneously, combining different effects.

Call Init to start the mixer and Close when you are done with it. Call NewSoundSource to create a sound source from PCM data. You can use this source to play the sound using the Play... functions. Each call will give you a Sound which represents that particular instance of the sound source that is then played. You can change its parameters, pause and resume it and change its position. Once the Sound is done playing, its Stopped function will return true. In this case you cannot use the Sound anymore and should set its pointer to nil so the Go garbage collector can remove it. Calling any function on a Stopped Sound has no effect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close blocks until playing sound is stopped. It shuts down the DirectSound system.

func Error

func Error() error

Error returns the last error that occurred. If a fatal error occurs, the Go routine for mixing and playing sounds might stop before you call Close. In this case, call Error to retrieve the cause of the failure.

func Init

func Init() error

Init sets up DirectSound and prepares for mixing and playing sounds. It starts a Go routine that periodically writes to the sound buffer to output to the sound card. Call Close when you are done with the mixer.

func SetVolume

func SetVolume(v float32)

SetVolume sets the master volume. All sounds will be scaled by this factor. It is in the range [0..1] and will be clamped to it.

Types

type Sound

type Sound interface {
	// SetPaused starts or stops the sound. Note that the sound position is not
	// changed with this function, meaning that if the sound is not playing
	// because it was played all the way to the end, calling SetPaused(false)
	// will not restart it from the beginning. You have to call SetPosition(0)
	// to reset the sound to the start. If the sound is not paused, it will then
	// play right away.
	SetPaused(bool)

	// Paused returns the last value set in SetPaused. It does not consider
	// whether the sound is being played right now. It may not be paused but
	// could have reached the end and thus is not audible although not paused.
	Paused() bool

	// Playing returns true if the sound is not paused and has not reached the
	// end.
	Playing() bool

	// Stopped returns true if the sound has been fully played. This means that
	// the user cannot use the sound anymore. Set the pointer to nil in this
	// case so that the Go runtime can free its memory on the next GC.
	Stopped() bool

	// SetVolume sets the volume factor for all channels. Its range is [0..1]
	// and it will be clamped to that range.
	// Note that the audible difference in loudness between 100% and 50% is the
	// same as between 50% and 25% and so on. Changing the sound on a
	// logarithmic scale will sound to the human ear as if you decrease the
	// sound by equal steps.
	SetVolume(float32)

	// Volume returns a value in the range of 0 (silent) to 1 (full volume).
	Volume() float32

	// SetPan changes the volume ratio between left and right output channel.
	// Setting it to -1 will make channel 1 (left speaker) output at 100% volume
	// while channel 2 (right speaker) has a volume of 0%.
	// A pan of 0 means both speakers' volumes are at 100%, +1 means the left
	// speaker is silenced.
	// This value is clamped to [-1..1]
	SetPan(float32)

	// Pan returns the current pan as a value in the range of -1 (only left
	// speaker) to 1 (only right speaker). A value of 0 means both speakers play
	// at full volume.
	Pan() float32

	// Length is the length of the whole sound, it does not consider how far it
	// is already played or if it loops or not.
	Length() time.Duration

	// SetPosition sets the time offset into the sound at which it will continue
	// to play.
	SetPosition(time.Duration)

	// Position is the current offset from the start of the sound. It changes
	// while the sound is played.
	Position() time.Duration
}

type SoundSource

type SoundSource interface {
	// PlayPaused adds a new one-time sound to the mixer. It is in paused state.
	PlayPaused() Sound
	// PlayOnce adds a new one-time sound to the mixer. It is started right away
	// and stopped when it finishes.
	PlayOnce() Sound

	// SetVolume sets the default volume for all sounds played in the future.
	// Changing the Sound's volume will simply overwrite this setting (instead
	// of combining the factors).
	// The range is [0..1] and it is clamped to that.
	SetVolume(float32)
	Volume() float32

	// SetPan sets the default pan for all sounds played in the future.
	// Changing the Sound's pan will simply overwrite this setting (instead
	// of combining the factors).
	SetPan(float32)
	Pan() float32

	// Length returns the duration of the sound data. Note that a played Sound
	// may have a different value for its Length function as it considers
	// looping.
	Length() time.Duration
}

func NewSoundSource

func NewSoundSource(w *wav.Wave) (SoundSource, error)

NewSound creates a new sound source from the given wave data and starts playing it right away. You can call SetPlaying(false) on the returned sound if you do not want to play the sound right away.

Directories

Path Synopsis
Package wav provides functions to load audio files in the WAV (wave) format.
Package wav provides functions to load audio files in the WAV (wave) format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL