synth

package
v3.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

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: audio.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 Duration

func Duration(t time.Duration) Option

Duration sets the duration of a generated waveform

func Mono

func Mono() Option

Mono sets the format to play mono audio.

func Stereo

func Stereo() Option

Stereo sets the format 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 helper type for synth functions so a user can write A4 instead of a frequency value for a desired tone

const (
	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 Values taken from http://peabody.sapp.org/class/st2/lab/notehz/

func NoteFromIndex

func NoteFromIndex(i int) Pitch

NoteFromIndex is a utility for pitch converters that for some reason have integers representing their notes to get a pitch from said integer

func (Pitch) Down

func (p Pitch) Down(s Step) Pitch

Down lowers a pitch s steps

func (Pitch) IsAccidental added in v3.4.0

func (p Pitch) IsAccidental() bool

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

func (Pitch) Up

func (p Pitch) Up(s Step) Pitch

Up raises a pitch s steps

type Source

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

A Source stores necessary information for generating audio and waveform data

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) Wave

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) PulsePCM added in v3.4.0

func (s Source) PulsePCM(pulse float64) func(opts ...Option) (pcm.Reader, error)

PulsePCM acts like Pulse, but returns a PCM type instead of a klang type.

func (Source) Saw

func (s Source) Saw(opts ...Option) (audio.Audio, error)

Saw produces a saw wave

 ^   ^   ^
/ | / | /

/ |/ |/

func (Source) SawPCM added in v3.4.0

func (s Source) SawPCM(opts ...Option) (pcm.Reader, error)

SawPCM acts like Saw, but returns a PCM type instead of a klang type.

func (Source) Sin

func (s Source) Sin(opts ...Option) (audio.Audio, error)

Sin produces a Sin wave

   __
 --  --
/      \

--__-- --__--

func (Source) SinPCM added in v3.4.0

func (s Source) SinPCM(opts ...Option) (pcm.Reader, error)

SinPCM acts like Sin, but returns a PCM type instead of a klang type.

func (Source) Square

func (s Source) Square(opts ...Option) (audio.Audio, error)

Square produces a Square wave

_________
|       |

______| |________

func (Source) Triangle

func (s Source) Triangle(opts ...Option) (audio.Audio, error)

Triangle produces a Triangle wave

 ^   ^
/ \ / \

v v v

func (Source) TrianglePCM added in v3.4.0

func (s Source) TrianglePCM(opts ...Option) (pcm.Reader, error)

TrianglePCM acts like Triangle, but returns a PCM type instead of a klang type.

func (Source) Update

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

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

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) (audio.Audio, error)

Wave functions take a set of options and return an audio

type Wave16Reader added in v3.4.0

type Wave16Reader struct {
	Source
	// contains filtered or unexported fields
}

func (*Wave16Reader) PCMFormat added in v3.4.0

func (pr *Wave16Reader) PCMFormat() pcm.Format

func (*Wave16Reader) ReadPCM added in v3.4.0

func (pr *Wave16Reader) ReadPCM(b []byte) (n int, err error)

type Wave32Reader added in v3.4.0

type Wave32Reader struct {
	Source
	// contains filtered or unexported fields
}

func (*Wave32Reader) PCMFormat added in v3.4.0

func (pr *Wave32Reader) PCMFormat() pcm.Format

func (*Wave32Reader) ReadPCM added in v3.4.0

func (pr *Wave32Reader) ReadPCM(b []byte) (n int, err error)

Jump to

Keyboard shortcuts

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