Main Content

Process and Analyze Streaming Audio

This example shows how to create an audio test bench and apply real-time processing.

Open the Audio Test Bench

The Audio Test Bench app enables you to graphically set up your audio input and output, audio processing, and open common analysis tools like timescope and spectrumAnalyzer. Click to read from a file and write to your speaker.


View the Audio Signal in the Time and Frequency Domains

Click and to analyze the audio signal in the time and frequency domains.

Apply Dynamic Range Compression

To apply dynamic range compression to the audio, first click to stop the audio I/O, then enter compressor in the Add an Object Under Test edit box. The tunable properties of the compressor object are exposed. You can tune these properties while the test bench runs.

Generate a Test Bench Script

To generate a test bench script, first click to stop the audio I/O, then click . The Audio Test Bench generates code in a new untitled script. The code generated by the test bench in this example is shown below.

% Test bench script for 'compressor'.
% Generated by Audio Test Bench on 06-Jul-2022 11:47:07 UTC-04:00.

% Create test bench input and output
fileReader = dsp.AudioFileReader('Filename','RockGuitar-16-44p1-stereo-72secs.wav');
deviceWriter = audioDeviceWriter('SampleRate',fileReader.SampleRate);

% Create scopes
timeScope = timescope(SampleRate=fileReader.SampleRate, ...
    TimeSpanSource="property", ...
    TimeSpan=1, ...
    AxesScaling="manual", ...
    BufferLength=176400, ...
    ChannelNames=["Input channel 1","Input channel 2","Output channel 1","Output channel 2"], ...
    Position=[538 508 800 500], ...
    YLimits=[-1 1], ...

specScope = spectrumAnalyzer(SampleRate=fileReader.SampleRate, ...
    PlotAsTwoSidedSpectrum=false, ...
    FrequencyScale="log", ...
    AxesScaling="manual", ...
    AveragingMethod="exponential", ...
    ForgettingFactor=0.8, ...
    YLimits=[-132.83298112 25.39711835], ...
    ChannelNames=["Input channel 1","Input channel 2","Output channel 1","Output channel 2"], ...

% Set up the system under test
sut = compressor;
sut.SampleRate = fileReader.SampleRate;

% Uncomment to open visualizer:
% visualize(sut);

% Open parameterTuner for interactive tuning during simulation
tuner = parameterTuner(sut);

% Stream processing loop
nUnderruns = 0;
while ~isDone(fileReader)
    % Read from input, process, and write to output
    in = fileReader();
    out = sut(in);
    nUnderruns = nUnderruns + deviceWriter(out);
    % Visualize input and output data in scopes
    % Process parameterTuner callbacks
    drawnow limitrate

% Clean up

You can add additional processing steps, scopes, and analysis tools to the script. If you run the generated script, the parameterTuner opens and enables you to tune parameters while stream processing.

See Also

| | | | | |

Related Topics