%% 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 dsp.TimeScope and dsp.SpectrumAnalyzer. Click  to read from a file and write to your speaker.

audioTestBench

%
%
%% 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 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 31-Oct-2019 13:55:00 -0400.
%
% % Create test bench input and output
% fileReader = dsp.AudioFileReader('Filename','RockDrums-44p1-stereo-11secs.mp3');
% deviceWriter = audioDeviceWriter('SampleRate',fileReader.SampleRate);
%
% % Create scopes
% timeScope = dsp.TimeScope('SampleRate',44100, ...
%     'TimeSpan',1, ...
%     'TimeSpanOverrunAction','Scroll', ...
%     'AxesScaling','Manual', ...
%     'BufferLength',176400, ...
%     'ShowLegend',true, ...
%     'ChannelNames',{'Input channel 1','Output channel 1'}, ...
%     'ShowGrid',true, ...
%     'YLimits',[-1 1]);
% specScope = dsp.SpectrumAnalyzer('SampleRate',44100, ...
%     'PlotAsTwoSidedSpectrum',false, ...
%     'FrequencyScale','Log', ...
%     'ShowLegend',true, ...
%     'ChannelNames',{'Input channel 1','Output channel 1'}, ...
%     'YLimits',[-141.09585005611308 2.0945795945027577]);
%
%
% % Set up the system under test
% sut = compressor;
% sut.SampleRate = fileReader.SampleRate;
% sut.Ratio = 9.11364;
% sut.Threshold = -35.8766;
% sut.KneeWidth = 1.62338;
% sut.AttackTime = 0.350649;
% sut.MakeUpGain = 2.91558;
%
% % 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
%     timeScope([in(:,1),out(:,1)]);
%     specScope([in(:,1),out(:,1)]);
%
%     % Process parameterTuner callbacks
%     drawnow limitrate
% end
%
% % Clean up
% release(sut)
% release(fileReader)
% release(deviceWriter)
% release(timeScope)
% release(specScope)
%
% 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.
%
% Copyright 2019 The MathWorks, Inc.