Pebble.Speaker

Speaker playback for Pebble watches with PBL_SPEAKER.

Use limits to validate note and track counts before calling playNotes or playTracks. Subscribe with onFinished before starting playback when you need completion callbacks.

import Pebble.Speaker as Speaker

type Msg
    = PlayMelody
    | SpeakerFinished Speaker.FinishReason

melody : List Speaker.Note
melody =
    [ { midiNote = 60, waveform = Speaker.Sine, durationMs = 120, velocity = 100 }
    , { midiNote = 64, waveform = Speaker.Sine, durationMs = 120, velocity = 100 }
    , { midiNote = 67, waveform = Speaker.Sine, durationMs = 200, velocity = 100 }
    ]

update msg model =
    case msg of
        PlayMelody ->
            ( model, Speaker.playNotes melody 80 )

        SpeakerFinished Speaker.FinishedDone ->
            ( { model | playing = False }, Cmd.none )

        _ ->
            ( model, Cmd.none )

subscriptions _ =
    Speaker.onFinished SpeakerFinished

For PCM-backed tracks, set sample = Just … on each Track using values from Pebble.Speaker.Resources. See the watch-demo-speaker project template.

Native Pebble C API

Union Types

Waveform

type Waveform
    = Sine
    | Square
    | Triangle
    | Sawtooth

Synthesizer waveform for tones and notes.

PcmFormat

type PcmFormat
    = Pcm8kHz8bit
    | Pcm16kHz8bit
    | Pcm8kHz16bit
    | Pcm16kHz16bit

Mono PCM stream format for streamOpen.

Status

type Status
    = Idle
    | Playing
    | Draining

Current speaker activity reported by status.

FinishReason

type FinishReason
    = FinishedDone
    | FinishedStopped
    | FinishedPreempted
    | FinishedError
    | FinishedUnknown

Why speaker playback ended.

Type Aliases

Note

type alias Note =
    { midiNote : Int
    , waveform : Waveform
    , durationMs : Int
    , velocity : Int
    }

A single note in a monophonic sequence.

Track

type alias Track =
    { notes : List Note
    , sample : Maybe Sample
    }

A monophonic voice for playTracks.

Set sample to Just a value from Pebble.Speaker.Resources for PCM-backed playback.

Limits

type alias Limits =
    { maxNotes : Int
    , maxTracks : Int
    , maxSampleBytesTotal : Int
    }

Speaker playback limits exposed by the Pebble SDK.

Commands

playTone

playTone : Int -> Int -> Int -> Waveform -> Cmd msg

Play a single tone.

frequencyHz is the tone frequency. durationMs is capped at 10 seconds on device. volume is 0100.

playNotes

playNotes : List Note -> Int -> Cmd msg

Play a monophonic note sequence at the given global volume (0100).

playTracks

playTracks : List Track -> Int -> Cmd msg

Play up to four monophonic tracks mixed together.

Each track is a sequential note list. Use sample = Just … from Pebble.Speaker.Resources for PCM-backed tracks; leave sample = Nothing for synthesized waveforms.

stop

stop : Cmd msg

Stop any active speaker playback immediately.

setVolume

setVolume : Int -> Cmd msg

Set the global speaker volume (0100), including during playback.

status

status : (Status -> msg) -> Cmd msg

Poll the current speaker status.

isMuted

isMuted : (Bool -> msg) -> Cmd msg

Check whether the watch speaker is muted system-wide.

streamOpen

streamOpen : PcmFormat -> Int -> Cmd msg

Open a raw PCM stream at the given format and starting volume.

streamWrite

streamWrite : List Int -> Cmd msg

Write signed PCM bytes (0255 wire encoding) to the open stream.

streamClose

streamClose : Cmd msg

Close the open PCM stream and drain buffered audio.

Subscriptions

onFinished

onFinished : (FinishReason -> msg) -> Sub msg

Receive speaker playback completion events.

Register this subscription before starting playback that needs a completion callback.

Values

limits

limits : Limits

All speaker validation limits in one record.

maxNotes

maxNotes : Int

SDK SPEAKER_MAX_NOTES.

maxTracks

maxTracks : Int

SDK SPEAKER_MAX_TRACKS.

maxSampleBytesTotal

maxSampleBytesTotal : Int

SDK SPEAKER_MAX_SAMPLE_BYTES_TOTAL (16 KiB).