Documentation ¶
Overview ¶
Package waveform is capable of generating waveform images from audio streams. MIT Licensed.
Index ¶
- Variables
- func Generate(r io.Reader, options ...OptionsFunc) (image.Image, error)
- func RMSF64Samples(samples audio.Float64) float64
- type ColorFunc
- type OptionsError
- type OptionsFunc
- func BGColorFunction(function ColorFunc) OptionsFunc
- func FGColorFunction(function ColorFunc) OptionsFunc
- func Resolution(resolution uint) OptionsFunc
- func SampleFunction(function SampleReduceFunc) OptionsFunc
- func Scale(x uint, y uint) OptionsFunc
- func ScaleClipping() OptionsFunc
- func Sharpness(sharpness uint) OptionsFunc
- type SampleReduceFunc
- type Waveform
- func (w *Waveform) Compute() ([]float64, error)
- func (w *Waveform) Draw(values []float64) image.Image
- func (w *Waveform) SetBGColorFunction(function ColorFunc) error
- func (w *Waveform) SetFGColorFunction(function ColorFunc) error
- func (w *Waveform) SetOptions(options ...OptionsFunc) error
- func (w *Waveform) SetResolution(resolution uint) error
- func (w *Waveform) SetSampleFunction(function SampleReduceFunc) error
- func (w *Waveform) SetScale(x uint, y uint) error
- func (w *Waveform) SetScaleClipping() error
- func (w *Waveform) SetSharpness(sharpness uint) error
Examples ¶
Constants ¶
This section is empty.
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.
Functions ¶
func Generate ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
SetBGColorFunction applies the input ColorFunc to the receiving Waveform struct for background use.
func (*Waveform) SetFGColorFunction ¶
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 ¶
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 ¶
SetScale applies the input X and Y axis scaling to the receiving Waveform struct.
func (*Waveform) SetScaleClipping ¶
SetScaleClipping applies sets the scaleClipping member true for the receiving Waveform struct.
func (*Waveform) SetSharpness ¶
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. |