waveform

package module
v0.0.0-...-ad7bcb1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2019 License: MIT Imports: 10 Imported by: 0

README

#waveform

##音频流生成波形图像的 Go package capable of generating waveform images from audio streams. MIT Licensed.

This library supports any audio streams which the azul3d/engine/audio package is able to decode. At the time of writing, this includes:

  • WAV
  • FLAC

An example binary called waveform is provided which show's the library's usage. Please see cmd/waveform/README.md for details.

Examples

Here are several example images generated using waveform. Enjoy!

Generate a waveform image, and scale it both vertically and horizontally.

$ cat ~/Music/02\ -\ Peace\ Of\ Mind.flac | waveform -x 5 -y 2 > ~/waveform.png

waveform

Apply a foreground and background color, to make things more interesting.

cat ~/Music/02\ -\ Peace\ Of\ Mind.flac | waveform -fg=#FF3300 -bg=#0099CC -x 5 -y 2 > ~/waveform_color.png

waveform_color

Apply an alternate foreground color, draw using a stripe pattern.

cat ~/Music/02\ -\ Peace\ Of\ Mind.flac | waveform -fg=#FF3300 -bg=#0099CC -alt=#FF9933 -fn stripe -x 5 -y 2 > ~/waveform_stripe.png

waveform_stripe

Apply an alternate foreground color, draw using a random fuzz pattern.

cat ~/Music/02\ -\ Peace\ Of\ Mind.flac | waveform -fg=#FF3300 -bg=#0099CC -alt=#FF9933 -fn fuzz -x 5 -y 2 > ~/waveform_fuzz.png

waveform_fuzz

Apply a new set of colors, draw using a gradient pattern.

cat ~/Music/02\ -\ Peace\ Of\ Mind.flac | waveform -fg=#FF0000 -bg=#00FF00 -alt=#0000FF -fn gradient -x 5 -y 2 > ~/waveform_gradient.png

waveform_gradient

Apply a checkerboard color set, draw using a checkerboard pattern.

cat ~/Music/02\ -\ Peace\ Of\ Mind.flac | waveform -fg=#000000 -bg=#222222 -alt=#FFFFFF -fn checker -x 5 -y 2 > ~/waveform_checker.png

waveform_checker

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

View Source
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.

Functions

func Generate

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.

Example

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.

// 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

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.

Types

type ColorFunc

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

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

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

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

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

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

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

func (e *OptionsError) Error() string

Error returns the string representation of an OptionsError.

type OptionsFunc

type OptionsFunc func(*Waveform) error

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

func BGColorFunction

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

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

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

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

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

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

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

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

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

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

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

func (*Waveform) Compute

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

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

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

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

func (*Waveform) SetFGColorFunction

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

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

func (*Waveform) SetOptions

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

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

func (*Waveform) SetResolution

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

SetResolution applies the input resolution to the receiving Waveform struct.

func (*Waveform) SetSampleFunction

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

SetSampleFunction applies the input SampleReduceFunc to the receiving Waveform struct.

func (*Waveform) SetScale

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

func (w *Waveform) SetScaleClipping() error

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

func (*Waveform) SetSharpness

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

SetSharpness applies the input sharpness to the receiving Waveform struct.

Directories

Path Synopsis
cmd
waveform
Command waveform is a simple utility which reads an audio file from stdin, processes it into a waveform image using input flags, and writes a PNG image of the generated waveform to stdout.
Command waveform is a simple utility which reads an audio file from stdin, processes it into a waveform image using input flags, and writes a PNG image of the generated waveform to stdout.

Jump to

Keyboard shortcuts

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