pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • Examples
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

Pebble C API

  • Pebble C API
  • Moddable API (Alloy)
  • PebbleKit JS
  • PebbleKit iOS
  • PebbleKit Android
  • Foundation
    • Alloy
    • App
    • App Communication
    • App Glance
    • AppMessage
    • AppSync
    • AppWorker
    • DataLogging
    • DataStructures
      • UUID
    • Dictation
    • Dictionary
    • Event Service
      • AccelerometerService
      • AppFocusService
      • BatteryStateService
      • CompassService
      • ConnectionService
      • HealthService
      • TickTimerService
      • TouchService
    • Exit Reason
    • Internationalization
    • Launch Reason
    • Logging
    • Math
    • Memory Management
    • Platform
    • Resources
      • File Formats
    • Storage
    • Timer
    • Wakeup
    • Wall Time
    • WatchInfo
  • Graphics
    • Draw Commands
    • Drawing Paths
    • Drawing Primitives
    • Drawing Text
    • Fonts
    • Graphics Context
    • Graphics Types
      • Color Definitions
  • User Interface
    • Animation
      • PropertyAnimation
    • Clicks
    • Layers
      • ActionBarLayer
      • BitmapLayer
      • MenuLayer
      • RotBitmapLayer
      • ScrollLayer
      • SimpleMenuLayer
      • StatusBarLayer
      • TextLayer
    • Light
    • Preferences
    • Speaker
    • UnobstructedArea
    • Vibes
    • Window
      • ActionMenu
      • NumberWindow
    • Window Stack
  • Standard C
    • Format
    • Locale
    • Math
    • Memory
    • String
    • Time

The APIs on this page will only work with SDK 4.9+.

Speaker

Controlling the speaker

The Speaker API provides calls that let you play sounds through the watch's speaker. You can play simple note sequences (melodies), single tones, or stream raw PCM audio.

Note sequences are compact representations of melodies using MIDI-like note definitions, supporting 4 basic waveforms: sine, square, triangle, and sawtooth.

Raw PCM streaming allows apps to generate arbitrary audio in configurable formats.

Function Documentation

bool speaker_play_notes(const SpeakerNote * notes, uint32_t num_notes, uint8_t volume)

Play a sequence of notes on the speaker.

Parameters

notes

Array of SpeakerNote structs defining the melody

num_notes

Number of notes in the array

volume

Playback volume (0-100)

Returns

true if playback started successfully

bool speaker_play_tracks(const SpeakerTrack * tracks, uint32_t num_tracks, uint8_t volume)

Play N monophonic tracks in parallel, mixed (polyphony).

Parameters

tracks

Array of track descriptors (notes + optional sample).

num_tracks

Number of tracks. Must be >= 1 and <= 4.

volume

Playback volume (0-100).

Returns

true if playback started successfully.

bool speaker_play_tone(uint16_t frequency_hz, uint32_t duration_ms, uint8_t volume, SpeakerWaveform waveform)

Play a single tone on the speaker (convenience wrapper).

Parameters

frequency_hz

Tone frequency in Hz

duration_ms

Tone duration in milliseconds (max 10000)

volume

Playback volume (0-100)

waveform

Waveform to use

Returns

true if playback started successfully

bool speaker_stream_open(SpeakerPcmFormat format, uint8_t volume)

Open a raw PCM stream for app-generated audio.

Parameters

format

PCM format specifying sample rate and bit depth

volume

Playback volume (0-100)

Returns

true if stream opened successfully

uint32_t speaker_stream_write(const void * data, uint32_t num_bytes)

Write PCM data to the open stream.

Parameters

data

Buffer of PCM data in the format specified at open

num_bytes

Number of bytes to write

Returns

Number of bytes actually written (may be less if buffer is full)

void speaker_stream_close(void)

Close the PCM stream. Buffered data will be played before stopping.

void speaker_stop(void)

Stop any active speaker playback immediately.

void speaker_set_volume(uint8_t volume)

Set the speaker volume.

Parameters

volume

Volume level (0-100)

SpeakerStatus speaker_get_status(void)

Get the current speaker status.

Returns

Current SpeakerStatus

void speaker_set_finish_callback(SpeakerFinishedCallback cb, void * ctx)

Register a callback invoked when speaker playback ends. The callback runs on the app task.

Parameters

cb

Callback to invoke, or NULL to unregister.

ctx

User context passed back to cb.

Data Structure Documentation

struct SpeakerNote

A single note in a sequence. midi_note: MIDI note number (0-127, 60=C4). 0 = rest (silence). waveform: SpeakerWaveform value. duration_ms: Note duration in ms (max 10000). velocity: Volume 0-127 (0 = use global volume).

Data Fields

uint8_t midi_note
uint8_t waveform
uint16_t duration_ms
uint8_t velocity
uint8_t reserved
struct SpeakerSample

A raw PCM sample that can be pitch-shifted when played by a track. data: mono signed PCM in the given format. num_bytes: size of data in bytes. format: sample rate + bit depth (see SpeakerPcmFormat). base_midi_note: the MIDI note at which the sample plays unshifted (e.g. 60 = C4). Notes above/below this value are produced by resampling. loop: if true, the sample restarts from the beginning each time it runs out, and keeps playing until the owning note's duration elapses.

Data Fields

const void * data
uint32_t num_bytes
SpeakerPcmFormat format
uint8_t base_midi_note
bool loop
struct SpeakerTrack

A single monophonic voice. Multiple tracks are mixed together by speaker_play_tracks() to produce polyphony. notes: array of notes to play sequentially. num_notes: length of the notes array. sample: if non-NULL, notes are played by pitch-shifting this sample; note.waveform is ignored. If NULL, notes use their waveform field.

Data Fields

const SpeakerNote * notes
uint32_t num_notes
const SpeakerSample * sample

Enum Documentation

enum SpeakerWaveform

Enumerators

SpeakerWaveformSine
SpeakerWaveformSquare
SpeakerWaveformTriangle
SpeakerWaveformSawtooth
SpeakerWaveformCount
enum SpeakerPcmFormat

PCM audio format for speaker streaming. Bit layout: bit0 = sample rate (0=8kHz, 1=16kHz), bit1 = bit depth (0=8-bit, 1=16-bit). All formats are mono signed PCM (8-bit samples are signed [-128,127], not unsigned).

Enumerators

SpeakerPcmFormat_8kHz_8bit

8kHz 8-bit signed (1 byte/sample)

SpeakerPcmFormat_16kHz_8bit

16kHz 8-bit signed (1 byte/sample)

SpeakerPcmFormat_8kHz_16bit

8kHz 16-bit signed little-endian (2 bytes/sample)

SpeakerPcmFormat_16kHz_16bit

16kHz 16-bit signed little-endian (2 bytes/sample)

SpeakerPcmFormatCount
enum SpeakerStatus

Speaker status.

Enumerators

SpeakerStatusIdle
SpeakerStatusPlaying
SpeakerStatusDraining
enum SpeakerFinishReason

Reason reported when speaker playback ends.

Enumerators

SpeakerFinishReasonDone

Playback completed naturally.

SpeakerFinishReasonStopped

Playback was stopped by the app.

SpeakerFinishReasonPreempted

Preempted by higher priority source.

SpeakerFinishReasonError

An error occurred.

Typedef Documentation

typedef void(* SpeakerFinishedCallback)(SpeakerFinishReason reason, void *ctx)

Callback invoked when playback finishes.

Parameters

reason

Why playback ended

ctx

User context

Need some help?

Functions

  • speaker_play_notes
  • speaker_play_tracks
  • speaker_play_tone
  • speaker_stream_open
  • speaker_stream_write
  • speaker_stream_close
  • speaker_stop
  • speaker_set_volume
  • speaker_get_status
  • speaker_set_finish_callback

Data Structures

  • SpeakerNote
  • SpeakerSample
  • SpeakerTrack

Enums

  • SpeakerWaveform
  • SpeakerPcmFormat
  • SpeakerStatus
  • SpeakerFinishReason

Typedefs

  • SpeakerFinishedCallback

Getting Help

Do you have questions about the Pebble SDK?

Do you need some help understanding something on this page?

You can either take advantage of our awesome developer community and check out the SDK Help forums, or you can join us on the Discord!