tsunami

package module
v0.0.0-...-9ae2292 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 4 Imported by: 0

README

go-tsunami GoDoc

Sparkfun Tsunami serial control library for golang

Tsunami is a polyphonic Wav file player with 4 stereo (or 8 mono) outputs. Wav files can be triggered using the 16 onboard contacts, via MIDI, serial connection or Qwiic to a PC or other microcontroller.

This library is a manual transpilation from the official library Tsunami-Arduino-Serial-Library.

Installation

The recommended way to install go-tsunami

go get github.com/mcuadros/go-tsunami

Example

ts, err := tsunami.NewTsunami("/dev/ttyUSB0")
if err != nil {
	panic(err)
}

defer ts.Close()

if err := ts.Start(); err != nil {
	panic(err)
}

trackNum := 1

ts.TrackGain(trackNum, -70)                     // muted
ts.TrackPlaySolo(trackNum, 0, false)            // track = 19 (aka "19.WAV"), output = 0 (aka "1L")
ts.TrackFade(trackNum, 0, time.Second*5, false) // track 19, fade to gain of 0,
// fade time = 5000ms, stopFlag is false = do not stop

fmt.Println("Fading IN track 19 right now...")
time.Sleep(time.Second * 5)

fmt.Println("Gain set to unity (0)! Playing for 5 seconds...")
time.Sleep(time.Second * 5)

ts.TrackFade(trackNum, -70, time.Second*5, true) // track 3, fade to gain of -70,
// fade time = 5000ms, stopFlag is true = stop track when fade is done

fmt.Println("Fading OUT track 19 right now...")
time.Sleep(time.Second * 5)

fmt.Println("Track 19 stopped.")

License

MIT, see LICENSE

Documentation

Overview

Sparkfun Tsunami serial control library for golang

Tsunami is a polyphonic Wav file player with 4 stereo (or 8 mono) outputs. Wav files can be triggered using the 16 onboard contacts, via MIDI, serial connection or Qwiic to a PC or other microcontroller.

Index

Examples

Constants

View Source
const (
	CMD_GET_VERSION       = 1
	CMD_GET_SYS_INFO      = 2
	CMD_TRACK_CONTROL     = 3
	CMD_STOP_ALL          = 4
	CMD_MASTER_VOLUME     = 5
	CMD_TRACK_VOLUME      = 8
	CMD_TRACK_FADE        = 10
	CMD_RESUME_ALL_SYNC   = 11
	CMD_SAMPLERATE_OFFSET = 12
	CMD_SET_REPORTING     = 13
	CMD_SET_TRIGGER_BANK  = 14
	CMD_SET_INPUT_MIX     = 15
	CMD_SET_MIDI_BANK     = 16

	TRK_PLAY_SOLO      = 0
	TRK_PLAY_POLY      = 1
	TRK_PAUSE          = 2
	TRK_RESUME         = 3
	TRK_STOP           = 4
	TRK_LOOP_ON        = 5
	TRK_LOOP_OFF       = 6
	TRK_LOAD           = 7
	RSP_VERSION_STRING = 129
	RSP_SYSTEM_INFO    = 130
	RSP_STATUS         = 131
	RSP_TRACK_REPORT   = 132

	MAX_MESSAGE_LEN    = 32
	MAX_NUM_VOICES     = 18
	VERSION_STRING_LEN = 23

	SOM1 = 0xf0
	SOM2 = 0xaa
	EOM  = 0x55

	IMIX_OUT1 = 0x01
	IMIX_OUT2 = 0x02
	IMIX_OUT3 = 0x04
	IMIX_OUT4 = 0x08
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Tsunami

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

Tsunami serial connection.

Example
package main

import (
	"fmt"
	"time"

	"github.com/mcuadros/go-tsunami"
)

func main() {
	ts, err := tsunami.NewTsunami("/dev/ttyUSB0")
	if err != nil {
		panic(err)
	}

	defer ts.Close()

	if err := ts.Start(); err != nil {
		panic(err)
	}

	trackNum := 1002

	fmt.Println(ts.GetNumTracks())

	ts.TrackGain(trackNum, 70)                      // muted
	ts.TrackPlaySolo(trackNum, 0, false)            // track = 19 (aka "19.WAV"), output = 0 (aka "1L")
	ts.TrackFade(trackNum, 0, time.Second*5, false) // track 19, fade to gain of 0,

	fmt.Println("Track 19 stopped.")
}
Output:
Fading IN track 19 right now...
Gain set to unity (0)! Playing for 5 seconds...
Fading OUT track 19 right now...
Track 19 stopped.

func NewTsunami

func NewTsunami(portName string) (*Tsunami, error)

NewTsunami returns a new Tsuanmi connection to the given port.

func (*Tsunami) Close

func (t *Tsunami) Close() error

Close should be called to close the connection with the port.

func (*Tsunami) GetNumTracks

func (t *Tsunami) GetNumTracks() int

GetNumTracks this function will return the Tsunami version. This function requires bi-directional communication with Tsunami.

func (*Tsunami) GetVersion

func (t *Tsunami) GetVersion() string

GetVersion this function will return the Tsunami version string. This function requires bi-directional communication with Tsunami.

func (*Tsunami) IsTrackPlaying

func (t *Tsunami) IsTrackPlaying(trk int) bool

IsTrackPlaying if reporting has been enabled, this function can be used to determine if a particular track is currently playing.

func (*Tsunami) MasterGain

func (t *Tsunami) MasterGain(out, gain int) error

MasterGain this function immediately sets the gain of the specific stereo output to the specified value. The range for gain is -70 to +4. If audio is playing, you will hear the result immediately. If audio is not playing, the new gain will be used the next time a track is started.

func (*Tsunami) ResumeAllInSync

func (t *Tsunami) ResumeAllInSync() error

ResumeAllInSync this command resumes all paused tracks within the same audio buffer. Any tracks that were loaded using the TrackLoad() function will start and remain sample locked (in sample sync) with one another.

func (*Tsunami) SamplerateOffset

func (t *Tsunami) SamplerateOffset(out, offset int) error

SamplerateOffset this function immediately sets sample-rate offset, or playback speed / pitch, of the specified stereo output. The range for for the offset is -32767 to +32676, giving a speed range of 1/2x to 2x, or a pitch range of down one octave to up one octave. If audio is playing, you will hear the result immediately. If audio is not playing, the new sample-rate offset will be used the next time a track is started.

func (*Tsunami) SetInputMix

func (t *Tsunami) SetInputMix(mix int) error

SetInputMix this function controls the routing of the audio input channels. For bits 1 through 4, a "1" causes the 2 input channels to be mixed into the corresponding output pair. As an example, to route the audio input to output pairs 1, 2 and 4, the syntax is: SetInputMix(IMIX_OUT1 | IMIX_OUT2 | IMIX_OUT4)

The routing is immediate and does no ramping, so to avoid pops, be sure that the input is quiet when switching.

func (*Tsunami) SetMidiBank

func (t *Tsunami) SetMidiBank(bank int) error

SetMidiBank this function sets the MIDI bank. The bank range is 1 - 32. Each bank will offset the MIDI Note number to track assignment by 128. For bank 1, the default, MIDI Note number maps to track 1. For bank 2, MIDI Note number 1 maps to track 129, MIDI Note number 2 to track 130, and so on.

func (*Tsunami) SetReporting

func (t *Tsunami) SetReporting(enable bool) error

SetReporting this function enables or disables track reporting. When enabled, the Tsunami will send a message whenever a track starts or ends, specifying the track number. Provided you call update() periodically, the library will use these messages to maintain status of all tracks, allowing you to query if particular tracks are playing or not.

func (*Tsunami) SetTriggerBank

func (t *Tsunami) SetTriggerBank(bank int) error

SetTriggerBank this function sets the trigger bank. The bank range is 1 - 32. Each bank will offset the normal trigger function track assignment by 16. For bank 1, the default, trigger one maps to track 1. For bank 2, trigger 1 maps to track 17, trigger 2 to track 18, and so on.

func (*Tsunami) Start

func (t *Tsunami) Start() error

Start initialize the serial communications.

func (*Tsunami) StopAllTracks

func (t *Tsunami) StopAllTracks() error

StopAllTracks this commands stops any and all tracks that are currently playing.

func (*Tsunami) TrackFade

func (t *Tsunami) TrackFade(trk, gain int, d time.Duration, stopFlag bool) error

TrackFade this command initiates a hardware volume fade on track number trk if it is currently playing. The track volume will transition smoothly from the current value to the target gain in the specified number of milliseconds. If the stopFlag is non-zero, the track will be stopped at the completion of the fade (for fade-outs.)

func (*Tsunami) TrackGain

func (t *Tsunami) TrackGain(trk, gain int) error

TrackGain this function immediately sets the gain of track trk to the specified value. The range for gain is -70 to +10. A value of 0 (no gain) plays the track at the nominal value in the wav file. This is the default gain for every track until changed. A value of -70 is completely muted. If the track is playing, you will hear the result immediately. If the track is not playing, the gain will be used the next time the track is started. Every track can have its own gain.

Because the effect is immediate, large changes can produce ubrupt results. If you want to fade in or fade out a track, send small changes spaced out at regular intervals. Increment or decrementing by 1 every 20 to 50 msecs produces nice smooth fades. Better yet, use the trackFade() function below.

func (*Tsunami) TrackLoad

func (t *Tsunami) TrackLoad(trk, out int, lock bool) error

TrackLoad this function loads track number trk and pauses it at the beginning of the track. Loading muiltiple tracks and then un-pausing the all with resumeAllInSync() function below allows for starting multiple tracks in sample sync. The track is routed to the specified stereo output. If lock is true, the track will not be subject to Tsunami's voice stealing algorithm.

func (*Tsunami) TrackLoop

func (t *Tsunami) TrackLoop(trk int, enable bool) error

TrackLoop this function enables (true) or disables (false) the loop flag for track trk. This command does not actually start a track, only determines how it behaves once it is playing and reaches the end. If the loop flag is set, that track will loop continuously until it's stopped, in which case it will stop immediately but the loop flag will remain set, or until the loop flag is cleared, in which case it will stop when it reaches the end of the track. This command may be used either before a track is started or while it's playing.

func (*Tsunami) TrackPause

func (t *Tsunami) TrackPause(trk int) error

TrackPause this function pauses track number trk if it's currently playing. If track is not playing, this function does nothing. Keep in mind that a paused track is still using one of the 8 voice slots. A voice allocated to playing a track becomes free only when that sound is stopped or the track reaches the end of the file (and is not looping).

func (*Tsunami) TrackPlayPoly

func (t *Tsunami) TrackPlayPoly(trk, out int, lock bool) error

TrackPlayPoly this function starts track number trk from the beginning, blending it with any other tracks that are currently playing, including potentially another copy of the same track. The track is routed to the specified stereo output. If lock is true, the track will not be subject to Tsunami's voice stealing algorithm.

func (*Tsunami) TrackPlaySolo

func (t *Tsunami) TrackPlaySolo(trk, out int, lock bool) error

TrackPlaySolo this function stops any and all tracks that are currently playing and starts track number trk from the beginning. The track is routed to the specified stereo output. If lock is true, the track will not be subject to Tsunami's voice stealing algorithm.

func (*Tsunami) TrackResume

func (t *Tsunami) TrackResume(trk int) error

TrackResume this function resumes track number trk if it's currently paused. If track number t is not paused, this function does nothing.

func (*Tsunami) TrackStop

func (t *Tsunami) TrackStop(trk int) error

TrackStop this function stops track number trk if it's currently playing. If track t is not playing, this function does nothing. No other tracks are affected.

Jump to

Keyboard shortcuts

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