audioread — Read audio samples from a file.
audioread(filename) decodes an N-by-C sample tensor, where rows are frames and columns are channels. [y, Fs] = audioread(filename) also returns the double sample rate in Hz.
Syntax
y = audioread(filename)
[y, Fs] = audioread(filename)
y = audioread(filename, samples)
[y, Fs] = audioread(filename, samples)
y = audioread(filename, datatype)
[y, Fs] = audioread(filename, samples, datatype)Inputs
filename- Type
StringScalar- Required
- Yes
- Description
- Audio file path.
samples- Type
NumericArray- Required
- No
- Description
- One-based inclusive frame range [first last].
datatype- Type
StringScalar- Required
- Yes
- Description
- Output class: double or native.
datatype- Type
StringScalar- Required
- No
- Default
"double"- Description
- Output class; "native" preserves representable source classes.
Returns
y- Type
NumericArray- Description
- Decoded samples as an N-by-C matrix.
Fs- Type
NumericScalar- Description
- Sample rate in Hz.
Returned values from audioread depend on how many outputs the caller requests.
Errors
RunMat:audioread:InvalidArgument- When
- Filename, sample range, datatype, or output count is invalid.
- Message
audioread: invalid argument
RunMat:audioread:Io- When
- The audio file cannot be read.
- Message
audioread: unable to read file
RunMat:audioread:UnsupportedFormat- When
- The file is not a supported audio container, uses unsupported audio coding, or is malformed.
- Message
audioread: unsupported or invalid audio file
Inputs and range
Filenames are scalar strings, singleton string arrays, or one-row character arrays read through the active filesystem provider. The optional two-element double sample range is one-based and inclusive: audioread(filename, [first last]). Inf as the last endpoint reads through the final frame; typed-integer ranges reject.
The datatype argument is double (default) or native. It may follow the filename directly or a sample range. At most two outputs are available; a zero-output statement still reads and decodes.
Decoding and classes
WAV/RF64 PCM 8-bit unsigned, PCM 16/24/32-bit signed, IEEE 32/64-bit float, and WAVE_FORMAT_EXTENSIBLE PCM/float subformats are decoded. FLAC, MP3, Ogg/Vorbis, and AIFF metadata can be inspected by audioinfo, but their samples are not decoded here.
Default PCM output is normalized double: unsigned 8-bit maps around zero and signed PCM divides by signed full scale. Native integer storage follows the sample container width: uint8 for 8-bit unsigned PCM, int16 for 16-bit signed PCM, and int32 for 24/32-bit signed PCM; 24-bit containers are left-aligned in int32. Extensible PCM may have fewer valid bits left-aligned inside a wider container, such as 24 valid bits in a 32-bit int32 sample. Native IEEE float32 returns single and float64 returns double. Sample tensors are column-major N-by-C, including N-by-1 mono.
Execution
Filesystem I/O and decoding execute on the host, terminate fusion, and create host-resident tensors. No provider kernel runs; GPU-resident arguments reject before provider access. Use gpuArray explicitly after reading if subsequent computation should reside on a device.
The active native, browser, sandbox, or remote filesystem provider must supply the input bytes. A filename is not a transferable file snapshot, so distributed workers require their own accessible file and filesystem capability.
With a seekable filesystem handle, WAV/RF64 sample ranges are read from selected frames in bounded input chunks. Providers that support whole-file reads but not seekable handles use the read fallback. The returned tensor occupies memory proportional to requested frames and channels; providers may have additional internal buffering costs.
When a finite host-memory budget is active, the complete read-and-decode call is admitted before opening the file. If the installed host resource service cannot establish complete bounds for the captured filesystem and decoder, the call fails without reading the file. Calls without a finite host-memory budget retain the ordinary reading path when no measured route is selected.
Examples
Read samples and sample rate
[y, Fs] = audioread('speech.wav');
size(y)
FsExpected output:
ans = 2 1
Fs = 8000Read a one-based inclusive frame range
clip = audioread('speech.wav', [2 Inf]);
clipExpected output:
clip = 0.5Preserve native PCM16 storage
raw = audioread('speech.wav', 'native');
class(raw)Expected output:
ans = int16Using audioread with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how audioread changes the result.
Run a small audioread example, explain the result, then change one input and compare the output.
FAQ
What shape does audioread return?⌄
An N-by-C tensor: N decoded sample frames and C channels; mono is N-by-1.
Does it use the active filesystem provider?⌄
Yes. Native, sandbox, remote, and browser providers can supply file bytes when installed by the host.
Does it decode MP3 or FLAC samples?⌄
Not yet. audioinfo reads metadata for those containers; audioread currently decodes WAV/RF64 PCM and IEEE float.
Related Io functions
Audio
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · filesep · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · memmapfile.subsasgn · memmapfile.subsref · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · getvaropts · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · setvaropts · setvartype · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Data
commit · data.copy · data.create · data.delete · data.exists · data.export · data.import · data.inspect · data.list · data.move · data.open · DataArray.chunk_shape · DataArray.codec · DataArray.dtype · DataArray.fill · DataArray.name · DataArray.rank · DataArray.read · DataArray.resize · DataArray.shape · DataArray.write · Dataset.array · Dataset.arrays · Dataset.attrs · Dataset.begin · Dataset.get_attr · Dataset.has_array · Dataset.id · Dataset.path · Dataset.refresh · Dataset.set_attr · Dataset.set_attrs · Dataset.snapshot · Dataset.version · DataTransaction.abort · DataTransaction.commit · DataTransaction.create_array · DataTransaction.delete_array · DataTransaction.fill · DataTransaction.id · DataTransaction.resize · DataTransaction.set_attr · DataTransaction.set_attrs · DataTransaction.status · DataTransaction.write
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
Source you can read
Every RunMat function's source is public. Read exactly how audioread is executed, line by line, in Rust.
- View the source for audioread in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.