Main Content

read

Read next consecutive audio file

Description

example

data = read(ADS) returns audio extracted from the datastore. Each subsequent call to the read function continues reading from the endpoint of the previous call.

example

[data,info] = read(ADS) also returns information about the extracted audio data.

Examples

collapse all

Specify the file path to the audio samples included with Audio Toolbox™. Create an audio datastore that points to the specified folder.

folder = fullfile(matlabroot,'toolbox','audio','samples');
ADS = audioDatastore(folder);

While the audio datastore has unread files, read consecutive files from the datastore. Use progress to monitor the fraction of files read.

while hasdata(ADS)
    data = read(ADS);
    fprintf('Fraction of files read: %.2f\n',progress(ADS))
end
Fraction of files read: 0.03
Fraction of files read: 0.06
Fraction of files read: 0.09
Fraction of files read: 0.12
Fraction of files read: 0.15
Fraction of files read: 0.18
Fraction of files read: 0.21
Fraction of files read: 0.24
Fraction of files read: 0.26
Fraction of files read: 0.29
Fraction of files read: 0.32
Fraction of files read: 0.35
Fraction of files read: 0.38
Fraction of files read: 0.41
Fraction of files read: 0.44
Fraction of files read: 0.47
Fraction of files read: 0.50
Fraction of files read: 0.53
Fraction of files read: 0.56
Fraction of files read: 0.59
Fraction of files read: 0.62
Fraction of files read: 0.65
Fraction of files read: 0.68
Fraction of files read: 0.71
Fraction of files read: 0.74
Fraction of files read: 0.76
Fraction of files read: 0.79
Fraction of files read: 0.82
Fraction of files read: 0.85
Fraction of files read: 0.88
Fraction of files read: 0.91
Fraction of files read: 0.94
Fraction of files read: 0.97
Fraction of files read: 1.00

Specify the file path to the audio samples you want to include in the audio datastore. In this example, the samples are located on a local desktop. Create an audio datastore that points to the specified folder.

folder = 'C:\Users\bhemmat\Desktop';
ADS = audioDatastore(folder,'LabelSource','foldernames');

When you read data from the datastore, you can additionally return information about the data as a struct. The information struct contains the file name, any labels associated with the file, and the sample rate of the file.

[data,info] = read(ADS);
info
info = 

  struct with fields:

    SampleRate: 44100
      FileName: 'C:\Users\bhemmat\Desktop\Turbine-16-44p1-mono-22secs.wav'
         Label: Desktop

Input Arguments

collapse all

Specify ADS as an audioDatastore object.

Output Arguments

collapse all

Audio data, returned as a M-by-N matrix, where:

  • M –– Total samples per channel in file.

  • N –– Number of channels in file.

Information about audio data, returned as a struct with the following fields:

  • FileName –– Name of the current file.

  • Label –– All labels of the file.

  • SampleRate –– Sample rate of the file.

Introduced in R2018b