AudioStream: real-time audio streaming (playback and microphone)

AudioStream is a non-visible extension for playing and capturing audio as a stream, in small chunks, instead of as complete files.

I created this extension for one of my projects, where I use it together with my other extension WebSocketExt to send and receive audio between an ESP32 and a smartphone.

The standard App Inventor components work with files. Player plays a file that already exists, and SoundRecorder records a file and hands it back only at the end. An intercom, a walkie-talkie, a simple voice call or remote listening to a microphone need the opposite:

  • playing audio as it arrives, one chunk at a time;
  • getting the microphone audio as it is captured, without waiting for the recording to end.

The extension does not depend on any particular server or protocol. Audio chunks are plain lists of bytes, so you can send and receive them over anything: WebSocket, UDP, TCP or other communication extensions.

Audio format

The format is fixed:

  • 16-bit signed PCM, little-endian
  • 16 kHz
  • mono

This is the most common format on microcontrollers with I2S audio (for example an ESP32 with an INMP441 microphone and a MAX98357A amplifier). Whatever sends audio to the extension must use this same format, and audio coming out of the extension is in this format too.

Audio chunks are lists of numbers from 0 to 255. Each pair of bytes makes one 16-bit sample, low byte first, then high byte.

Permissions

The extension declares the RECORD_AUDIO permission. On recent Android versions it must also be requested from the user at runtime before calling StartRecording, for example with Screen.AskForPermission. Without it, recording does not start and the Error event fires.

General usage

Playback:

  1. Call StartPlayback once.
  2. For each audio chunk you receive, call PlayChunk with the list of bytes.
  3. When finished, call StopPlayback.

Microphone capture:

  1. Request the RECORD_AUDIO permission.
  2. Call StartRecording.
  3. Each captured chunk arrives in the AudioChunkCaptured event; send it wherever you need it.
  4. When finished, call StopRecording.

Playback and capture are independent. You can use either one alone, or both together for two-way communication.

StartPlayback
Starts playback. Call it once, before the first PlayChunk. If playback is already active, the call is ignored.

PlayChunk
Plays one audio chunk immediately. The bytes parameter is a list of numbers from 0 to 255, in the PCM format described above. If called before StartPlayback, it fires the Error event.

StopPlayback
Stops playback and releases the audio device. If playback is not active, it does nothing.

StartRecording
Starts capturing the microphone. From then on, the AudioChunkCaptured event fires continuously until StopRecording is called. Where the device supports it, capture uses Android’s voice-communication processing (echo cancellation and noise suppression). This helps when the phone’s speaker and microphone are active at the same time. If that processing is not available, the extension falls back to the plain microphone.

StopRecording
Stops capturing the microphone and releases its resources. If recording is not active, it does nothing.


Fires for each new chunk captured from the microphone. bytes is a list of numbers from 0 to 255, in the PCM format described above. Each chunk holds 256 samples (512 bytes), about 16 ms of audio, so the event fires roughly 60 times per second. Keep this handler short and only send the chunk from it, with no slow operations.


Fires when playback or recording fails to start, for example because of a missing permission or a format the device does not support. It also fires when a block is used incorrectly, such as PlayChunk before StartPlayback. message describes the problem.

com.audiostreamext.aix (12.0 KB)

Typical example: two-way intercom

  • When the communication starts: StartPlayback and StartRecording.
  • In AudioChunkCaptured: send bytes to the other device over your communication channel.
  • When a chunk arrives from the other device: PlayChunk.
  • When the communication ends: StopPlayback and StopRecording.

I hope You find useful

Best Regards

Marco Perrone

2 Likes