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 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 27-May-2020 15:34:48 -0400. % Create test bench input and output fileReader = dsp.AudioFileReader('Filename','RockGuitar-16-44p1-stereo-72secs.wav'); deviceWriter = audioDeviceWriter('SampleRate',fileReader.SampleRate); % Create scopes timeScope = dsp.TimeScope('SampleRate',fileReader.SampleRate, ... 'TimeSpan',1, ... 'TimeSpanOverrunAction','Scroll', ... 'AxesScaling','Manual', ... 'BufferLength',4*fileReader.SampleRate, ... 'ShowLegend',true, ... 'ChannelNames',{'Input channel 1','Output channel 1'}, ... 'ShowGrid',true, ... 'YLimits',[-1 1]); specScope = dsp.SpectrumAnalyzer('SampleRate',fileReader.SampleRate, ... 'PlotAsTwoSidedSpectrum',false, ... 'FrequencyScale','Log', ... 'ShowLegend',true, ... 'ChannelNames',{'Input channel 1','Output channel 1'}, ... 'YLimits',[-137.68466894418421 21.786707286754297]); % 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); drawnow % 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.
See Also
Audio Test Bench | parameterTuner
| audioPlayerRecorder
| dsp.AudioFileWriter
| dsp.AudioFileReader
| audioDeviceReader
| audioDeviceWriter