audio

package module
Version: v0.0.0-...-6506882 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 0 Imported by: 0

README

audio: Simple Audio Engine for Go

Package audio is a simple audio engine for Go that can play some music and sound files. It currently supports an SDL2 (Mixer) driver suitable for use on desktop systems like Linux, Mac OS and Windows, with support to load and play music files (.ogg and .mp3 format, depending on your system libraries) and sound effects (.wav).

Example

See the examples/play/main.go for a simple command-line media player sample that uses the SDL2 engine.

package main

import (
    "time"
    "git.kirsle.net/go/audio/sdl"
)

func main() {
    sfx, err := sdl.New(mix.INIT_MP3 | mix.INIT_OGG)
    if err != nil {
        panic(err)
    }

    // Call once at program startup.
    sfx.Setup()
    defer sfx.Teardown()

    // Load a file from disk.
    music, err := sfx.LoadMusic("filename.mp3")
    if err != nil {
        panic(err)
    }

    // Play it.
    music.Play(0)

    // Wait until done.
    for sfx.Playing() {
        time.Sleep(1 * time.Second)
    }
}

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine interface {
	// Setup runs initialization tasks for the audio engine.
	Setup() error

	// Teardown runs closing tasks for the audio engine to shut down gracefully.
	Teardown() error

	// Playing returns a bool if something is actively playing.
	// PlayingMusic and PlayingSound check specifically if music or sound
	// effects are currently playing.
	Playing() bool
	PlayingMusic() bool
	PlayingSound() bool

	// StopAll stops all music and sound effects.
	// StopMusic and StopSounds to selectively stop either the music or all
	// sound effects, respectively.
	StopAll()
	StopMusic()
	StopSounds()

	// LoadMusic opens a music file from disk and loads it into memory.
	// LoadMusicBin to load file by bytes in memory instead.
	LoadMusic(filename string) (Playable, error)
	LoadMusicBin(data []byte) (Playable, error)

	// LoadSound opens a sound effect file.
	// LoadSoundBin to load file by bytes in memory instead.
	LoadSound(filename string) (Playable, error)
	LoadSoundBin(data []byte) (Playable, error)
}

Engine is a music and sound effects driver.

type Playable

type Playable interface {
	Play(loops int) error
	Pause() error
	Stop() error

	// Destroy deallocates and frees the memory.
	Destroy() error
}

Playable is a music or sound effect object that can be played and managed.

Source Files

Directories

Path Synopsis
examples
play
Example program using the SDL2 Mix engine.
Example program using the SDL2 Mix engine.
Package null implements a dummy audio driver that doesn't play any audio.
Package null implements a dummy audio driver that doesn't play any audio.
Package sdl implements an audio engine using libSDL2.
Package sdl implements an audio engine using libSDL2.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL