%% Modify Wavetable While Stream Processing
% Modify the Wavetable property of a wavetableSynthesizer object while stream processing. Visualize the wavetable and play the resulting audio.
% Create a single-cycle waveform for the wavetableSynthesizer to index into. Create a wavetable synthesizer object.

t = 0:0.001:1;
exponent = 5;
waveTable = [t.^exponent,fliplr(t.^exponent)] - 0.5;

waveSynth = wavetableSynthesizer('Wavetable',waveTable);

%
% Create a dsp.ArrayPlot object to plot the wavetable as it is modified over time. Create an audioDeviceWriter object to listen to the signal output by your wavetable synthesizer.

arrayPlotter = dsp.ArrayPlot('YLimits',[-1,1],'PlotType','Line');
deviceWriter = audioDeviceWriter;

%
% In an audio stream loop, incrementally modify the Wavetable property of the wavetable synthesizer and plot it. Call the synthesizer to output a waveform and play the waveform to your audio device.

tic
while toc < 10
    exponent = exponent - 0.01;
    waveSynth.Wavetable = [t.^abs(exponent),fliplr(t.^abs(exponent))] - 0.5;
    
    arrayPlotter(waveSynth.Wavetable')
    
    deviceWriter(waveSynth());
end
release(deviceWriter)

%
% Copyright 2012 The MathWorks, Inc.