texttoaudio.io

WAV to C array converter

Turn a WAV audio file into a C array you can paste into microcontroller firmware. Choose a WAV file and this page reads its header and writes the sample values as C code. Everything runs in your browser and the file is not uploaded.

WAV in · C array out · .h file
runs in your browser · free
WAV file

Choose a WAV file, or load the example. Compressed WAV files such as ADPCM are not supported.

Your C code

Copy it or save it.

No file yet. Choose a WAV file or load the example.

WAV to C array online free: how to use it

Four steps.

  1. Choose a .wav file, or press the example button.
  2. Check the About this file panel: encoding, channels, sample rate, frames, length and the number of header bytes before the first sample.
  3. Pick the bit depth. For many small boards 8-bit unsigned is the starting point, and 16-bit signed suits I2S outputs.
  4. Press Copy, or Download .h to save a header file named after the array.

Long arrays show only their first lines in the code box. Copy and Download .h include every line.

About this file panel after loading the example sine wave, showing 16-bit integer PCM, one channel, 8000 Hz, 400 frames and a 44 byte header, next to the option controls
Chrome test run with the built-in example.

File to C array: what the converter writes

A plain header file.

The output has a comment naming the source and settings, an optional include guard, #include <stdint.h>, the constants and the array. An 8-bit mono file called click.wav gives const uint8_t click[] plus click_len, click_sample_rate and click_channels. The name is cleaned up from the file name, and you can type another in the Array name box.

Hexadecimal output uses two digits for 8-bit and four for 16-bit values. A negative signed value is written as a minus sign and the magnitude, such as -0x0002, so it compiles without a conversion warning. Stereo stays interleaved, and _len counts every value, so it is twice the frames.

C code box showing a header with include guard, stdint include, length, sample rate and channel constants and a const uint8_t array of hexadecimal values, with a flash size line above it
The example converted to 8-bit unsigned, hexadecimal, with the defaults left on.

8-bit unsigned or 16-bit signed

They suit different outputs.

8-bit unsigned

Values run from 0 to 255 with silence at 128, which is how WAV files store 8-bit audio. It typically matches a PWM output, where the value becomes a duty cycle, or an 8-bit DAC. It uses one byte per sample.

16-bit signed

Values run from -32768 to 32767 with silence at 0, as in WAV files. I2S chips and codecs typically take 16-bit signed words. It uses two bytes per sample. The Sample type menu can also write 16-bit unsigned.

Converting to fewer bits keeps the top bits and rounds down: 16 to 8 bits is a shift right by eight. A float file is scaled and rounded to the nearest whole value. Mixing to mono averages the channels. There is no dithering.

C code box after choosing 16-bit signed output and ticking PROGMEM, showing the avr pgmspace include block and a const int16_t array with PROGMEM after the name
The same example as 16-bit signed with PROGMEM ticked.

WAV to header file (.h): why the 44 bytes come off

The header describes the audio. Your player does not need it.

A basic WAV starts with 44 bytes: 12 for the RIFF header, 24 for the fmt chunk and 8 for the start of the data chunk. If you send the whole file to an output as audio, those bytes play as if they were samples, which typically sounds like a click.

The header is not always 44 bytes. The extensible format has a 40-byte fmt chunk, making 68. Editors often add a LIST chunk with tags before the audio, and chunks of odd length are followed by a padding byte.

This page walks the chunks, skips what it does not need, honours padding and starts at the first data byte. The About this file panel shows the header size it found. A file that does not start with RIFF and WAVE is refused with a message, and so is one whose data chunk claims more bytes than the file holds. The total length written in the RIFF header is not checked.

How much flash will the array use?

Multiply four numbers.

Bytes = sample rate x seconds x bytes per sample x channels. The size line above the code box shows it for your settings.

  • 2 s at 8000 Hz, 8-bit, mono: 8000 x 2 x 1 x 1 = 16,000 bytes.
  • 1 s at 16,000 Hz, 16-bit, mono: 16,000 x 1 x 2 x 1 = 32,000 bytes.
  • 0.5 s at 22,050 Hz, 16-bit, stereo: 22,050 x 0.5 x 2 x 2 = 44,100 bytes.
  • The example: 400 values, so 400 bytes as 8-bit and 800 as 16-bit.

The .h text is larger than the flash it produces: 0x80, is six characters for one byte. Only the compiled array counts against flash. To shrink it, lower the rate, use 8-bit or mix to mono. Check your chip's data sheet for its flash size.

Audio to C array for Arduino: PROGMEM and playback

What the option changes, and a sketch to start from.

On classic AVR boards such as the Uno, a plain array is copied into the small RAM at start-up, which usually runs out quickly. PROGMEM keeps it in flash, and you read values with pgm_read_byte or pgm_read_word. On ESP32 and most ARM boards constant arrays typically stay in flash already, so PROGMEM can stay off. The block this page writes works either way.

Illustration only, not run on hardware. Timer and pin set-up are left out, and the timer must fire at the sample rate.

// 8-bit, unsigned, PROGMEM on
#include "sine_440hz.h"
volatile uint32_t pos = 0;

// call from a timer interrupt at the sample rate
void playNextSample() {
  uint8_t s = pgm_read_byte(&sine_440hz[pos]);
  analogWrite(9, s);  // PWM duty, 0 to 255
  if (++pos >= sine_440hz_len) pos = 0;
}

Limits you should know

The page states what it does not do.

  • Simple resampling. The Sample rate menu uses linear interpolation with no anti-aliasing filter, so lowering the rate can add noise. Convert in an audio editor for better results.
  • No compressed WAV. ADPCM, mu-law and A-law are refused.
  • Not tested on hardware. The C code was checked with a desktop compiler only.

To pull samples out of an instrument bank first, use SoundFont to WAV. To make a sound from scratch, the car engine sound generator saves a WAV you can bring here. To make a Nintendo 3DS audio file instead, use MP3 to BCSTM.

WAV to C array converter: questions

How do I convert a WAV file to a C array?

Choose your WAV file at the top of this page, or press the example button. The page reads the header and writes the sample values as a C array. Pick the bit depth and other options, then press Copy or Download .h. The file is not uploaded.

Does the converter keep the 44-byte WAV header?

Not by default. With the Output menu on sample data only, the array holds just the audio values. The whole file option keeps every byte, header included, and switches off the bit depth, channel and sample rate options.

Which WAV files can it read?

Integer PCM at 8, 16, 24 and 32 bits, and 32-bit float, including the extensible header. ADPCM, mu-law and A-law are refused with a message that names the encoding.

Is the sample rate conversion good enough for music?

It is simple linear interpolation with no anti-aliasing filter, so lowering the rate can add noise. It suits short sounds and quick tests. For better quality, resample in an audio editor and keep the Sample rate menu on the file's own rate.

What does the PROGMEM option do?

It adds PROGMEM to the array, makes it const, and adds a block that includes avr/pgmspace.h on AVR and defines PROGMEM as empty elsewhere. On AVR you read values with pgm_read_byte or pgm_read_word. On ESP32 and most ARM boards it is usually not needed.

Does it work on Arduino, ESP32 and STM32?

The output is plain C with fixed-width integer types, checked only with a desktop C compiler. It has not been tried on real boards. Compare the size line under the code box with your chip's flash.

More on the desk