Skip to main content
This tutorial builds an Android app that turns a spoken prompt into an image. Whisper transcribes the speech and Stable Diffusion generates the image, both on the Hexagon NPU through Qualcomm AI Engine Direct (QNN). The reference code is in qnn-genai-speech_to_image.
Android app interface recording audio and displaying generated AI artwork

App demo showing speech input generating AI images

What you’ll build

An Android app that:
  • Records speech with the Android audio APIs
  • Transcribes it with a Whisper model on the NPU
  • Generates an image from the transcript with Stable Diffusion on the NPU
  • Shows the prompts and images in a chat interface

Architecture overview

Architecture diagram connecting Android speech input, Edge Impulse inference, QNN acceleration, and image generation

Application architecture with QNN acceleration

Prerequisites

  • Android Studio 2024.3.1 or later
  • Android NDK 27.2.12479018
  • The latest stable Rust toolchain
  • Qualcomm AI Engine Direct SDK 2.40.0
  • Python 3.10, for the asset generation script
  • A Snapdragon device with a Hexagon NPU and at least 500 MB of free storage for the models

Supported devices

The app is tested on Snapdragon 8 Elite. It needs a Hexagon NPU Gen 2 or later:

1. Clone the repository

2. Set up the Qualcomm AI Engine Direct SDK

Download and extract the SDK, then point QNN_SDK_ROOT at it:

3. Install Rust and the Android target

4. Compile Rust tokenization libraries

Build the Rust tokenizer and detokenizer libraries for Android:

5. Set up Android project dependencies

Copy QNN libraries

Create the JNI libraries folder:
Copy in the libraries. Replace VXX and vXX with your Hexagon version, for example V79 for Snapdragon 8 Elite:

Copy QNN headers and utilities

Run the dependency resolution script:
The script copies the QNN headers and C++ utilities into the project.

Download the remaining source files

6. Download AI model assets

Create the assets folder:

Tokenizers

Stable Diffusion models

Download text_encoder.bin, unet.bin, and vae_decoder.bin from Qualcomm Package Manager (you need a Qualcomm account), and put them in speech_to_image/app/src/main/assets/.

Whisper models

Audio processing assets

Generate embedding tokens

Run the Python script to generate the token files:
This creates t_emb_0.raw through t_emb_19.raw and tokens.raw in the assets directory.

7. Configure device for QNN

Push the HTP skeleton library to the device:
These commands need root access and modify the device’s /vendor partition, which might void its warranty.

8. Build and run

  1. Check that AndroidManifest.xml declares the microphone permission:
  2. In Android Studio, select File -> Open and open the speech_to_image folder.
  3. Build and run the app on your Snapdragon device, and grant the microphone permission when prompted.
  4. Tap the microphone button and speak a prompt.
The app transcribes your speech, generates an image, and shows both in the chat.

Troubleshooting

  • Check that all QNN and Rust libraries are in jniLibs/arm64-v8a/.
  • Check that resolveDependencies.sh finished without errors.
  • Check that your NDK and CMake versions match the prerequisites.
  • Check that the device has a Hexagon NPU and that you pushed the skeleton library in step 7.
  • Check that every asset file from step 6 is in the assets folder.
The device might be throttling because it’s hot. Let it cool down and try again.
  • Check the microphone permission and the device’s audio settings.
  • Whisper Tiny is the smallest Whisper model. A larger Whisper model transcribes more accurately.

Next steps

Resources