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
- Speaker on developer.repebble.com
Union Types
Waveform
type Waveform
= Sine
| Square
| Triangle
| SawtoothSynthesizer waveform for tones and notes.
PcmFormat
type PcmFormat
= Pcm8kHz8bit
| Pcm16kHz8bit
| Pcm8kHz16bit
| Pcm16kHz16bitMono PCM stream format for streamOpen.
Status
type Status
= Idle
| Playing
| DrainingCurrent speaker activity reported by status.
FinishReason
type FinishReason
= FinishedDone
| FinishedStopped
| FinishedPreempted
| FinishedError
| FinishedUnknownWhy 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 msgPlay a single tone.
frequencyHz is the tone frequency. durationMs is capped at 10 seconds on device. volume is 0–100.
playNotes
playNotes : List Note -> Int -> Cmd msgPlay a monophonic note sequence at the given global volume (0–100).
playTracks
playTracks : List Track -> Int -> Cmd msgPlay 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 msgStop any active speaker playback immediately.
setVolume
setVolume : Int -> Cmd msgSet the global speaker volume (0–100), including during playback.
status
status : (Status -> msg) -> Cmd msgPoll the current speaker status.
isMuted
isMuted : (Bool -> msg) -> Cmd msgCheck whether the watch speaker is muted system-wide.
streamOpen
streamOpen : PcmFormat -> Int -> Cmd msgOpen a raw PCM stream at the given format and starting volume.
streamWrite
streamWrite : List Int -> Cmd msgWrite signed PCM bytes (0–255 wire encoding) to the open stream.
streamClose
streamClose : Cmd msgClose the open PCM stream and drain buffered audio.
Subscriptions
onFinished
onFinished : (FinishReason -> msg) -> Sub msgReceive speaker playback completion events.
Register this subscription before starting playback that needs a completion callback.
Values
limits
limits : LimitsAll speaker validation limits in one record.
maxNotes
maxNotes : IntSDK SPEAKER_MAX_NOTES.
maxTracks
maxTracks : IntSDK SPEAKER_MAX_TRACKS.
maxSampleBytesTotal
maxSampleBytesTotal : IntSDK SPEAKER_MAX_SAMPLE_BYTES_TOTAL (16 KiB).