The commands below write and read the raw forms the explorer offers. Every FFmpeg
command on this page was run with FFmpeg 8 and gives the result described; the SoX and
Audacity entries are from their documentation, because neither is installed
where this page was checked. Replace in.raw and out.wav with your
own names.
FFmpeg: raw to something else
Tell FFmpeg the format with -f, the rate with -ar and the channels
with -ac, before -i:
ffmpeg -f s16le -ar 44100 -ac 2 -i in.raw out.wav
Without -ar and -ac FFmpeg assumes 44,100 Hz and one channel. The format names:
FFmpeg -f | Explorer Format, Bits, Sign, Byte order |
|---|---|
s8, u8 | Linear, 8 bits, signed or unsigned |
s16le, s16be, u16le, u16be | Linear, 16 bits |
s24le, s24be, u24le, u24be | Linear, 24 bits |
s32le, s32be, u32le, u32be | Linear, 32 bits |
f32le, f32be, f64le, f64be | IEEE floating point, 32 or 64 bits |
mulaw, alaw | µ-law and A-law (ITU) |
vidc | Acorn VIDC logarithmic |
sln (read only) | Linear, 16 bits, little-endian, 8,000 Hz default |
The codec behind a format can be forced to read a raw stream of codes. Put -c:a before -i:
ffmpeg -f u8 -c:a sdx2_dpcm -ar 22050 -ac 1 -i in.raw out.wav reads SDX2 (as the explorer’s test does);
the same works with adpcm_yamaha, adpcm_ima_oki and adpcm_ct.
FFmpeg: something else to raw
ffmpeg -i in.wav -f s16le -acodec pcm_s16le out.raw # 16-bit little-endian, the file’s own rate and channels
ffmpeg -i in.wav -f u8 out.raw # 8-bit unsigned
ffmpeg -i in.wav -f f32le out.raw # 32-bit float
ffmpeg -i in.wav -ar 8000 -ac 1 -f mulaw out.ul # telephone µ-law
ffmpeg -f lavfi -i "sine=frequency=440:duration=1" -f s16le tone.raw
The rate and channel count are not written anywhere: note them down, or put them in the
file name (tone_44100_mono.s16le) where the explorer’s auto fields read them.
Cutting a part: -ss 0.1 -t 0.2 as output options.
Fixing a raw file
# skip a 44-byte header (tail counts from 1)
tail -c +45 in.wav > data.raw
# the same with dd
dd if=in.wav of=data.raw bs=1 skip=44
# swap the byte order of 16-bit samples
ffmpeg -f s16le -ar 22050 -ac 2 -i in.raw -f s16be swapped.raw
# split a stereo file into two mono raw files, and put them back together
ffmpeg -f s16le -ar 22050 -ac 2 -i in.raw -af "pan=mono|c0=c0" -f s16le left.raw
ffmpeg -f s16le -ar 22050 -ac 2 -i in.raw -af "pan=mono|c0=c1" -f s16le right.raw
ffmpeg -f s16le -ar 22050 -ac 1 -i left.raw -f s16le -ar 22050 -ac 1 -i right.raw \
-filter_complex "[0:a][1:a]amerge=inputs=2" -f s16le stereo.raw
The last pair gives back the original interleaved file byte for byte; the explorer’s
planar layout reads a file of the two runs one after the other
(cat left.raw right.raw > planar.raw).
SoX
SoX needs the format of raw input on the command line (from its documentation; not run here):
sox -t raw -r 44100 -e signed -b 16 -c 2 in.raw out.wav
sox -r 8000 -e mu-law -b 8 -c 1 in.ul out.wav
sox in.wav -t raw -e signed -b 16 -c 2 out.raw
Its names for raw types are also extensions: .sw (16-bit signed), .uw, .sb, .ub,
.ul (µ-law), .al (A-law) and .sl (32-bit signed), which is the origin of the explorer’s
extension list. Encodings: signed-integer, unsigned-integer, floating-point,
a-law, u-law, oki-adpcm, ima-adpcm, ms-adpcm
and others.
Audacity
File, Import, Raw Data… asks the same questions as the explorer’s options bar: Encoding (signed or unsigned 8, 16, 24 and 32-bit, float, µ-law, A-law, GSM, ADPCM variants), Byte order, Channels, Start offset (bytes) – the explorer’s Skip, Amount to import (per cent) and Sample rate. Audacity’s own Export as Other uncompressed files can write a headerless RAW file with a chosen encoding.
Programming
With NumPy (from its documentation; NumPy is not installed where this page was checked, so this was not run):
import numpy as np
samples = np.fromfile("in.raw", dtype="<i2") # 16-bit little-endian signed
stereo = samples.reshape(-1, 2) # interleaved: one row a frame
planar = samples.reshape(2, -1) # planar: one row a channel
floats = np.fromfile("in.raw", dtype="<f4") # 32-bit little-endian float
unsigned = np.fromfile("in.raw", dtype="u1").astype(np.int16) - 128
The dtype’s first character is the byte order (< little, > big), the
letter the kind (i signed, u unsigned, f float) and the digit the bytes.