Audio File IO Routines

Routines to printout information about an audio file, read an audio file, write an audio file, and compare data in two audio files.
Updated 8 Dec 2020

View License

Audio File IO Routines v1 2020-11-08
This package provides routines for printing information about an audio file, reading an audio file, and writing an audio file. These functions are wrappers around the Matlab routines audioinfo, audioread, and audiowrite. A fourth routine compares the data in two audio files.

This routine prints the information about an audio file. An example of the printout is:
Audio file: D:\Matlab\Audio\test\AudioFiles\Ti32s.aifc
Samples / channel : 5000 (0.5 s)
Sampling frequency: 10000 Hz
Number of channels: 2 (32-bit integer)
Title: integer32 AIFF-C/sowt
Artist: P. Kabal
Comment: 2017-09-21 16:59:06 UTC
Based solely on the information returned by audioinfo, it is not possible to determine whether an uncompressed 32-bit data is float or integer. In the example, PrintAudioPar peeked inside the file to determine whether the file uses 32-bit floats or 32-bit integers.
PrintAudioPar has a work-around on Windows to find the correct case of the full file name when audioinfo finds a case-insensitive match to the requested file name.

This routine reads audio data from an audio file and returns the data in a double array. In its simplest form it reads data directly from the audio file. It has another mode useful for programs that process audio data block-by-block. In that case, the routine implements a look-ahead buffer so that subsequent requests for data can be satisfied from the buffer.
ReadAudioData checks for incorrect behaviour in older implementations of audioread. It throws an error when an audio file containing µ-law or A-law data is accessed with a Matlab version before Matlab 2018a.
This routine takes data from a real double or real single array and writes it to a WAV or FLAC file with integer data. For WAV files, the default behaviour of audiowrite for float input values (nominally in the range from -1 to +1) is scale to those values to a range of values supported by the integer file data and then convert to integer using the floor function. This routine uses rounding instead to reduce the maximum magnitude of the quantization error by a factor of two.

WriteAudioData checks for anomalous behaviour in audiowrite.
It throws an error for writing to WAV files with Matlab versions before Matlab 2015a. The scaling used for float input to audiowrite is incorrect for those versions.
It signals a warning when writing to WAV files when the total length of the data in the file is an odd number of bytes for Matlab versions before Matlab 2017b. The resulting WAV file is malformed – it is missing a padding byte. While audioread accepts this malformed file, other programs may not.
Clipping warnings in audiowrite are based on the input float values lying outside the interval -1≤x≤+1. For the value x = 1, the data is clipped without a warning message. Clipping occurs when the data after conversion to integer values exceeds the range supported by the file format. WriteAudioData temporarily turns off clipping messages generated by Matlab and provides its own clipping message (including the total number of samples clipped) based on when clipping actually occurs.

This routine takes one or two file names as input. For a single file, the statistics for the audio data is printed. These statistics include the minimum value, the maximum value, the standard deviation, and the mean value. The values are printed as a percent of full scale. For multi-channel data, the data is treated as a single channel.
For two files, this routine prints the statistics as above for each file. The data in the two files is compared.
File A:
Std Dev: 64%, Mean: -0.00456%
Max: 100%, Min: -100%
File B:
Std Dev: 32%, Mean: -0.00228%
Max: 50%, Min: -50%
SNR = 6.02 dB
SNR = 91.67 dB (File B Gain = 2.000)
Max Diff: 50% full scale, No. Diff = 9999/10000
The signal-to-noise ratio (SNR) uses the first file as a reference and calculates the ratio of the energy in the first file to the energy of the difference between the samples of the two files. This ratio is expressed in decibel units. An alternative SNR calculation finds the best gain for the second file to maximize the SNR. In the example output above, we see that the second file is a scaled version of the first file.

Cite As

Peter Kabal (2024). Audio File IO Routines (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2020a
Compatible with R2015b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Added CompAudioData