audio

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package audio provides functions to play sound effects and music. Call Sfx() to play sound effect and Music() to play music.

Sound effects and music can be changed using functions: SetSfx and SetMusic.

Index

Constants

View Source
const SampleRate = internal.SampleRate

Variables

View Source
var (
	Sfx [64]SoundEffect // Sound effects
	Pat [64]Pattern     // Music patterns
)

Functions

func Load

func Load(state []byte) error

Load restores audio system state from byte slice. State is restored from binary form.

func Music

func Music(patterNo int, fadeMs int, channelMask byte)

Music starts playing music with given patterNo.

patterNo is the number of music pattern to play (0-63). patternNo=-1 stops playing music.

fadeMs fades in (or out) the music volume over a duration, given as number of milliseconds.

channelMask is a bitfield indicating which of the four sound channels should be reserved for music.

Not implemented yet.

func Play

func Play(sfxNo int, channel, offset, length int)

Play starts playing sound effect with given sfxNo on specified channel.

sfxNo is the number of sound effect to play (0-63).

channel is the channel number from 0-3. -1 chooses available channel automatically.

offset is the note position to start playing (0-31). Offset is clamped to [0,31].

length is the number of notes to play. If length <= 0 and sfx has no loop then entire sfx is played. If length < sfx length then only fraction of sfx is played. If length <= 0 and sfx has loop then sfx is played infinitely.

func Save

func Save() ([]byte, error)

Save stores audio system state to byte slice. State is stored in binary form.

func SetSystem

func SetSystem(s System)

SetSystem is executed by the back-end to replace audio system with his own implementation.

func Stop

func Stop(sfxNo int)

Stop stops playing sound effect with given sfxNo. -1 stops all sounds.

sfxNo is the number of sound effect to stop (0-63).

func StopChan

func StopChan(channel int)

channel is the channel number from 0-3. -1 stops all channels.

func StopLoop

func StopLoop(channel int)

channel is the channel number from 0-3. -1 stops loop on all channels.

func Sync

func Sync()

Sync is required for changes made to Sfx and Pat to be audible. Sync is automatically run after each command issued via devtools terminal.

Types

type Effect

type Effect byte // Not implemented yet.
const (
	EffectNoEffect Effect = 0 // Not implemented yet.
	EffectSlide    Effect = 1 // Not implemented yet.
	EffectVibrato  Effect = 2 // Not implemented yet.
	EffectDrop     Effect = 3 // Not implemented yet.
	EffectFadeIn   Effect = 4 // Not implemented yet.
	EffectFadeOut  Effect = 5 // Not implemented yet.
	EffectArpFast  Effect = 6 // Not implemented yet.
	EffectArpSlow  Effect = 7 // Not implemented yet.
)

type Instrument

type Instrument byte
const (
	InstrumentTriangle  Instrument = 0
	InstrumentTiltedSaw Instrument = 1
	InstrumentSaw       Instrument = 2
	InstrumentSquare    Instrument = 3
	InstrumentPulse     Instrument = 4
	InstrumentOrgan     Instrument = 5
	InstrumentNoise     Instrument = 6
	InstrumentPhaser    Instrument = 7
	InstrumentSfx0      Instrument = 8  // Not implemented yet.
	InstrumentSfx1      Instrument = 9  // Not implemented yet.
	InstrumentSfx2      Instrument = 10 // Not implemented yet.
	InstrumentSfx3      Instrument = 11 // Not implemented yet.
	InstrumentSfx4      Instrument = 12 // Not implemented yet.
	InstrumentSfx5      Instrument = 13 // Not implemented yet.
	InstrumentSfx6      Instrument = 14 // Not implemented yet.
	InstrumentSfx7      Instrument = 15 // Not implemented yet.
)

type LiveReader

type LiveReader struct {
	BufferSize      time.Duration
	ReadSamplesFunc func([]float64)
	Now             func() time.Time
	// contains filtered or unexported fields
}

LiveReader is used by backend to read samples live taking into consideration current time. It drops samples when ReadSamples was called too late, and limits how much samples can be read.

func (*LiveReader) ReadSamples

func (l *LiveReader) ReadSamples(buf []float64) int

ReadSamples reads samples and writes them to buf. Returns how much samples was written to buffer.

type Note

type Note struct {
	Pitch      Pitch      // 0-63
	Instrument Instrument // 0-15
	Volume     Volume     // 0-7
	Effect     Effect     // 0-7. Not implemented yet.
}

type Pattern

type Pattern struct {
	Sfx          [4]PatternSfx // Not implemented yet.
	BeginLoop    bool          // Not implemented yet.
	EndLoop      bool          // Not implemented yet.
	StopAtTheEnd bool          // Not implemented yet.
}

Pattern is a music pattern

type PatternSfx

type PatternSfx struct {
	SfxNo   byte // 0-63. Not implemented yet.
	Enabled bool // Not implemented yet.
}

type Pitch

type Pitch byte
const (
	PitchC0  Pitch = 0
	PitchCs0 Pitch = 1 // C#0
	PitchD0  Pitch = 2
	PitchDs0 Pitch = 3
	PitchE0  Pitch = 4
	PitchF0  Pitch = 5
	PitchFs0 Pitch = 6
	PitchG0  Pitch = 7
	PitchGs0 Pitch = 8
	PitchA0  Pitch = 9
	PitchAs0 Pitch = 10
	PitchB0  Pitch = 11

	PitchC1  Pitch = 12
	PitchCs1 Pitch = 13
	PitchD1  Pitch = 14
	PitchDs1 Pitch = 15
	PitchE1  Pitch = 16
	PitchF1  Pitch = 17
	PitchFs1 Pitch = 18
	PitchG1  Pitch = 19
	PitchGs1 Pitch = 20
	PitchA1  Pitch = 21
	PitchAs1 Pitch = 22
	PitchB1  Pitch = 23

	PitchC2  Pitch = 24
	PitchCs2 Pitch = 25
	PitchD2  Pitch = 26
	PitchDs2 Pitch = 27
	PitchE2  Pitch = 28
	PitchF2  Pitch = 29
	PitchFs2 Pitch = 30
	PitchG2  Pitch = 31
	PitchGs2 Pitch = 32
	PitchA2  Pitch = 33
	PitchAs2 Pitch = 34
	PitchB2  Pitch = 35

	PitchC3  Pitch = 36
	PitchCs3 Pitch = 37
	PitchD3  Pitch = 38
	PitchDs3 Pitch = 39
	PitchE3  Pitch = 40
	PitchF3  Pitch = 41
	PitchFs3 Pitch = 42
	PitchG3  Pitch = 43
	PitchGs3 Pitch = 44
	PitchA3  Pitch = 45
	PitchAs3 Pitch = 46
	PitchB3  Pitch = 47

	PitchC4  Pitch = 48
	PitchCs4 Pitch = 49
	PitchD4  Pitch = 50
	PitchDs4 Pitch = 51
	PitchE4  Pitch = 52
	PitchF4  Pitch = 53
	PitchFs4 Pitch = 54
	PitchG4  Pitch = 55
	PitchGs4 Pitch = 56
	PitchA4  Pitch = 57
	PitchAs4 Pitch = 58
	PitchB4  Pitch = 59

	PitchC5  Pitch = 60
	PitchCs5 Pitch = 61
	PitchD5  Pitch = 62
	PitchDs5 Pitch = 63
)

type SfxStat

type SfxStat struct {
	SfxNo     int // -1 means no sfx on channel
	Note      int // current note in sfx
	Remaining int // number of remaining notes in sfx. -1 if loop
}

type SoundEffect

type SoundEffect struct {
	Notes     [32]Note
	Speed     byte // 1 is the fastest (~8.33ms). For 120 the length of one note becomes 1 second. 0 means SoundEffect takes no time.
	LoopStart byte // 0-63. Notes 32-63 are silent.
	LoopStop  byte // 0-63. Notes 32-63 are silent.
	Detune    byte // 0 (disabled), 1 or 2. Not implemented yet.
	Reverb    byte // 0 (disabled), 1 or 2. Not implemented yet.
	Dampen    byte // 0 (disabled), 1 or 2. Not implemented yet.
	Noiz      bool // Not implemented yet.
	Buzz      bool // Not implemented yet.
}

func (SoundEffect) String

func (s SoundEffect) String() string

type Stat

type Stat struct {
	Sfx           [4]SfxStat
	Pattern       int // currently played music pattern. Not implemented yet.
	PatternsCount int // the number of music patterns played since the most recent call to Music(). Not implemented yet.
	TicksCount    int // the number of ticks (notes or rests) played on the current pattern. Not implemented yet.
}

func GetStat

func GetStat() Stat

GetStat returns information about currently played sound effects and music.

type Synthesizer

type Synthesizer struct {
	// contains filtered or unexported fields
}

Synthesizer is used by a back-end. It is an System implementation. Plus it provides method for synthesizing audio samples.

func (*Synthesizer) GetMusic

func (s *Synthesizer) GetMusic(patterNo int) (p Pattern)

func (*Synthesizer) GetSfx

func (s *Synthesizer) GetSfx(sfxNo int) (e SoundEffect)

func (*Synthesizer) Music

func (s *Synthesizer) Music(patterNo int, fadeMs int, channelMask byte)

func (*Synthesizer) Play

func (s *Synthesizer) Play(sfxNo, ch, offset, length int)

func (*Synthesizer) ReadSamples

func (s *Synthesizer) ReadSamples(buffer []float64)

ReadSamples method is used by a back-end to read generated audio samples and play them back to the user. The sample rate is 22050. One channel is used (mono).

ReadSamples is (usually) executed concurrently with main game loop. Back-end should add proper synchronization to avoid race conditions. Back-end could decide about buffer size, although the higher the size the higher the lag. Usually the buffer is 512 samples, which is 23ms of audio.

Values written to the buffer are usually in range between -1.0 and 1.0, but sometimes they can exceed the range (for example due to audio channels summing). Min is -4.0, max is 4.0 inclusive.

func (*Synthesizer) SetMusic

func (s *Synthesizer) SetMusic(patternNo int, pattern Pattern)

func (*Synthesizer) SetSfx

func (s *Synthesizer) SetSfx(sfxNo int, effect SoundEffect)

func (*Synthesizer) Stat

func (s *Synthesizer) Stat() Stat

func (*Synthesizer) Stop

func (s *Synthesizer) Stop(sfxNo int)

func (*Synthesizer) StopChan

func (s *Synthesizer) StopChan(channel int)

func (*Synthesizer) StopLoop

func (s *Synthesizer) StopLoop(channel int)

type System

type System interface {
	Play(sfxNo, channel, offset, length int)
	Stop(sfxNo int)
	StopChan(channel int)
	StopLoop(channel int)
	Music(patterNo int, fadeMs int, channelMask byte)
	Stat() Stat
	// SetSfx updates the sound effect. sfxNo is 0-63. Updating sfx number which
	// is higher than 63 does not do anything.
	//
	// SoundEffect parameters are clamped when out of range.
	// For example, sfx note volume equal 8 will be silently clamped to 7.
	SetSfx(sfxNo int, e SoundEffect)
	// SetMusic updates the music pattern. patternNo is 0-63. Updating pattern number which
	// is higher than 63 does not do anything.
	//
	// Pattern parameters are clamped when out of range.
	// For example, pattern sfx number equal 64 will be silently clamped to 63.
	SetMusic(patternNo int, _ Pattern)
}

type Volume

type Volume byte
const (
	VolumeSilence Volume = 0
	VolumeLoudest Volume = 7
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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