signals

package
v0.0.0-...-ed8a0fe Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package signals contains logic to express and synthesize audio signals. * * * Copyright 2020 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AM

type AM struct {
	// Fraction is the fraction of the amplitude coming from the AM.
	Fraction float64
	// Frequency is the frequency of oscillation between low and high amplitude.
	Frequency Hz
}

AM is an amptlitude modulation modulating a signal.

type DB

type DB float64

DB is power expressed on a logarithm scale.

func (DB) Gain

func (d DB) Gain() float64

Gain returns the gain of this Decibel level.

func (DB) Power

func (d DB) Power() Power

Power returns the power of this Decibel level.

type FM

type FM struct {
	// Width is the distance between low and high modulation.
	Width Hz
	// Frequency is the frequency of oscillation between low and high frequency.
	Frequency Hz
}

FM is a frequency modulation modulating a signal.

type Float64Slice

type Float64Slice []float64

Float64Slice represents a sound buffer of floats between -1 and 1.

func (Float64Slice) AddLevel

func (f Float64Slice) AddLevel(d DB)

AddLevel adds a number of Decibel to the signal.

func (Float64Slice) EqTol

func (f Float64Slice) EqTol(o Float64Slice, tol float64) bool

EqTol returns whether the other float slice is equal to this one, within the given tolerance.

func (Float64Slice) PeakSignals

func (f Float64Slice) PeakSignals(rate Hz, ratio float64) Signals

PeakSignals returns a slice of sine signals that describe the center frequencies of peaks in frequency space of this slice where the peak reaches over ratio of the max value across the entire frequency space.

func (Float64Slice) Power

func (f Float64Slice) Power() Power

Power returns the signal power of the slice.

func (Float64Slice) SetDBFS

func (f Float64Slice) SetDBFS(d DB)

SetDBFS set the level of f to be the given DB away from a full scale sine.

func (Float64Slice) SpectrumGains

func (f Float64Slice) SpectrumGains() Float64Slice

SpectrumGains returns a slice with the gain (the complex absolute value) of the first half of the FFT of the slice.

func (Float64Slice) WriteWAV

func (f Float64Slice) WriteWAV(w io.Writer, rate float64) error

WriteWAV writes the samples as a WAV file to a writer, declaring a given sample rate. Assumes the slice contains only values between -1.0 and 1.0.

type Hz

type Hz float64

Hz is cycles per second.

func (Hz) Period

func (h Hz) Period() Seconds

Period returns the period of this frequency.

type Noise

type Noise struct {
	// Onset is the onset of this noise.
	Onset Onset
	// Color is the distribution of this source.
	Color NoiseColor
	// LowerLimit is the lower (inclusive) limit of this source.
	LowerLimit Hz
	// UpperLimit is the upper (exclusive) limit of this source.
	UpperLimit Hz
	// Level is the level of this signal compared to a full scale sine.
	Level DB
	// Seed is the random seed for this source.
	Seed int64
}

Noise describes a band limited noise source.

func (*Noise) Sample

func (n *Noise) Sample(ts TimeStretch, rate Hz) (Float64Slice, error)

Sample samples this noise source at the given rate within the given time slice. NB: Assumes that this duration is all that is sampled, and will generate samples that (for this duration) should be indistinguisable from noise.

func (*Noise) String

func (n *Noise) String() string

type NoiseColor

type NoiseColor int

NoiseColor defines a distribution of noise.

const (
	// White defines a noise that has a average of zero, and where all frequencies within the limits
	// have equal gain.
	White NoiseColor = iota
)

func (NoiseColor) String

func (n NoiseColor) String() string

type NoisyAMSignal

type NoisyAMSignal struct {
	// Onset is the onset of this signal.
	Onset Onset
	// Shape is the shape of this signal.
	Shape SignalShape
	// Frequency is the average frequency of this signal.
	Frequency Hz
	// Level is the level of this signal compared to a full scale sine.
	Level DB
	// AMFrequency is the expected frequency of the amplitude modulation of this signal.
	AMFrequency Hz
	// Seed is the random seed for this source.
	Seed int64
}

NoisyAMSignal is a signal with a phase-noisy amplitude modulator.

func (NoisyAMSignal) Sample

func (n NoisyAMSignal) Sample(ts TimeStretch, rate Hz) (Float64Slice, error)

Sample samples this signal during the provided time stretch, at the provided rate.

func (*NoisyAMSignal) String

func (n *NoisyAMSignal) String() string

type NoisyFMSignal

type NoisyFMSignal struct {
	// Onset is the onset of this signal.
	Onset Onset
	// Shape is the shape of this signal.
	Shape SignalShape
	// Frequency is the average frequency of this signal.
	Frequency Hz
	// Level is the level of this signal compared to a full scale sine.
	Level DB
	// FMFrequency is the expected frequency of the frequency modulation of this signal.
	FMFrequency Hz
	// Seed is the random seed for this source.
	Seed int64
}

NoisyFMSignal is a signal with a phase-noisy frequency modulator.

func (NoisyFMSignal) Sample

func (n NoisyFMSignal) Sample(ts TimeStretch, rate Hz) (Float64Slice, error)

Sample samples this signal during the provided time stretch, at the provided rate.

func (*NoisyFMSignal) String

func (n *NoisyFMSignal) String() string

type Onset

type Onset struct {
	// Shape is the shape of the ramp up.
	Shape OnsetShape
	// Delay is the delay before onset starts.
	Delay Seconds
	// Duration is how long it takes for the signal to reach peak level.
	Duration Seconds
}

Onset defines how a signal starts.

func (Onset) Filter

func (o Onset) Filter(signal Float64Slice, ts TimeStretch) error

Filter filters the signal during the given time stretch with this onset.

type OnsetShape

type OnsetShape int

OnsetShape defines the shape of a signal onset.

const (
	// Sudden is when the signal gets peak level between one sample and the next.
	Sudden OnsetShape = iota
	// Linear is when the signal increases linearly.
	Linear
)

func (OnsetShape) String

func (o OnsetShape) String() string

type Power

type Power float64

Power is the signal power, which is equivalent to the variance ( avg(sum(v^2)) - avg(v)^2 ) of a signal.

const (
	// FullScaleSinePower is 0.5 due to power = avg(sum(v^2)) - avg(v)^2.
	FullScaleSinePower Power = 0.5
)

func (Power) DB

func (p Power) DB() DB

DB returns the power converted to Decibel.

type PowerCalculator

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

PowerCalculator calculates power of signals.

func (*PowerCalculator) Feed

func (p *PowerCalculator) Feed(f float64)

Feed feeds the calculator the next sample.

func (*PowerCalculator) Power

func (p *PowerCalculator) Power() Power

Power returns the power of the signal so far.

type Sampler

type Sampler interface {
	// Sample returns samples during the provided time stretch at the given sample rate.
	Sample(t TimeStretch, rate Hz) (Float64Slice, error)
}

Sampler can synthesize a signal for a given time.

func ParseSampler

func ParseSampler(spec string) (Sampler, error)

ParseSampler parses a spec and returns a Sampler.

type SamplerWrapper

type SamplerWrapper struct {
	Type   string
	Params map[string]interface{}
}

SamplerWrapper encodes a sampler by containing the type of sampler as a string along with the parameters of the underlying sampler type.

func (*SamplerWrapper) Sampler

func (s *SamplerWrapper) Sampler() (Sampler, error)

Sampler returns the sampler wrapped in the SamplerWrapper.

type SamplerWrappers

type SamplerWrappers []SamplerWrapper

SamplerWrappers is a slice of sampler wrappers.

func (SamplerWrappers) Superposition

func (s SamplerWrappers) Superposition() (Superposition, error)

Superposition returns a superposition of the wrapped samplers.

type Seconds

type Seconds float64

Seconds is a point in time.

type Signal

type Signal struct {
	// Onset is the onset of this signal.
	Onset Onset
	// Shape is the shape of this signal.
	Shape SignalShape
	// Frequency is the average frequency of this signal.
	Frequency Hz
	// FM is any frequency modulation applied to the signal.
	FM FM
	// AM is any amplitude modulation applied to the signal.
	AM AM
	// Level is the level of this signal compared to a full scale sine.
	Level DB
}

Signal describes a signal.

func (Signal) EqTol

func (s Signal) EqTol(o Signal, tol float64) bool

EqTol returns true if the signal is equal to the other signal within the given tolerance.

func (Signal) Sample

func (s Signal) Sample(ts TimeStretch, rate Hz) (Float64Slice, error)

Sample samples this signal during the provided time stretch, at the provided rate.

func (*Signal) String

func (s *Signal) String() string

type SignalShape

type SignalShape int

SignalShape defines the known shapes of audio signals.

const (
	// Sine defines a sine wave shape.
	Sine SignalShape = iota
)

func (SignalShape) String

func (s SignalShape) String() string

type Signals

type Signals []Signal

Signals is a slice of signals.

func (Signals) EqTol

func (s Signals) EqTol(o Signals, tol float64) bool

EqTol returns true if the signals are equal to the other signals within the given tolerance.

type Superposition

type Superposition []Sampler

Superposition is a superposition of signals.

func (Superposition) Sample

func (s Superposition) Sample(ts TimeStretch, rate Hz) (Float64Slice, error)

Sample samples the superposition of these signals at time t seconds.

func (Superposition) String

func (s Superposition) String() string

type TimeStretch

type TimeStretch struct {
	// FromInclusive is the start of the stretch of time, inclusive.
	FromInclusive Seconds
	// ToExclusive is the end of the stretch of time, exclusive.
	ToExclusive Seconds
}

TimeStretch defines a stretch of time.

func (TimeStretch) FrequencyDiscrimination

func (t TimeStretch) FrequencyDiscrimination() Hz

FrequencyDiscrimination returns the theoretical minim difference between two frequencies needed for a DCT or FFT to distinguish between them during this time stretch.

func (TimeStretch) Len

func (t TimeStretch) Len() Seconds

Len returns the length of this time stretch.

Jump to

Keyboard shortcuts

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