Help us shape the future of Go! Take the 2026 Go Developer Survey

atomix

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2016 License: MIT Imports: 6 Imported by: 0

README

Atomix

Build Status

Sequence-based Go-native audio mixer for Music apps
package main

import (
  "fmt"
  "github.com/outrightmental/go-atomix"
  "github.com/outrightmental/go-atomix/bind"
  "os"
  "time"
)

var (
  sampleHz   = int32(44100)
  spec = bind.AudioSpec{
    Freq:     sampleHz,
    Format:   bind.AudioF32LSB,
    Channels: 1,
    }
  bpm        = 120
  step       = time.Minute / time.Duration(bpm*4)
  loops      = 16
  prefix     = "assets/sounds/percussion/808/"
  kick1      = "kick1.wav"
  kick2      = "kick2.wav"
  marac      = "maracas.wav"
  snare      = "snare.wav"
  hitom      = "hightom.wav"
  clhat      = "cl_hihat.wav"
  pattern    = []string{
    kick2,
    marac,
    clhat,
    marac,
    snare,
    marac,
    clhat,
    kick2,
    marac,
    marac,
    hitom,
    marac,
    snare,
    kick1,
    clhat,
    marac,
  }
)

func main() {
  defer func() {
    atomix.Teardown()
  }()

  atomix.Debug(true)
  atomix.Configure(spec)
  atomix.SetSoundsPath(prefix)
  atomix.StartAt(time.Now().Add(1 * time.Second))

  t := 1 * time.Second // padding before music
  for n := 0; n < loops; n++ {
    for s := 0; s < len(pattern); s++ {
      atomix.SetFire(pattern[s], t+time.Duration(s)*step, 0, 1.0, 0)
    }
    t += time.Duration(len(pattern)) * step
  }

  atomix.OpenAudio()

  fmt.Printf("SDL OpenAudio > Atomix, pid:%v, spec:%v\n", os.Getpid(), spec)
  time.Sleep(t + 1*time.Second) // wait until music + 1 second
}
What?

Game audio mixers are designed to play audio spontaneously, but when the timing is known in advance (e.g. sequence-based music apps) there is a demand for much greater accuracy in playback timing.

Read the API documentation at godoc.org/github.com/outrightmental/go-atomix

Atomix seeks to solve the problem of audio mixing on top of bare SDL, specifically for the purpose of the playback of sequences where audio files and their playback timing is known in advance.

Though it is called via C bindings by the SDL audio callback, atomix stores and mixes audio in native Go []float64 and natively implements Paul Vögler's "Loudness Normalization by Logarithmic Dynamic Range Compression" (details below)

Built on go-sdl2 - Go bindings for the C++ library "Simple DirectMedia Layer" SDL 2.0

Author: Charney Kaye

NOTICE: THIS PROJECT IS IN ALPHA STAGE, AND THE API MAY BE SUBJECT TO CHANGE.

Best efforts will be made to preserve each API version in a release tag that can be parsed, e.g. http://gopkg.in

Why?

Even after selecting a hardware interface library such as C++ SDL 2.0 via go-sdl2, there remains a critical design problem to be solved.

This design is a music application mixer. Most available options are geared towards Game development, including the proprietary SDL_mixer project for which the go-sdl2 team has also implemented bindings.

Game audio mixers offer playback timing accuracy +/- 2 milliseconds. But that's totally unacceptable for music, specifically sequence-based sample playback.

The design pattern particular to Game design is that the timing of the audio is not know in advance- the timing that really matterns is that which is assembled in near-real-time in response to user interaction.

In the field of Music development, often the timing is known in advance, e.g. a *sequencer, the composition of music by specifying exactly how, when and which audio files will be played relative to the beginning of playback.

Ergo, atomix seeks to solve the problem of audio mixing on top of bare SDL, specifically for the purpose of the playback of sequences where audio files and their playback timing is known in advance. It seeks to do this with the absolute minimal logical overhead on top of SDL.

Atomix takes maximum advantage of Go by storing and mixing audio in native Go []float64 and natively implementing Paul Vögler's "Loudness Normalization by Logarithmic Dynamic Range Compression"

Time

To the Atomix API, time is specified as a time.Duration-since-epoch, where the epoch is the moment that atomix.Start() was called.

Internally, time is tracked as samples-since-epoch at the master output playback frequency (e.g. 48000 Hz). This is most efficient because source audio is pre-converted to the master output playback frequency, and all audio maths are performed in terms of samples.

The Mixing Algorithm

Insipired by the theory paper "Mixing two digital audio streams with on the fly Loudness Normalization by Logarithmic Dynamic Range Compression" by Paul Vögler, 2012-04-20. A .PDF has been included here, from the paper originally published here.

Usage

There's an example implementation of go-sdl2 + go-atomix included in the example/ folder in this repository.

Documentation

Overview

Package atomix is a sequence-based Go-native audio mixer

Package atomix is a sequence-based Go-native audio mixer

Package atomix is a sequence-based Go-native audio mixer

Package atomix is a sequence-based Go-native audio mixer

Index

Constants

View Source
const VERSION = "0.0.2"

VERSION # of this go-atomix source code

Variables

This section is empty.

Functions

func Configure

func Configure(spec bind.AudioSpec)

Configure the mixer frequency, format, channels & sample rate.

func Debug

func Debug(isOn bool)

Debug ON/OFF (ripples down to all sub-modules)

func GetStartTime added in v0.0.2

func GetStartTime() time.Time

GetStartTime the mixer was started at

func OpenAudio added in v0.0.2

func OpenAudio()

OpenAudio begins streaming to the bound output audio interface, via a callback function

func SetSoundsPath added in v0.0.2

func SetSoundsPath(prefix string)

SetSoundsPath prefix

func Spec

func Spec() *bind.AudioSpec

Spec for the mixer, which may include callback functions, e.g. go-sdl2

func Start

func Start()

Start the mixer now

func StartAt

func StartAt(t time.Time)

StartAt a specific time in the future

func Teardown

func Teardown()

Teardown everything and release all memory.

Types

type Fire

type Fire struct {
	/* setup */
	BeginTz Tz
	EndTz   Tz
	Source  string
	Volume  float64
	Pan     float64
	// contains filtered or unexported fields
}

Fire represents a single audio source playing at a specific time in the future.

func NewFire

func NewFire(source string, beginTz Tz, endTz Tz, volume float64, pan float64) *Fire

NewFire to represent a single audio source playing at a specific time in the future.

func SetFire

func SetFire(source string, begin time.Duration, sustain time.Duration, volume float64, pan float64) *Fire

SetFire to represent a single audio source playing at a specific time in the future.

func (*Fire) At

func (f *Fire) At(at Tz) (t Tz)

At the series of Tz it's playing for, return the series of Tz corresponding to source audio.

func (*Fire) IsAlive

func (f *Fire) IsAlive() bool

IsAlive the Fire?

func (*Fire) IsPlaying added in v0.0.2

func (f *Fire) IsPlaying() bool

IsPlaying the Fire?

func (*Fire) Teardown added in v0.0.2

func (f *Fire) Teardown()

Teardown the Fire and release its memory

type Source

type Source struct {
	URL string
	// contains filtered or unexported fields
}

Source stores a series of Samples in Channels across Time, for audio playback.

func NewSource

func NewSource(URL string) *Source

NewSource from a "URL" (which is actually only a file path for now)

func (*Source) Length added in v0.0.2

func (s *Source) Length() Tz

Length of the source audio in Tz

func (*Source) SampleAt

func (s *Source) SampleAt(at Tz) float64

SampleAt at a specific Tz (currently only mono, uses channel 0)

func (*Source) Teardown

func (s *Source) Teardown()

Teardown the source audio and release its memory.

type Tz

type Tz uint64

Tz is the unit of measurement of samples-over-time, e.g. for 48000Hz playback there are 48,000 Tz in 1 second.

Directories

Path Synopsis
Package bind is for modular binding of atomix to audio interface Package bind is for modular binding of atomix to audio interface Package bind has native Go WAV I/O
Package bind is for modular binding of atomix to audio interface Package bind is for modular binding of atomix to audio interface Package bind has native Go WAV I/O

Jump to

Keyboard shortcuts

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