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

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

This sample captures two seconds from the board’s microphone, the `MIC` audio device, and prints the peak and RMS level of every 200 ms. The class calls the sample back each time the hardware has filled a period of the ring; the callback, which runs in an interrupt, measures it, gives it back with `pbl_audio_period_ready()` and queues the level of each 200 ms for the main thread to print.

On a watch, speak or tap near the microphone while it listens: the levels rise. The emulator’s microphone is silent unless the host feeds it audio, so it reports 0.

Run:

`pbl samples samples/drivers/mic`

Boards:

every board

## Sources

src/main.c

```
#include \<errno.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/drivers/audio.h\>#include \<pbl/kernel/msgq.h\>#include \<pbl/util/math.h\>#define RATE 16000U#define PERIOD\_MS 20U#define PERIODS 2U#define WINDOW\_MS 200U#define WINDOWS 10U#define PERIOD\_FRAMES (RATE \* PERIOD\_MS / 1000U)#define WINDOW\_PERIODS (WINDOW\_MS / PERIOD\_MS)struct level {int32\_t peak;int32\_t rms;bool xrun;};/\* The callback runs in an interrupt: it measures and queues, the main thread prints. \*/static PBL\_MSGQ\_DEFINE(s\_levels, sizeof(struct level), WINDOWS);static struct {uint32\_t periods;int32\_t peak;uint64\_t sum\_sq;bool xrun;} s\_win;static void prv\_period\_done(const struct pbl\_audio \*audio, void \*period,const struct pbl\_audio\_pos \*pos, void \*arg) {const int16\_t \*samples = period;for (size\_t i = 0U; i \< PERIOD\_FRAMES; i++) {int32\_t v = samples[i];s\_win.peak = MAX(s\_win.peak, ABS(v));s\_win.sum\_sq += (uint64\_t)(v \* v);}s\_win.xrun |= pos-\>xrun;/\* Done with the period: the hardware may fill it again. \*/(void)pbl\_audio\_period\_ready(audio, period);if (++s\_win.periods == WINDOW\_PERIODS) {struct level level = {.peak = s\_win.peak,.rms = integer\_sqrt((int64\_t)(s\_win.sum\_sq / (WINDOW\_PERIODS \* PERIOD\_FRAMES))),.xrun = s\_win.xrun,};(void)pbl\_msgq\_put(&s\_levels, &level, PBL\_NO\_WAIT);s\_win.periods = 0U;s\_win.peak = 0;s\_win.sum\_sq = 0U;s\_win.xrun = false;}}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(MIC, &caps);if (ret != 0) {fprintf(stderr, "mic: not ready (%d)\n", ret);return ret;}if ((caps.rates & pbl\_audio\_rate\_bit(RATE)) == 0U || caps.min\_channels \> 1U) {fprintf(stderr, "mic: cannot capture mono at %d Hz\n", (int)RATE);return -ENOTSUP;}/\* 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, "mic: out of memory\n");return -ENOMEM;}printf("mic: capturing %d ms at %d Hz\n", (int)(WINDOWS \* WINDOW\_MS), (int)RATE);ret = pbl\_audio\_start(MIC, &stream);for (unsigned int i = 0U; ret == 0 && i \< WINDOWS; i++) {struct level level;ret = pbl\_msgq\_get(&s\_levels, &level, PBL\_MSEC(WINDOW\_MS \* 4U));if (ret == 0) {printf("mic: peak %d, rms %d%s\n", (int)level.peak, (int)level.rms,level.xrun ? ", overrun" : "");}}(void)pbl\_audio\_stop(MIC);pbl\_audio\_buf\_free(stream.buf);if (ret != 0) {fprintf(stderr, "mic: capture failed (%d)\n", ret);return ret;}printf("Sample done\n");return 0;}
```

## Output

```
mic: capturing 2000 ms at 16000 Hz
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
mic: peak \d+, rms \d+
Sample done
```

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