synth

package
v4.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: Apache-2.0 Imports: 5 Imported by: 5

Documentation

Overview

Package synth provides functions and types to support waveform synthesis.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Int16 is a default source for building 16-bit audio
	Int16 = Source{
		Format: pcm.Format{
			SampleRate: 44100,
			Channels:   2,

			Bits: 16,
		},
		Pitch:   A4,
		Volume:  .25,
		Seconds: 1,
	}
)

Functions

This section is empty.

Types

type Option

type Option func(Source) Source

Option types modify waveform sources before they generate a waveform

func AtPitch

func AtPitch(p Pitch) Option

AtPitch sets the pitch of a generated waveform.

func Detune

func Detune(percent float64) Option

Detune detunes between -1.0 and 1.0, 1.0 representing a half step up. Q: What is detuning? A: It's taking the pitch of the audio and adjusting it less than a single tone up or down. If you detune too far, you've just made the next pitch, but if you detune a little, you get a resonant sound.

func Duration

func Duration(t time.Duration) Option

Duration sets the duration of a generated waveform

func Mono

func Mono() Option

Mono sets a synth source to play mono audio.

func Stereo

func Stereo() Option

Stereo sets a synth source to play stereo audio.

func Volume

func Volume(v float64) Option

Volume sets the volume of a generated waveform. It guarantees that 0 <= v <= 1 (silent <= v <= max volume)

type Pitch

type Pitch uint16

A Pitch is a frequency value which represents how fast a wave should oscillate to produce a specific tone.

const (
	// 0 is reserved as representing a 'rest' for the purpose of composition
	Rest Pitch = 0
	C0   Pitch = 16
	C0s  Pitch = 17
	D0b  Pitch = 17
	D0   Pitch = 18
	D0s  Pitch = 20
	E0b  Pitch = 20
	E0   Pitch = 21
	F0   Pitch = 22
	F0s  Pitch = 23
	G0b  Pitch = 23
	G0   Pitch = 25
	G0s  Pitch = 26
	A0b  Pitch = 26
	A0   Pitch = 28
	A0s  Pitch = 29
	B0b  Pitch = 29
	B0   Pitch = 31
	C1   Pitch = 33
	C1s  Pitch = 35
	D1b  Pitch = 35
	D1   Pitch = 37
	D1s  Pitch = 39
	E1b  Pitch = 39
	E1   Pitch = 41
	F1   Pitch = 44
	F1s  Pitch = 46
	G1b  Pitch = 46
	G1   Pitch = 49
	G1s  Pitch = 52
	A1b  Pitch = 52
	A1   Pitch = 55
	A1s  Pitch = 58
	B1b  Pitch = 58
	B1   Pitch = 62
	C2   Pitch = 65
	C2s  Pitch = 69
	D2b  Pitch = 69
	D2   Pitch = 73
	D2s  Pitch = 78
	E2b  Pitch = 78
	E2   Pitch = 82
	F2   Pitch = 87
	F2s  Pitch = 93
	G2b  Pitch = 93
	G2   Pitch = 98
	G2s  Pitch = 104
	A2b  Pitch = 104
	A2   Pitch = 110
	A2s  Pitch = 117
	B2b  Pitch = 117
	B2   Pitch = 124
	C3   Pitch = 131
	C3s  Pitch = 139
	D3b  Pitch = 139
	D3   Pitch = 147
	D3s  Pitch = 156
	E3b  Pitch = 156
	E3   Pitch = 165
	F3   Pitch = 175
	F3s  Pitch = 185
	G3b  Pitch = 185
	G3   Pitch = 196
	G3s  Pitch = 208
	A3b  Pitch = 208
	A3   Pitch = 220
	A3s  Pitch = 233
	B3b  Pitch = 233
	B3   Pitch = 247
	C4   Pitch = 262
	C4s  Pitch = 278
	D4b  Pitch = 278
	D4   Pitch = 294
	D4s  Pitch = 311
	E4b  Pitch = 311
	E4   Pitch = 330
	F4   Pitch = 349
	F4s  Pitch = 370
	G4b  Pitch = 370
	G4   Pitch = 392
	G4s  Pitch = 415
	A4b  Pitch = 415
	A4   Pitch = 440
	A4s  Pitch = 466
	B4b  Pitch = 466
	B4   Pitch = 494
	C5   Pitch = 523
	C5s  Pitch = 554
	D5b  Pitch = 554
	D5   Pitch = 587
	D5s  Pitch = 622
	E5b  Pitch = 622
	E5   Pitch = 659
	F5   Pitch = 699
	F5s  Pitch = 740
	G5b  Pitch = 740
	G5   Pitch = 784
	G5s  Pitch = 831
	A5b  Pitch = 831
	A5   Pitch = 880
	A5s  Pitch = 932
	B5b  Pitch = 932
	B5   Pitch = 988
	C6   Pitch = 1047
	C6s  Pitch = 1109
	D6b  Pitch = 1109
	D6   Pitch = 1175
	D6s  Pitch = 1245
	E6b  Pitch = 1245
	E6   Pitch = 1319
	F6   Pitch = 1397
	F6s  Pitch = 1475
	G6b  Pitch = 1475
	G6   Pitch = 1568
	G6s  Pitch = 1661
	A6b  Pitch = 1661
	A6   Pitch = 1760
	A6s  Pitch = 1865
	B6b  Pitch = 1865
	B6   Pitch = 1976
	C7   Pitch = 2093
	C7s  Pitch = 2218
	D7b  Pitch = 2218
	D7   Pitch = 2349
	D7s  Pitch = 2489
	E7b  Pitch = 2489
	E7   Pitch = 2637
	F7   Pitch = 2794
	F7s  Pitch = 2960
	G7b  Pitch = 2960
	G7   Pitch = 3136
	G7s  Pitch = 3322
	A7b  Pitch = 3322
	A7   Pitch = 3520
	A7s  Pitch = 3729
	B7b  Pitch = 3729
	B7   Pitch = 3951
	C8   Pitch = 4186
	C8s  Pitch = 4435
	D8b  Pitch = 4435
	D8   Pitch = 4699
	D8s  Pitch = 4978
	E8b  Pitch = 4978
	E8   Pitch = 5274
	F8   Pitch = 5588
	F8s  Pitch = 5920
	G8b  Pitch = 5920
	G8   Pitch = 6272
	G8s  Pitch = 6645
	A8b  Pitch = 6645
	A8   Pitch = 7040
	A8s  Pitch = 7459
	B8b  Pitch = 7459
	B8   Pitch = 7902
)

Pitch frequencies, taken from http://peabody.sapp.org/class/st2/lab/notehz/ These span octave 0 through octave 8, with sharps suffixed 's' and flats suffixed 'b'

func (Pitch) Down

func (p Pitch) Down(s Step) Pitch

Down lowers a pitch s steps

func (Pitch) IsAccidental

func (p Pitch) IsAccidental() bool

IsAccidental reports true if this pitch is represented with a single sharp or a flat, usually.

func (Pitch) Round

func (p Pitch) Round() Pitch

Round rounds a pitch value to the closest predefined pitch value in hertz:

func main() {
	hz := synth.Pitch(1024)
	hz2 := hz.Round()
	fmt.Println(hz2, int(hz2))) // "C6", 1047
}

func (Pitch) String

func (p Pitch) String() string

func (Pitch) Up

func (p Pitch) Up(s Step) Pitch

Up raises a pitch s steps

type PitchDetector

type PitchDetector struct {
	pcm.Reader

	// DetectedPitches and DetectedRawPitches store the calculated pitch values as this reader parses data. The length
	// of these slices will be equal to this reader's format's channel count. Consumers should not modify these slices.
	DetectedPitches    []Pitch
	DetectedRawPitches []float64
	// contains filtered or unexported fields
}

func NewPitchDetector

func NewPitchDetector(r pcm.Reader) *PitchDetector

func (*PitchDetector) ReadPCM

func (pd *PitchDetector) ReadPCM(b []byte) (n int, err error)

type Source

type Source struct {
	pcm.Format
	Pitch Pitch
	// Volume, between 0.0 -> 1.0
	Volume  float64
	Seconds float64
}

A Source stores necessary information for generating waveform data

func (Source) MultiWave

func (s Source) MultiWave(waveFns []Waveform, opts ...Option) pcm.Reader

MultiWave converts a series of waveform functions into a combined reader, outputting the average of all of the source waveforms at any given index

func (Source) Noise

func (s Source) Noise(opts ...Option) pcm.Reader

Noise produces random audio data.

func (Source) NoiseWave

func (s Source) NoiseWave(idx int) float64

NoiseWave returns noise pcm data bounded by this source's volume.

func (Source) Phase

func (s Source) Phase(i int) float64

Phase is shorthand for phase(s.Pitch, i, s.SampleRate). Some sources might have custom phase functions in the future, however.

func (Source) PlayLength

func (s Source) PlayLength() time.Duration

PlayLength returns the time it will take before audio generated from this source will stop.

func (Source) Pulse

func (s Source) Pulse(pulse float64) func(opts ...Option) pcm.Reader

Pulse acts like Square when given a pulse of 2, when given any lesser pulse the time up and down will change so that 1/pulse time the wave will be up.

    __    __
    ||    ||
____||____||____

func (Source) Saw

func (s Source) Saw(opts ...Option) pcm.Reader

Saw produces a saw wave

  ^   ^   ^
 / | / | /
/  |/  |/

func (Source) SawWave

func (s Source) SawWave(idx int) float64

func (Source) Sin

func (s Source) Sin(opts ...Option) pcm.Reader

Sin produces a Sin wave

         __
       --  --
      /      \
--__--        --__--

func (Source) SinWave

func (s Source) SinWave(idx int) float64

func (Source) Square

func (s Source) Square(opts ...Option) pcm.Reader

func (Source) Triangle

func (s Source) Triangle(opts ...Option) pcm.Reader

Triangle produces a Triangle wave

  ^   ^
 / \ / \
v   v   v

func (Source) TriangleWave

func (s Source) TriangleWave(idx int) float64

func (Source) Update

func (s Source) Update(opts ...Option) Source

Update is shorthand for applying a set of options to a source

func (Source) Wave

func (s Source) Wave(waveFn Waveform, opts ...Option) pcm.Reader

Wave converts a waveform function into a pcm.Reader

type Step

type Step int

A Step is an index offset on a pitch to raise or lower it to a relative new pitch

const (
	HalfStep  Step = 1
	WholeStep Step = 2
	Octave    Step = 12
)

Step values

type Wave

type Wave func(opts ...Option) pcm.Reader

Wave functions take a set of options and return an audio

type Waveform

type Waveform func(s Source, idx int) float64

A Waveform is a function that can report a point of audio data given some source parameters for generating the audio and an index of where in the generated waveform the requested point lies

func PulseWave

func PulseWave(pulse float64) Waveform

Jump to

Keyboard shortcuts

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