waveform: github.com/mdlayher/waveform Index | Examples | Files

package waveform

import "github.com/mdlayher/waveform"

Package waveform is capable of generating waveform images from audio streams. MIT Licensed.

Index

Examples

Package Files

colorfunc.go options.go samplereducefunc.go waveform.go

Variables

var (
    // ErrFormat is returned when the input audio format is not a registered format
    // with the audio package.
    ErrFormat = audio.ErrFormat

    // ErrInvalidData is returned when the input audio format is recognized, but
    // the stream is invalid or corrupt in some way.
    ErrInvalidData = audio.ErrInvalidData

    // ErrUnexpectedEOS is returned when end-of-stream is encountered in the middle
    // of a fixed-size block or data structure.
    ErrUnexpectedEOS = audio.ErrUnexpectedEOS
)

Error values from azul3d/engine/audio are wrapped, so that callers do not have to import an additional package to check for common errors.

func Generate Uses

func Generate(r io.Reader, options ...OptionsFunc) (image.Image, error)

Generate immediately opens and reads an input audio stream, computes the values required for waveform generation, and returns a waveform image which is customized by zero or more, variadic, OptionsFunc parameters.

Generate is equivalent to calling New, followed by the Compute and Draw methods of a Waveform struct. In general, Generate should only be used for one-time waveform image generation.

ExampleGenerate provides example usage of Generate, using a media file from the filesystem. Generate is typically used for one-time, direct creation of an image.Image from an input audio stream.

Code:

// Generate accepts io.Reader, so we will use a media file in the filesystem
file, err := os.Open("./test/tone16bit.flac")
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println("open:", file.Name())
defer file.Close()

// Directly generate waveform image from audio file, applying any number
// of options functions along the way
img, err := Generate(file,
    // Solid white background
    BGColorFunction(SolidColor(color.White)),
    // Striped red, green, and blue foreground
    FGColorFunction(StripeColor(
        color.RGBA{255, 0, 0, 255},
        color.RGBA{0, 255, 0, 255},
        color.RGBA{0, 0, 255, 255},
    )),
    // Scaled 10x horizontally, 2x vertically
    Scale(10, 2),
)
if err != nil {
    fmt.Println(err)
    return
}

// Encode image as PNG into buffer
buf := bytes.NewBuffer(nil)
if err := png.Encode(buf, img); err != nil {
    fmt.Println(err)
    return
}
fmt.Printf("encoded: %d bytes\nresolution: %s", buf.Len(), img.Bounds().Max)

Output:

open: ./test/tone16bit.flac
encoded: 344 bytes
resolution: (50,256)

func RMSF64Samples Uses

func RMSF64Samples(samples audio.Float64) float64

RMSF64Samples is a SampleReduceFunc which calculates the root mean square of a slice of float64 audio samples, enabling the measurement of magnitude over the entire set of samples.

Derived from: http://en.wikipedia.org/wiki/Root_mean_square.

type ColorFunc Uses

type ColorFunc func(n int, x int, y int, maxN int, maxX int, maxY int) color.Color

ColorFunc is a function which accepts a variety of values which can be used to customize an output image. These values include the current computed sample count (n), the current X coordinate (x), the current Y coordinate (y), and the maximum computed values for each of these. (maxN, maxX, maxY)

A ColorFunc is applied during each image drawing iteration, and will return the appropriate color which should be drawn at the specified value for n, x, and y; possibly taking into account their maximum values.

func CheckerColor Uses

func CheckerColor(colorA color.Color, colorB color.Color, size uint) ColorFunc

CheckerColor generates a ColorFunc which produces a checkerboard pattern, using the two input colors. Each square is drawn to the size specified by the size parameter.

func FuzzColor Uses

func FuzzColor(colors ...color.Color) ColorFunc

FuzzColor generates a ColorFunc which applies a random color on each call, selected from an input, variadic slice of colors. This can be used to create a random fuzz or "static" effect in the resulting waveform image.

func GradientColor Uses

func GradientColor(start color.RGBA, end color.RGBA) ColorFunc

GradientColor generates a ColorFunc which produces a color gradient between two RGBA input colors. The gradient attempts to gradually reduce the distance between two colors, creating a sweeping color change effect in the resulting waveform image.

func SolidColor Uses

func SolidColor(inColor color.Color) ColorFunc

SolidColor generates a ColorFunc which simply returns the input color as the color which should be drawn at all coordinates.

This is the default behavior of the waveform package.

func StripeColor Uses

func StripeColor(colors ...color.Color) ColorFunc

StripeColor generates a ColorFunc which applies one color from the input, variadic slice at each computed value. Each color is used in order, and the rotation will repeat until the image is complete. This creates a stripe effect in the resulting waveform image.

type OptionsError Uses

type OptionsError struct {
    Option string
    Reason string
}

OptionsError is an error which is returned when invalid input options are set on a Waveform struct.

func (*OptionsError) Error Uses

func (e *OptionsError) Error() string

Error returns the string representation of an OptionsError.

type OptionsFunc Uses

type OptionsFunc func(*Waveform) error

OptionsFunc is a function which is applied to an input Waveform struct, and can manipulate its properties.

func BGColorFunction Uses

func BGColorFunction(function ColorFunc) OptionsFunc

BGColorFunction generates an OptionsFunc which applies the input background ColorFunc to an input Waveform struct.

This function is used to apply a variety of color schemes to the background of a waveform image, and is called during each drawing loop of the background image.

func FGColorFunction Uses

func FGColorFunction(function ColorFunc) OptionsFunc

FGColorFunction generates an OptionsFunc which applies the input foreground ColorFunc to an input Waveform struct.

This function is used to apply a variety of color schemes to the foreground of a waveform image, and is called during each drawing loop of the foreground image.

func Resolution Uses

func Resolution(resolution uint) OptionsFunc

Resolution generates an OptionsFunc which applies the input resolution value to an input Waveform struct.

This value indicates the number of times audio is read and drawn as a waveform, per second of audio.

func SampleFunction Uses

func SampleFunction(function SampleReduceFunc) OptionsFunc

SampleFunc generates an OptionsFunc which applies the input SampleReduceFunc to an input Waveform struct.

This function is used to compute values from audio samples, for use in waveform generation. The function is applied over a slice of float64 audio samples, reducing them to a single value.

func Scale Uses

func Scale(x uint, y uint) OptionsFunc

Scale generates an OptionsFunc which applies the input X and Y axis scaling factors to an input Waveform struct.

This value indicates how a generated waveform image will be scaled, for both its X and Y axes.

func ScaleClipping Uses

func ScaleClipping() OptionsFunc

ScaleClipping generates an OptionsFunc which sets the scaleClipping member to true on an input Waveform struct.

This value indicates if the waveform image should be scaled down on its Y-axis when clipping thresholds are reached. This can be used to show a more accurate waveform when the input audio stream exhibits signs of clipping.

func Sharpness Uses

func Sharpness(sharpness uint) OptionsFunc

Sharpness generates an OptionsFunc which applies the input sharpness value to an input Waveform struct.

This value indicates the amount of curvature which is applied to a waveform image, scaled on its X-axis. A higher value results in steeper curves, and a lower value results in more "blocky" curves.

type SampleReduceFunc Uses

type SampleReduceFunc func(samples audio.Float64) float64

SampleReduceFunc is a function which reduces a set of float64 audio samples into a single float64 value.

type Waveform Uses

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

Waveform is a struct which can be manipulated and used to generate audio waveform images from an input audio stream.

func New Uses

func New(r io.Reader, options ...OptionsFunc) (*Waveform, error)

New generates a new Waveform struct, applying any input OptionsFunc on return.

func (*Waveform) Compute Uses

func (w *Waveform) Compute() ([]float64, error)

Compute creates a slice of float64 values, computed using an input function.

Compute is typically used once on an audio stream, to read and calculate the values used for subsequent waveform generations. Its return value can be used with Draw to generate and customize multiple waveform images from a single stream.

func (*Waveform) Draw Uses

func (w *Waveform) Draw(values []float64) image.Image

Draw creates a new image.Image from a slice of float64 values.

Draw is typically used after a waveform has been computed one time, and a slice of computed values was returned from the first computation. Subsequent calls to Draw may be used to customize a waveform using the same input values.

func (*Waveform) SetBGColorFunction Uses

func (w *Waveform) SetBGColorFunction(function ColorFunc) error

SetBGColorFunction applies the input ColorFunc to the receiving Waveform struct for background use.

func (*Waveform) SetFGColorFunction Uses

func (w *Waveform) SetFGColorFunction(function ColorFunc) error

SetFGColorFunction applies the input ColorFunc to the receiving Waveform struct for foreground use.

func (*Waveform) SetOptions Uses

func (w *Waveform) SetOptions(options ...OptionsFunc) error

SetOptions applies zero or more OptionsFunc to the receiving Waveform struct, manipulating its properties.

func (*Waveform) SetResolution Uses

func (w *Waveform) SetResolution(resolution uint) error

SetResolution applies the input resolution to the receiving Waveform struct.

func (*Waveform) SetSampleFunction Uses

func (w *Waveform) SetSampleFunction(function SampleReduceFunc) error

SetSampleFunction applies the input SampleReduceFunc to the receiving Waveform struct.

func (*Waveform) SetScale Uses

func (w *Waveform) SetScale(x uint, y uint) error

SetScale applies the input X and Y axis scaling to the receiving Waveform struct.

func (*Waveform) SetScaleClipping Uses

func (w *Waveform) SetScaleClipping() error

SetScaleClipping applies sets the scaleClipping member true for the receiving Waveform struct.

func (*Waveform) SetSharpness Uses

func (w *Waveform) SetSharpness(sharpness uint) error

SetSharpness applies the input sharpness to the receiving Waveform struct.

Package waveform imports 10 packages (graph) and is imported by 1 packages. Updated 2016-07-16. Refresh now. Tools for package owners.