synth

package module
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: MIT Imports: 11 Imported by: 1

README

synth CI Go Report Card

Generate audio samples.

  • This package is a bit experimental and a work in progress.
  • Includes a kick utility for generating kick drum samples, a snare utility for generating snare drum samples, rms and linear for mixing audio and sweep for generating a detuned synth samples. They are all in the cmd directory.
  • Used by the Kickpad application.
  • Uses SDL2 to play sounds (unless -tags ff is given, then ffplay will be used instead).

General info

  • License: MIT
  • Version: 1.14.0

Documentation

Index

Constants

View Source
const (
	Kick = iota
	Clap
	Snare
	ClosedHH
	OpenHH
	Rimshot
	Tom
	Percussion
	Ride
	Crash
	Bass
	Xylophone
	Lead
)
View Source
const (
	WaveSine = iota
	WaveTriangle
	WaveSawtooth
	WaveSquare
	WaveWhiteNoise
	WavePinkNoise
	WaveBrownNoise
)

Constants for waveform types

Variables

This section is empty.

Functions

func AnalyzeHighestFrequency

func AnalyzeHighestFrequency(samples []float64, sampleRate int) float64

AnalyzeHighestFrequency estimates the highest frequency in the audio signal

func ApplyAddPartials added in v1.9.0

func ApplyAddPartials(duration, amplitude, frequency float64, partials, ampEnv []float64, sampleRate int) []float64

ApplyAddPartials adds harmonic partials to the samples using the audioeffects package. partials should contain frequency and amplitude pairs.

func ApplyBitcrusher added in v1.9.0

func ApplyBitcrusher(samples []float64, bitDepth, sampleRateReduction int) []float64

ApplyBitcrusher applies a bitcrusher effect to the samples using the audioeffects package. bitDepth controls the number of bits used in the reduction. sampleRateReduction reduces the sample rate by the specified factor.

func ApplyChorus added in v1.2.0

func ApplyChorus(samples []float64, sampleRate int, delaySec, depth, rate, mix float64) []float64

ApplyChorus applies a chorus effect to the samples using the audioeffects package. delaySec specifies the base delay time in seconds. depth controls the modulation depth of the delay time. rate is the modulation rate in Hz. mix determines the blend between dry and wet signals (0.0 = dry only, 1.0 = wet only).

func ApplyCompressor added in v1.9.0

func ApplyCompressor(samples []float64, threshold, ratio, attack, release float64, sampleRate int) []float64

ApplyCompressor applies dynamic range compression to the samples using the audioeffects package. threshold sets the level above which compression occurs. ratio determines the amount of compression applied. attack and release control the compressor's responsiveness.

func ApplyEnvelope

func ApplyEnvelope(samples []float64, attack, decay, sustain, release float64, sampleRate int) []float64

ApplyEnvelope applies an ADSR envelope to the waveform using the audioeffects package.

func ApplyFMSynthesis added in v1.9.0

func ApplyFMSynthesis(duration, carrierFreq, modFreq, modIndex, amplitude float64, ampEnv []float64, sampleRate int) []float64

ApplyFM_Synthesis generates a frequency-modulated synthesis signal using the audioeffects package.

func ApplyFadeIn added in v1.2.0

func ApplyFadeIn(samples []float64, fadeDuration float64, sampleRate int) []float64

ApplyFadeIn applies a fade-in to the start of the samples using the audioeffects package.

func ApplyFadeOut added in v1.2.0

func ApplyFadeOut(samples []float64, fadeDuration float64, sampleRate int) []float64

ApplyFadeOut applies a fade-out to the end of the samples using the audioeffects package.

func ApplyFlanger added in v1.9.0

func ApplyFlanger(samples []float64, sampleRate int, baseDelay, modDepth, modRate, feedback, mix float64) []float64

ApplyFlanger applies a flanger effect to the samples using the audioeffects package. baseDelay is the base delay time in seconds. modDepth controls the modulation depth of the delay time. modRate is the modulation rate in Hz. feedback controls the amount of delayed signal fed back into the delay line. mix determines the blend between dry and wet signals.

func ApplyFrequencyModulation added in v1.1.0

func ApplyFrequencyModulation(samples []float64, carrierFreq, modDepth float64, sampleRate int) []float64

ApplyFrequencyModulation applies frequency modulation to a waveform using the audioeffects package. carrierFreq is the base frequency of the carrier wave. modDepth controls the extent of frequency deviation.

func ApplyGranularSynthesis added in v1.9.0

func ApplyGranularSynthesis(samples []float64, grainSize, overlap, sampleRate int) []float64

ApplyGranularSynthesis applies granular synthesis to the samples using the audioeffects package. grainSize specifies the size of each grain in samples. overlap determines the overlap between consecutive grains.

func ApplyKarplusStrong added in v1.9.0

func ApplyKarplusStrong(duration, amplitude float64, p int, b float64, sampleRate int) []float64

ApplyKarplusStrong applies the Karplus-Strong algorithm to generate plucked string sounds using the audioeffects package. p is the delay line length in samples. b controls the damping factor.

func ApplyLimiter added in v1.9.0

func ApplyLimiter(samples []float64) []float64

ApplyLimiter applies a limiter to the samples using the audioeffects package. It ensures that the sample amplitudes do not exceed 1.0 or -1.0.

func ApplyMultibandCompression added in v1.9.0

func ApplyMultibandCompression(samples []float64, bands []struct {
	Low  float64
	High float64
}, compressors []struct {
	Threshold float64
	Ratio     float64
	Attack    float64
	Release   float64
}, sampleRate int) []float64

ApplyMultibandCompression applies multiband compression to the samples using the audioeffects package. bands defines the frequency ranges for each band. compressors defines the compression settings for each band.

func ApplyNoiseGate added in v1.9.0

func ApplyNoiseGate(samples []float64, threshold, attack, release float64, sampleRate int) []float64

ApplyNoiseGate applies a noise gate to the samples using the audioeffects package. threshold sets the level below which the signal is attenuated. attack and release control the gate's responsiveness.

func ApplyNormalize added in v1.9.0

func ApplyNormalize(samples []float64, targetPeak float64) []float64

ApplyNormalize scales the samples to ensure the peak amplitude matches the target peak using the audioeffects package.

func ApplyPanning added in v1.1.0

func ApplyPanning(samples []float64, pan float64) ([]float64, []float64)

ApplyPanning applies stereo panning to the samples using the audioeffects package. Pan should be in the range [-1, 1], where -1 is full left and 1 is full right.

func ApplyPhaser added in v1.9.0

func ApplyPhaser(samples []float64, sampleRate int, rate, depth, feedback float64) []float64

ApplyPhaser applies a phaser effect to the samples using the audioeffects package. rate is the modulation frequency in Hz. depth controls the modulation depth. feedback determines the amount of phase-shifted signal fed back into the phaser.

func ApplyPitchModulation added in v1.1.0

func ApplyPitchModulation(samples []float64, modFreq, modDepth float64, sampleRate int) []float64

ApplyPitchModulation applies pitch modulation (vibrato) to the samples using the audioeffects package.

func ApplyQuadraticFadeIn added in v1.9.0

func ApplyQuadraticFadeIn(samples []float64, fadeDuration float64, sampleRate int) []float64

ApplyQuadraticFadeIn applies a fade-in to the start of the samples using the audioeffects package.

func ApplyQuadraticFadeOut added in v1.9.0

func ApplyQuadraticFadeOut(samples []float64, fadeDuration float64, sampleRate int) []float64

ApplyQuadraticFadeOut applies a fade-out to the end of the samples using the audioeffects package.

func ApplyReverb added in v1.9.0

func ApplyReverb(samples []float64, sampleRate int, delayTimes, decays []float64, mix float64) []float64

ApplyReverb applies a reverb effect to the samples using the audioeffects package. delayTimes and decays should be of the same length, specifying multiple delay and decay pairs. mix determines the blend between dry and wet signals.

func ApplySidechainCompressor added in v1.9.0

func ApplySidechainCompressor(target, trigger []float64, threshold, ratio, attack, release float64, sampleRate int) []float64

ApplySidechainCompressor applies a sidechain compressor to the target samples using the trigger samples. threshold sets the compression threshold. ratio determines the compression ratio. attack and release control the compressor's responsiveness.

func ApplySoftClipping added in v1.9.0

func ApplySoftClipping(samples []float64, drive float64) []float64

ApplySoftClipping applies soft clipping distortion to the samples using the audioeffects package.

func ApplyStereoDelay added in v1.9.0

func ApplyStereoDelay(left, right []float64, sampleRate int, delayTimeLeft, delayTimeRight, feedback, mix float64) ([]float64, []float64)

ApplyStereoDelay applies a stereo delay effect to the left and right channels using the audioeffects package. delayTimeLeft and delayTimeRight specify delay times for each channel in seconds. feedback controls the amount of delayed signal fed back into the delay line. mix determines the blend between dry and wet signals.

func ApplySubtractOp added in v1.9.0

func ApplySubtractOp(duration, amplitude, b1 float64, ampEnv []float64, sampleRate int) []float64

ApplySubtractOp subtracts noise from the samples using the audioeffects package. duration specifies the noise duration in seconds. amplitude sets the noise amplitude. b1 is a scaling factor for the noise. ampEnv defines the amplitude envelope.

func ApplyTremolo added in v1.9.0

func ApplyTremolo(samples []float64, sampleRate int, rate, depth float64) []float64

ApplyTremolo applies a tremolo effect to the samples using the audioeffects package. rate is the modulation frequency in Hz. depth controls the amplitude modulation depth.

func BandPassFilter added in v1.1.0

func BandPassFilter(samples []float64, lowCutoff, highCutoff float64, sampleRate int) []float64

BandPassFilter applies a band-pass filter to the samples

func DetunedOscillators

func DetunedOscillators(freq float64, detune []float64, length int, sampleRate int) []float64

DetunedOscillators generates multiple detuned sawtooth oscillators and combines them

func Drive

func Drive(samples []float64, gain float64) []float64

Drive applies a simple drive effect by scaling and clipping

func ExponentialFade added in v1.2.0

func ExponentialFade(t float64) float64

ExponentialFade is an exponential fade curve

func FindPeakAmplitude

func FindPeakAmplitude(samples []float64) float64

FindPeakAmplitude returns the maximum absolute amplitude in the sample set

func GenerateBrownNoise added in v1.7.0

func GenerateBrownNoise(length int, amount float64) []float64

GenerateBrownNoise generates brown noise

func GeneratePinkNoise added in v1.7.0

func GeneratePinkNoise(length int, amount float64) []float64

GeneratePinkNoise generates pink noise

func GenerateWhiteNoise added in v1.7.0

func GenerateWhiteNoise(length int, amount float64) []float64

GenerateWhiteNoise generates white noise

func HighPassFilter added in v1.1.0

func HighPassFilter(samples []float64, cutoff float64, sampleRate int) []float64

HighPassFilter applies a basic high-pass filter to the samples

func Limiter

func Limiter(samples []float64) []float64

Limiter ensures the signal doesn't exceed [-1, 1] range

func LinearFade added in v1.2.0

func LinearFade(t float64) float64

LinearFade is a linear fade curve

func LinearSummation

func LinearSummation(samples ...[]float64) ([]float64, error)

LinearSummation mixes multiple audio samples by averaging them together. It automatically clamps the sum to avoid overflow and distortion.

func LogarithmicFade added in v1.2.0

func LogarithmicFade(t float64) float64

LogarithmicFade is a logarithmic fade curve

func LowPassFilter

func LowPassFilter(samples []float64, cutoff float64, sampleRate int) []float64

LowPassFilter applies a basic low-pass filter to the samples

func NormalizeSamples

func NormalizeSamples(samples []float64, targetPeak float64) []float64

NormalizeSamples scales the samples so the peak amplitude matches the given max amplitude

func PadSamples

func PadSamples(wave1, wave2 []float64) ([]float64, []float64)

PadSamples pads the shorter waveform with zeros to make both waveforms the same length.

func QuadraticFade added in v1.2.0

func QuadraticFade(t float64) float64

QuadraticFade is a quadratic (ease-in) fade curve

func RMSMixing

func RMSMixing(samples ...[]float64) ([]float64, error)

RMSMixing mixes audio samples using the Root Mean Square method.

func Resample added in v1.11.1

func Resample(waveform []float64, originalSampleRate, targetSampleRate int) []float64

func SawtoothOscillator

func SawtoothOscillator(freq float64, length int, sampleRate int) []float64

SawtoothOscillator generates a sawtooth waveform at a specific frequency

func SchroederReverb added in v1.1.0

func SchroederReverb(samples []float64, decayFactor float64, combDelays []int, allPassDelays []int) ([]float64, error)

SchroederReverb applies a high-quality reverb effect using the Schroeder algorithm

func SineFade added in v1.2.0

func SineFade(t float64) float64

SineFade is a sinusoidal fade curve

func WeightedSummation

func WeightedSummation(weights []float64, samples ...[]float64) ([]float64, error)

WeightedSummation mixes multiple audio samples by applying a weight to each sample. Each sample's amplitude is scaled by its corresponding weight before summing.

Types

type FadeCurve added in v1.2.0

type FadeCurve func(t float64) float64

FadeCurve defines a type for fade curve functions

type Settings

type Settings struct {
	SoundType                  SoundType
	SampleRate                 int
	BitDepth                   int
	Channels                   int
	Output                     io.WriteSeeker
	StartFreq                  float64
	EndFreq                    float64
	Duration                   float64
	WaveformType               int
	NoiseAmount                float64
	Attack                     float64
	Decay                      float64
	Sustain                    float64
	Release                    float64
	Drive                      float64
	FilterCutoff               float64
	FilterResonance            float64
	Sweep                      float64
	PitchDecay                 float64
	NumOscillators             int
	OscillatorLevels           []float64
	SaturatorAmount            float64
	FilterBands                []float64
	FadeDuration               float64
	SmoothFrequencyTransitions bool
	AttackCurve                FadeCurve
	DecayCurve                 FadeCurve
	ReleaseCurve               FadeCurve
	ReverbAmount               float64
	ReverbDecay                float64
	DelayAmount                float64
	DelayTime                  float64
	DelayFeedback              float64
}

Settings holds the configuration for generating a sound

func CopySettings

func CopySettings(cfg *Settings) *Settings

CopySettings creates a deep copy of a Settings struct

func New606

func New606(soundType SoundType, output io.WriteSeeker, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

New606 creates sounds similar to the Roland TR-606

func New707

func New707(soundType SoundType, output io.WriteSeeker, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

New707 creates sounds similar to the Roland TR-707

func New808

func New808(soundType SoundType, output io.WriteSeeker, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

New808 creates sounds similar to the Roland TR-808

func New909

func New909(soundType SoundType, output io.WriteSeeker, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

New909 creates sounds similar to the Roland TR-909

func NewClapSettings added in v1.13.0

func NewClapSettings(output io.WriteSeeker, sampleRate, bitDepth, channels int) (*Settings, error)

NewClapSettings creates a new Settings instance with predefined values optimized for clap sounds.

func NewDeepHouse

func NewDeepHouse(soundType SoundType, output io.WriteSeeker, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

NewDeepHouse creates Deep House style sounds

func NewExperimental

func NewExperimental(soundType SoundType, output io.WriteSeeker, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

NewExperimental creates experimental sounds with unusual parameters

func NewLinn added in v1.13.0

func NewLinn(soundType SoundType, output io.WriteSeeker, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

NewLinn creates sounds similar to the LinnDrum

func NewRandom

func NewRandom(soundType SoundType, output io.WriteSeeker, sampleRate, bitDepth, channels int) *Settings

NewRandom generates random settings for the given sound type. It handles each SoundType by initializing a Settings instance with randomized parameters suitable for that sound.

func NewSettings

func NewSettings(output io.WriteSeeker, startFreq, endFreq, duration float64, sampleRate, bitDepth, channels int) (*Settings, error)

NewSettings creates a new Settings instance with default values for a percussive sound

func NewSnareSettings added in v1.10.0

func NewSnareSettings(output io.WriteSeeker, sampleRate, bitDepth, channels int) (*Settings, error)

NewSnareSettings creates a new Settings instance with predefined values optimized for snare drum sounds.

func (*Settings) ApplyDrive

func (cfg *Settings) ApplyDrive(sample float64) float64

ApplyDrive applies a drive (distortion) effect to a single sample using the audioeffects package.

func (*Settings) ApplyEnvelopeAtTime added in v1.2.0

func (cfg *Settings) ApplyEnvelopeAtTime(t float64) float64

ApplyEnvelopeAtTime generates the ADSR envelope value at a specific normalized time. This function retains the custom implementation as audioeffects does not expose envelope evaluation at a specific time.

func (*Settings) Color

func (cfg *Settings) Color() color.RGBA

Color returns a color that represents the current kick config

func (*Settings) Generate added in v1.5.0

func (cfg *Settings) Generate() ([]float64, error)

Generate is a wrapper function that calls the appropriate Generate* function based on the given sound type

func (*Settings) GenerateAndSaveTo added in v1.5.0

func (cfg *Settings) GenerateAndSaveTo(directory string) (string, error)

GenerateAndSaveTo generates samples for a given type (e.g., "kick", "snare") and saves it to a specified directory, avoiding filename collisions.

func (*Settings) GenerateBass added in v1.5.0

func (cfg *Settings) GenerateBass() ([]float64, error)

GenerateBass generates a deep, detuned bass sound typical of deep house

func (*Settings) GenerateClap added in v1.5.0

func (cfg *Settings) GenerateClap() ([]float64, error)

GenerateClap generates a clap sound by combining filtered noise bursts

func (*Settings) GenerateClosedHH added in v1.5.0

func (cfg *Settings) GenerateClosedHH() ([]float64, error)

GenerateClosedHH generates a closed hi-hat sound using filtered noise

func (*Settings) GenerateCrash added in v1.5.0

func (cfg *Settings) GenerateCrash() ([]float64, error)

GenerateCrash generates a crash cymbal sound using filtered noise

func (*Settings) GenerateKick

func (cfg *Settings) GenerateKick() ([]float64, error)

GenerateKick generates the kick waveform and returns it as a slice of float64 samples (without writing to disk).

func (*Settings) GenerateLead added in v1.5.0

func (cfg *Settings) GenerateLead() ([]float64, error)

GenerateLead generates a bright, detuned lead sound

func (*Settings) GenerateOpenHH added in v1.5.0

func (cfg *Settings) GenerateOpenHH() ([]float64, error)

GenerateOpenHH generates an open hi-hat sound using filtered noise

func (*Settings) GeneratePercussion added in v1.5.0

func (cfg *Settings) GeneratePercussion() ([]float64, error)

GeneratePercussion generates a tonal percussion sound like bongo or conga

func (*Settings) GenerateRide added in v1.5.0

func (cfg *Settings) GenerateRide() ([]float64, error)

GenerateRide generates a ride cymbal sound using filtered noise

func (*Settings) GenerateRimshot added in v1.5.0

func (cfg *Settings) GenerateRimshot() ([]float64, error)

GenerateRimshot generates a rimshot sound by using a short burst of high-frequency noise

func (*Settings) GenerateSnare added in v1.5.0

func (cfg *Settings) GenerateSnare() ([]float64, error)

GenerateSnare generates a snare drum sound by combining noise and a tonal component

func (*Settings) GenerateSweepWaveform added in v1.2.0

func (cfg *Settings) GenerateSweepWaveform() ([]float64, error)

GenerateSweepWaveform generates a frequency sweep waveform based on the settings.

func (*Settings) GenerateTom added in v1.5.0

func (cfg *Settings) GenerateTom() ([]float64, error)

GenerateTom generates a tom drum sound, configurable for low, mid, and high toms

func (*Settings) GenerateXylophone added in v1.5.0

func (cfg *Settings) GenerateXylophone() ([]float64, error)

GenerateXylophone generates a xylophone-like sound for arpeggios

type SoundType added in v1.12.0

type SoundType int

func (SoundType) String added in v1.12.0

func (soundType SoundType) String() string

Directories

Path Synopsis
cmd
clap command
kick command
linear command
rms command
snare command
sweep command

Jump to

Keyboard shortcuts

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