gnome

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: Unlicense Imports: 19 Imported by: 0

README

gnome

import "github.com/cognusion/go-gnome"

Overview

Package gnome is a library for building a nearly-realtime metro...gnomes. Get it? GET IT?! One would think there would already be such a thing, but given the complexities involved in getting decent timing, there was not. Supports WAV, MP3, and Ogg Vorbis as passthoughs from gopxl/beep (which stands on the shoulders of other giants).

This is not perfect, either. If the system is very busy, the rhythm will not be smooth. If the tempo is exceptionally high, the rhythm will not be smooth. On a normal system, doing nothing else, BPMs under 180 are almost always great.

Gnome was built from scratch expecting WASM as the target platform. Some decisions that may seem odd were made because of that. Most odd decisions are simply odd. The consuming app was written for my brother-in-law's music students: [MetroGnome](https://github.com/cognusion/metrognome).

Index

Package files

gnome.go ts.go

Variables

var (
    // RPool is a recyclable.BufferPool
    RPool = recyclable.NewBufferPool()
)

func BufferToStreamer

func BufferToStreamer(buff Buffer) (beep.StreamSeekCloser, beep.Format, error)

BufferToStreamer checks the first 12 bytes of the Buffer to see if it's a WAV, and tries to decode it as that or an MP3. Errors are returned if anything fails.

func FileToBuffer

func FileToBuffer(filename string) (*recyclable.Buffer, error)

FileToBuffer opens and reads the filename into a Buffer, returning it or an error.

func FromBPM

func FromBPM(bpm int32) time.Duration

FromBPM converts a beats-per-minute tempo to a Microsecond-precise time.Duration.

func ToBPM

func ToBPM(interval time.Duration) int32

ToBPM converts a Microsecond-precise time.Duration to a beats-per-minute tempo.

type Buffer

type Buffer = io.ReadSeekCloser

Buffer is our local interface to encapsulate all the interfaces

type Gnome

type Gnome struct {
    // TS tracks and reports the time signature information for the 'gnome.
    TS *TimeSignature
    // contains filtered or unexported fields
}

Gnome is a metro...gnome. Get it? Get it?!

func NewGnomeFromBuffer
func NewGnomeFromBuffer(buff Buffer, ts *TimeSignature, tickFunc TickFunc) (*Gnome, error)

NewGnomeFromBuffer takes a Buffer, a TimeSignature and an optional tickFunc to call when the 'gnome fires, and gives you a Gnome or an error. :)

func NewGnomeFromFile
func NewGnomeFromFile(soundFile string, ts *TimeSignature, tickFunc TickFunc) (*Gnome, error)

NewGnomeFromFile takes a filename, a TimeSignature and an optional tickFunc to call when the 'gnome fires, and gives you a Gnome or an error. :)

func (*Gnome) Change
func (g *Gnome) Change(tempo int32)

Change sets a new tempo.

func (*Gnome) Close
func (g *Gnome) Close()

Close terminally cleans up all the things.

func (*Gnome) IsPaused
func (g *Gnome) IsPaused() bool

IsPaused returns if the 'gnome is paused. This should not be used for blocking or timing decisions

func (*Gnome) IsRunning
func (g *Gnome) IsRunning() bool

IsRunning returns if the 'gnome is running.

func (*Gnome) Mute
func (g *Gnome) Mute()

Mute toggles whether or not audio will play.

func (*Gnome) Pan
func (g *Gnome) Pan()

Pan toggles whether or not beats will alternate on speakers.

func (*Gnome) Pause
func (g *Gnome) Pause()

Pause toggles whether the 'gnome is paused.

func (*Gnome) ReplaceStreamerFromBuffer
func (g *Gnome) ReplaceStreamerFromBuffer(buff Buffer) error

ReplaceStreamerFromBuffer attempts to replace the current steamer with a new one from the provided buffer.

func (*Gnome) Restart
func (g *Gnome) Restart() error

Restart will re-initialize some stopped components so the 'gnome can carry on.

func (*Gnome) SetTickFilter
func (g *Gnome) SetTickFilter(tf TickFilter) error

SetTickFilter installs a new tickFilter. A tickFilter is passed the beat count and returns true if the sound should be played for that beat

func (*Gnome) Start
func (g *Gnome) Start()

Start -s the Gnome. Only works the first time you call it.

func (*Gnome) Stop
func (g *Gnome) Stop()

Stop stops the Gnome.

type TickFilter

type TickFilter func(int) bool

TickFilter is passed the beat count, and returns true if the tick sound should be played

type TickFunc

type TickFunc func(int)

TickFunc is a function called during a tick event. The current beat number is passed to it. These functions must absolutely handle being called multiple times in quick succession, handling order, overlaps, and all of that jazz.

type TimeSignature

type TimeSignature struct {
    Beats     atomic.Int32
    NoteValue atomic.Int32
    Tempo     atomic.Int32
}

TimeSignature is a mechaism to make tracking time signatures, and their changes, easier and safe.

func NewTimeSignature
func NewTimeSignature(beats, noteValue, tempo int32) *TimeSignature

NewTimeSignature returns an initialized TimeSignature.

func (*TimeSignature) FromString
func (ts *TimeSignature) FromString(sig string) error

FromString takes a signature string (e.g. "4/4" or "6/8") and sets TimeSignature accordingly.

func (*TimeSignature) String
func (ts *TimeSignature) String() string

String pretty-prints the values of the TimeSignature.

func (*TimeSignature) TempoFromDuration
func (ts *TimeSignature) TempoFromDuration(interval time.Duration)

TempoFromDuration sets the Tempo based on the interval provided.

func (*TimeSignature) TempoToDuration
func (ts *TimeSignature) TempoToDuration() time.Duration

TempoToDuration returns the tempo as a time.Duration.

func (*TimeSignature) ToString
func (ts *TimeSignature) ToString() string

ToString returns the signature string (e.g. "4/4" or "6/8"), without the Tempo.


Generated by godoc2md

Documentation

Overview

Package gnome is a library for building a nearly-realtime metro...gnomes. Get it? GET IT?! One would think there would already be such a thing, but given the complexities involved in getting decent timing, there was not. Supports WAV, MP3, and Ogg Vorbis as passthoughs from `gopxl/beep` (which stands on the shoulders of other giants).

This is not perfect, either. If the system is very busy, the rhythm will not be smooth. If the tempo is exceptionally high, the rhythm will not be smooth. On a normal system, doing nothing else, BPMs under 180 are almost always great.

Gnome was built from scratch expecting WASM as the target platform. Some decisions that may seem odd were made because of that. Most odd decisions are simply odd. The consuming app was written for my brother-in-law's music students: [MetroGnome](https://github.com/cognusion/metrognome).

Index

Constants

This section is empty.

Variables

View Source
var (
	// RPool is a recyclable.BufferPool
	RPool = recyclable.NewBufferPool()
)

Functions

func BufferToStreamer

func BufferToStreamer(buff Buffer) (beep.StreamSeekCloser, beep.Format, error)

BufferToStreamer checks the first 12 bytes of the Buffer to see if it's a WAV, and tries to decode it as that or an MP3. Errors are returned if anything fails.

func FileToBuffer

func FileToBuffer(filename string) (*recyclable.Buffer, error)

FileToBuffer opens and reads the filename into a Buffer, returning it or an error.

func FromBPM

func FromBPM(bpm int32) time.Duration

FromBPM converts a beats-per-minute tempo to a Microsecond-precise time.Duration.

func ToBPM

func ToBPM(interval time.Duration) int32

ToBPM converts a Microsecond-precise time.Duration to a beats-per-minute tempo.

Types

type Buffer added in v0.2.0

type Buffer = io.ReadSeekCloser

Buffer is our local interface to encapsulate all the interfaces

type Gnome

type Gnome struct {
	// TS tracks and reports the time signature information for the 'gnome.
	TS *TimeSignature
	// contains filtered or unexported fields
}

Gnome is a metro...gnome. Get it? Get it?!

func NewGnomeFromBuffer added in v0.3.0

func NewGnomeFromBuffer(buff Buffer, ts *TimeSignature, tickFunc TickFunc) (*Gnome, error)

NewGnomeFromBuffer takes a Buffer, a TimeSignature and an optional tickFunc to call when the 'gnome fires, and gives you a Gnome or an error. :)

func NewGnomeFromFile added in v0.3.0

func NewGnomeFromFile(soundFile string, ts *TimeSignature, tickFunc TickFunc) (*Gnome, error)

NewGnomeFromFile takes a filename, a TimeSignature and an optional tickFunc to call when the 'gnome fires, and gives you a Gnome or an error. :)

func (*Gnome) Change

func (g *Gnome) Change(tempo int32)

Change sets a new tempo.

func (*Gnome) Close

func (g *Gnome) Close()

Close terminally cleans up all the things.

func (*Gnome) IsPaused added in v0.4.0

func (g *Gnome) IsPaused() bool

IsPaused returns if the 'gnome is paused. This should not be used for blocking or timing decisions

func (*Gnome) IsRunning

func (g *Gnome) IsRunning() bool

IsRunning returns if the 'gnome is running.

func (*Gnome) Mute

func (g *Gnome) Mute()

Mute toggles whether or not audio will play.

func (*Gnome) Pan added in v0.6.1

func (g *Gnome) Pan()

Pan toggles whether or not beats will alternate on speakers.

func (*Gnome) Pause

func (g *Gnome) Pause()

Pause toggles whether the 'gnome is paused.

func (*Gnome) ReplaceStreamerFromBuffer added in v0.6.1

func (g *Gnome) ReplaceStreamerFromBuffer(buff Buffer) error

ReplaceStreamerFromBuffer attempts to replace the current steamer with a new one from the provided buffer.

func (*Gnome) Restart

func (g *Gnome) Restart() error

Restart will re-initialize some stopped components so the 'gnome can carry on.

func (*Gnome) SetTickFilter added in v0.5.2

func (g *Gnome) SetTickFilter(tf TickFilter) error

SetTickFilter installs a new tickFilter. A tickFilter is passed the beat count and returns true if the sound should be played for that beat

func (*Gnome) Start

func (g *Gnome) Start()

Start -s the Gnome. Only works the first time you call it.

func (*Gnome) Stop

func (g *Gnome) Stop()

Stop stops the Gnome.

type TickFilter added in v0.5.2

type TickFilter func(int) bool

TickFilter is passed the beat count, and returns true if the tick sound should be played

type TickFunc added in v0.5.0

type TickFunc func(int)

TickFunc is a function called during a tick event. The current beat number is passed to it. These functions must absolutely handle being called multiple times in quick succession, handling order, overlaps, and all of that jazz.

type TimeSignature

type TimeSignature struct {
	Beats     atomic.Int32
	NoteValue atomic.Int32
	Tempo     atomic.Int32
}

TimeSignature is a mechaism to make tracking time signatures, and their changes, easier and safe.

func NewTimeSignature added in v0.3.0

func NewTimeSignature(beats, noteValue, tempo int32) *TimeSignature

NewTimeSignature returns an initialized TimeSignature.

func (*TimeSignature) FromString

func (ts *TimeSignature) FromString(sig string) error

FromString takes a signature string (e.g. "4/4" or "6/8") and sets TimeSignature accordingly.

func (*TimeSignature) String

func (ts *TimeSignature) String() string

String pretty-prints the values of the TimeSignature.

func (*TimeSignature) TempoFromDuration

func (ts *TimeSignature) TempoFromDuration(interval time.Duration)

TempoFromDuration sets the Tempo based on the interval provided.

func (*TimeSignature) TempoToDuration

func (ts *TimeSignature) TempoToDuration() time.Duration

TempoToDuration returns the tempo as a time.Duration.

func (*TimeSignature) ToString

func (ts *TimeSignature) ToString() string

ToString returns the signature string (e.g. "4/4" or "6/8"), without the Tempo.

Directories

Path Synopsis
Package speaker implements playback of beep.Streamer values through physical speakers.
Package speaker implements playback of beep.Streamer values through physical speakers.

Jump to

Keyboard shortcuts

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