---
title: "Speaker"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/speaker/
last_updated: 2026-11-17
sdk_version: 4.44
---
# Speaker

Source: https://developer.repebble.com/pebbleos/docs/samples/drivers/speaker/

This sample plays a short melody on the board’s speaker, the `SPEAKER` audio device. It starts a stream over a ring of two periods, and the class calls it back each time the hardware is done with one: the callback, which runs in an interrupt, computes the next 20 ms of the sine and hands the period back with `pbl_audio_period_ready()`. The ring comes from `pbl_audio_buf_alloc()`, so the hardware can reach it, and is filled before the stream starts.

On a watch, listen to four rising notes. The emulator plays them on the host’s audio output when run with `pbl run`; `pbl samples` runs it silent.

Run:

`pbl samples samples/drivers/speaker`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/audio.h\>#include \<pbl/drivers/battery.h\>#include \<pbl/kernel/sem.h\>#include \<pbl/util/size.h\>#include \<pbl/util/trig.h\>#define RATE 16000U#define PERIOD\_MS 20U#define PERIODS 2U#define NOTE\_MS 200U#define AMPLITUDE (INT16\_MAX / 4)#define PERIOD\_FRAMES (RATE \* PERIOD\_MS / 1000U)#define NOTE\_FRAMES (RATE \* NOTE\_MS / 1000U)/\* C major arpeggio, in Hz. \*/static const uint32\_t s\_notes[] = {523U, 659U, 784U, 1047U};/\* The stream state, written only by the period callback once it starts. \*/static struct {struct pbl\_sem done;uint32\_t frame;uint32\_t phase;uint32\_t tail;uint32\_t xruns;} s\_play = {.done = PBL\_SEM\_INITIALIZER(0, 1),};static void prv\_fill(int16\_t \*samples) {for (size\_t i = 0U; i \< PERIOD\_FRAMES; i++, s\_play.frame++) {size\_t note = s\_play.frame / NOTE\_FRAMES;if (note \>= ARRAY\_LENGTH(s\_notes)) {samples[i] = 0;continue;}/\* Phase in 1/65536 turns, scaled by 2^16 to keep the fraction. \*/s\_play.phase += (uint32\_t)(((uint64\_t)s\_notes[note] \* TRIG\_MAX\_ANGLE \<\< 16) / RATE);samples[i] = (int16\_t)(sin\_lookup((int32\_t)(s\_play.phase \>\> 16)) \* AMPLITUDE / TRIG\_MAX\_RATIO);}}/\* Runs in an interrupt, each time the hardware is done with a period: refill it and hand it back. \*/static void prv\_period\_done(const struct pbl\_audio \*audio, void \*period,const struct pbl\_audio\_pos \*pos, void \*arg) {prv\_fill(period);s\_play.xruns += pos-\>xrun ? 1U : 0U;(void)pbl\_audio\_period\_ready(audio, period);/\* Done once the whole ring holds silence: the last notes have played out. \*/if (s\_play.frame \>= ARRAY\_LENGTH(s\_notes) \* NOTE\_FRAMES && ++s\_play.tail == PERIODS + 1U) {pbl\_sem\_give(&s\_play.done);}}int main(void) {struct pbl\_audio\_stream stream = {.rate = RATE,.channels = 1U,.period\_frames = PERIOD\_FRAMES,.periods = PERIODS,.period\_done = prv\_period\_done,};struct pbl\_audio\_caps caps;int ret;ret = pbl\_audio\_get\_caps(SPEAKER, &caps);if (ret != 0) {fprintf(stderr, "speaker: not ready (%d)\n", ret);return ret;}if ((caps.rates & pbl\_audio\_rate\_bit(RATE)) == 0U || caps.min\_channels \> 1U) {fprintf(stderr, "speaker: cannot play mono at %d Hz\n", (int)RATE);return -ENOTSUP;}ret = pbl\_audio\_set\_gain(SPEAKER, caps.gain\_default\_mdb);if (ret == 0) {ret = pbl\_audio\_set\_mute(SPEAKER, false);}if (ret != 0) {fprintf(stderr, "speaker: gain failed (%d)\n", ret);return ret;}/\* The ring must be reachable by DMA: allocate it with the class. \*/stream.buf = pbl\_audio\_buf\_alloc(pbl\_audio\_ring\_bytes(&stream));if (stream.buf == nullptr) {fprintf(stderr, "speaker: out of memory\n");return -ENOMEM;}/\* Fill the whole ring before starting: the hardware plays it right away. \*/for (uint8\_t i = 0U; i \< PERIODS; i++) {prv\_fill((int16\_t \*)stream.buf + i \* PERIOD\_FRAMES);}printf("speaker: playing %d notes at %d Hz\n", (int)ARRAY\_LENGTH(s\_notes), (int)RATE);#ifdef CONFIG\_BATTERY\_HIGH\_LOAD/\* The speaker draws more than the battery gives by default. \*/(void)battery\_high\_load\_request();#endifret = pbl\_audio\_start(SPEAKER, &stream);if (ret == 0) {ret = pbl\_sem\_take(&s\_play.done, PBL\_MSEC(ARRAY\_LENGTH(s\_notes) \* NOTE\_MS + 1000U));(void)pbl\_audio\_stop(SPEAKER);}#ifdef CONFIG\_BATTERY\_HIGH\_LOAD(void)battery\_high\_load\_release();#endifpbl\_audio\_buf\_free(stream.buf);if (ret != 0) {fprintf(stderr, "speaker: playback failed (%d)\n", ret);return ret;}printf("speaker: done, %d xruns\n", (int)s\_play.xruns);printf("Sample done\n");return 0;}
```

## Output

```
speaker: playing 4 notes at 16000 Hz
speaker: done, \d+ xruns
Sample done
```

Generated from PebbleOS [9e782207f](https://github.com/coredevices/pebbleos/commit/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d). This page is maintained in the pebbleos repository: [docs/samples/drivers/speaker.md](https://github.com/coredevices/pebbleos/blob/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d/docs/samples/drivers/speaker.md).
