tone

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: BSD-3-Clause Imports: 1 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Note

type Note uint8

Note represents a MIDI note number. For example, Note(69) is A4 or 440Hz.

const (
	A0 Note = iota + 21 // 27.5Hz
	AS0
	B0
	C1
	CS1
	D1
	DS1
	E1
	F1
	FS1
	G1
	GS1
	A1 // 55Hz
	AS1
	B1
	C2
	CS2
	D2
	DS2
	E2
	F2
	FS2
	G2
	GS2
	A2 // 110Hz
	AS2
	B2
	C3
	CS3
	D3
	DS3
	E3
	F3
	FS3
	G3
	GS3
	A3 // 220Hz
	AS3
	B3
	C4
	CS4
	D4
	DS4
	E4
	F4
	FS4
	G4
	GS4
	A4 // 440Hz
	AS4
	B4
	C5
	CS5
	D5
	DS5
	E5
	F5
	FS5
	G5
	GS5
	A5 // 880Hz
	AS5
	B5
	C6
	CS6
	D6
	DS6
	E6
	F6
	FS6
	G6
	GS6
	A6 // 1760Hz
	AS6
	B6
	C7
	CS7
	D7
	DS7
	E7
	F7
	FS7
	G7
	GS7
	A7 // 3520Hz
	AS7
	B7
	C8
	CS8
	D8
	DS8
	E8
	F8
	FS8
	G8
	GS8
	A8 // 7040Hz
	AS8
	B8
)

Define all the notes in a format similar to the Tone library in the Arduino IDE.

func (Note) Period

func (n Note) Period() uint64

Period returns the period in nanoseconds of a single wave.

type PWM

type PWM interface {
	Configure(config machine.PWMConfig) error
	Channel(pin machine.Pin) (channel uint8, err error)
	Top() uint32
	Set(channel uint8, value uint32)
	SetPeriod(period uint64) error
}

PWM is the interface necessary for controlling a speaker.

type Speaker

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

Speaker is a configured audio output channel based on a PWM.

func New

func New(pwm PWM, pin machine.Pin) (Speaker, error)

New returns a new Speaker instance readily configured for the given PWM and pin combination. The lowest frequency possible is 27.5Hz, or A0. The audio output uses a PWM so the audio will form a square wave, a sound that generally sounds rather harsh.

func (Speaker) SetNote

func (s Speaker) SetNote(note Note)

SetNote starts playing the given note. For example, s.SetNote(C4) will produce a 440Hz square wave tone.

func (Speaker) SetPeriod

func (s Speaker) SetPeriod(period uint64)

SetPeriod sets the period for the signal in nanoseconds. Use the following formula to convert frequency to period:

period = 1e9 / frequency

You can also use s.SetNote() instead for MIDI note numbers.

func (Speaker) Stop

func (s Speaker) Stop()

Stop disables the speaker, setting the output to low continuously.

Jump to

Keyboard shortcuts

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