function [ output_args ] = waveforms ( input_args )
clear all;

global waveForms_mainGUI mainTune syncOn fmOn distOn firstRun player ringOn
syncOn = 0;
fmOn = 0;
distOn = 0;
firstRun = 0;
player = [];
ringOn = 0;

% --- waveForms GIU ---
waveForms_main.fh = figure('units','pix',...
    'position',[200 100 800 600],...
    'menubar','none',...
    'name','Wave forms',...
    'color',[.75,.75,.75],...
    'numbertitle','off',...
    'resize','off');
% --- waveForms background ---
waveForms_main.background = uibuttongroup('unit','pix',...
    'position',[20 20 760 560],...
    'backgroundcolor',[1,1,1]);
% --- Title ---
waveForms_main.title = uicontrol('style','text',...
    'unit','pix',...
    'position',[30 530 110 40],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',24,...
    'string','Waves');
waveForms_main.functioncall(1) = uicontrol('style','push',...
    'unit','pix',...
    'position',[40 515 88 26],...
    'horizontalalignment','center',...
    'fontsize',16,...
    'string','Reset',...
    'Enable','on',...
    'callback',{@resetWaves});
% --- Wave settings ---
waveForms_main.waveSettings = uibuttongroup('units','pixels',...
    'Title','Settings',...
    'pos',[150 510 620 60],...
    'fontsize',11,...
    'backgroundcolor',[1,1,1],...
    'Visible','on');
waveForms_main.mainTune = uicontrol(waveForms_main.waveSettings,...
    'style','slide',...
    'unit','pix',...
    'position',[5 5 97 16],...
    'min',55,'max',4400,'val',440,...
    'SliderStep',[0.10 0.10],...
    'Visible','on',...
    'callback',{@setMaintune});
waveForms_main.tuneTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[5 25 80 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','Main tune',...
    'Visible','on');
waveForms_main.tuneText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[75 25 38 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','',...
    'Visible','on');
waveForms_main.dutyTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[130 15 80 25],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','DC',...
    'Visible','on');
waveForms_main.dutyText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[134 5 30 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','',...
    'Visible','on');
waveForms_main.pulseTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[170 15 80 25],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','PW',...
    'Visible','on');
waveForms_main.pulseText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[174 5 30 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',9,...
    'string','',...
    'Visible','on');
waveForms_main.syncTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[210 15 80 25],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','Sync Hz',...
    'Visible','on');
waveForms_main.syncText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[220 5 60 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','',...
    'Visible','on');
waveForms_main.fmfreqTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[280 15 80 25],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','FM Hz',...
    'Visible','on');
waveForms_main.fmfreqText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[290 5 60 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','',...
    'Visible','on');
waveForms_main.fmdepthTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[350 15 80 25],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','FM depth',...
    'Visible','on');
waveForms_main.fmdepthText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[370 5 60 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','',...
    'Visible','on');
waveForms_main.rmTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[420 15 80 25],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','RM Hz',...
    'Visible','on');
waveForms_main.rmText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[426 5 60 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','',...
    'Visible','on');
waveForms_main.distTitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[490 25 80 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','Dist',...
    'Visible','on');
waveForms_main.distText = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[496 5 30 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',11,...
    'string','',...
    'Visible','on');
waveForms_main.syncWavetitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[531 25 30 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','center',...
    'fontsize',8,...
    'string','Sync',...
    'Visible','on');
waveForms_main.syncWave = uicontrol(waveForms_main.waveSettings,...
    'style','checkbox',...
    'unit','pix',...
    'position',[540 5 30 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',8,...
    'Value',0,...
    'Visible','on');
waveForms_main.fmWavetitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[564 25 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','center',...
    'fontsize',8,...
    'string','FM',...
    'Visible','on');
waveForms_main.fmWave = uicontrol(waveForms_main.waveSettings,...
    'style','checkbox',...
    'unit','pix',...
    'position',[568 5 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',8,...
    'Value',0,...
    'Visible','on');
waveForms_main.ringModtitle = uicontrol(waveForms_main.waveSettings,...
    'style','text',...
    'unit','pix',...
    'position',[592 25 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','center',...
    'fontsize',8,...
    'string','RM',...
    'Visible','on');
waveForms_main.rmWave = uicontrol(waveForms_main.waveSettings,...
    'style','checkbox',...
    'unit','pix',...
    'position',[596 5 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',8,...
    'Value',0,...
    'Visible','on');
% --- Buttons ---
waveForms_main.functioncalls = uibuttongroup('units','pixels',...
    'Title','Wave & mod',...
    'pos',[30 30 110 480],...
    'fontsize',11,...
    'backgroundcolor',[1,1,1],...
    'Visible','on');
waveForms_main.functioncall(1) = uicontrol(waveForms_main.functioncalls,...
    'style','push',...
    'unit','pix',...
    'position',[10 430 88 26],...
    'horizontalalignment','center',...
    'fontsize',16,...
    'string','Sine',...
    'Enable','on',...
    'callback',{@playSound,1});
waveForms_main.functioncall(2) = uicontrol(waveForms_main.functioncalls,...
    'style','push',...
    'unit','pix',...
    'position',[10 395 88 26],...
    'horizontalalignment','center',...
    'fontsize',16,...
    'string','Tri',...
    'Enable','on',...
    'callback',{@playSound,2});
waveForms_main.dutyC = uicontrol(waveForms_main.functioncalls,...
    'style','slide',...
    'unit','pix',...
    'position',[5 372 93 22],...
    'min',0,'max',1,'val',0.5,...
    'SliderStep',[0.05 0.1],...
    'Visible','on',...
    'callback',{@setdutyC});
waveForms_main.functioncall(3) = uicontrol(waveForms_main.functioncalls,...
    'style','push',...
    'unit','pix',...
    'position',[10 337 88 26],...
    'horizontalalignment','center',...
    'fontsize',16,...
    'string','Sqr',...
    'Enable','on',...
    'callback',{@playSound,3});
waveForms_main.pulseW = uicontrol(waveForms_main.functioncalls,...
    'style','slide',...
    'unit','pix',...
    'position',[5 314 93 22],...
    'min',0,'max',100,'val',50,...
    'SliderStep',[0.05 0.1],...
    'Visible','on',...
    'callback',{@setPulsewidth});
waveForms_main.functioncall(5) = uicontrol(waveForms_main.functioncalls,...
    'style','push',...
    'unit','pix',...
    'position',[10 279 50 26],...
    'horizontalalignment','center',...
    'fontsize',12,...
    'string','Sync',...
    'Enable','on',...
    'callback',{@modulations,5});
waveForms_main.syncOntext = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[70 279 30 22],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'string','On',...
    'Visible','off');
waveForms_main.syncFreq = uicontrol(waveForms_main.functioncalls,...
    'style','slide',...
    'unit','pix',...
    'position',[5 256 93 22],...
    'min',55,'max',8800,'val',440,...
    'SliderStep',[0.01 0.10],...
    'Visible','on',...
    'callback',{@setSyncfreq});
waveForms_main.functioncall(6) = uicontrol(waveForms_main.functioncalls,...
    'style','push',...
    'unit','pix',...
    'position',[10 221 50 26],...
    'horizontalalignment','center',...
    'fontsize',12,...
    'string','FM',...
    'Enable','on',...
    'callback',{@modulations,6});
waveForms_main.fmOntext = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[70 221 30 22],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'string','On',...
    'Visible','off');
waveForms_main.fmFreq = uicontrol(waveForms_main.functioncalls,...
    'style','slide',...
    'unit','pix',...
    'position',[5 198 93 22],...
    'min',55,'max',8800,'val',880,...
    'SliderStep',[0.01 0.10],...
    'Visible','on',...
    'callback',{@setFmfreq});
waveForms_main.fmDepth = uicontrol(waveForms_main.functioncalls,...
    'style','slide',...
    'unit','pix',...
    'position',[5 174 93 22],...
    'min',-1,'max',-0.01,'val',-1,...
    'SliderStep',[0.1 0.10],...
    'Visible','on',...
    'callback',{@setFmdepth});
waveForms_main.functioncall(7) = uicontrol(waveForms_main.functioncalls,...
    'style','push',...
    'unit','pix',...
    'position',[10 139 50 26],...
    'horizontalalignment','center',...
    'fontsize',12,...
    'string','RM',...
    'Enable','on',...
    'callback',{@modulations,7});
waveForms_main.ringOntext = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[70 139 30 22],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'string','On',...
    'Visible','off');
waveForms_main.rmFreq = uicontrol(waveForms_main.functioncalls,...
    'style','slide',...
    'unit','pix',...
    'position',[5 116 93 22],...
    'min',55,'max',8800,'val',220,...
    'SliderStep',[0.1 0.10],...
    'Visible','on',...
    'callback',{@setRing});
waveForms_main.functioncall(8) = uicontrol(waveForms_main.functioncalls,...
    'style','push',...
    'unit','pix',...
    'position',[10 81 50 26],...
    'horizontalalignment','center',...
    'fontsize',12,...
    'string','Dist',...
    'Enable','on',...
    'callback',{@modulations,8});
waveForms_main.distOntext = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[70 81 30 22],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'string','On',...
    'Visible','off');
waveForms_main.distAmount = uicontrol(waveForms_main.functioncalls,...
    'style','slide',...
    'unit','pix',...
    'position',[5 58 93 22],...
    'min',50,'max',99,'val',50,...
    'SliderStep',[0.1 0.10],...
    'Visible','on',...
    'callback',{@setDist});
waveForms_main.waveMixtext = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[5 30 100 22],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'string','Wave mix',...
    'Visible','on');
waveForms_main.waveMixsine = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[4 20 30 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',8,...
    'string','Sin',...
    'Visible','on');
waveForms_main.waveMixtri = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[44 20 30 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',8,...
    'string','Tri',...
    'Visible','on');
waveForms_main.waveMixsqr = uicontrol(waveForms_main.functioncalls,...
    'style','text',...
    'unit','pix',...
    'position',[83 20 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',8,...
    'string','Sqr',...
    'Visible','on');
waveForms_main.waveMixsine = uicontrol(waveForms_main.functioncalls,...
    'style','checkbox',...
    'unit','pix',...
    'position',[5 5 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'Value',0,...
    'Visible','on');
waveForms_main.waveMixtri = uicontrol(waveForms_main.functioncalls,...
    'style','checkbox',...
    'unit','pix',...
    'position',[45 5 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'Value',0,...
    'Visible','on');
waveForms_main.waveMixsqr = uicontrol(waveForms_main.functioncalls,...
    'style','checkbox',...
    'unit','pix',...
    'position',[85 5 20 15],...
    'backgroundcolor',[1,1,1],...
    'horizontalalignment','left',...
    'fontsize',12,...
    'Value',0,...
    'Visible','on');

% --- Show wave ---
waveForms_main.showWave = uibuttongroup('units','pixels',...
    'Title','Visualisation',...
    'pos',[150 30 620 480],...
    'fontsize',11,...
    'backgroundcolor',[1,1,1],...
    'Visible','on');
waveForms_main.showWavefigure = axes('Parent',waveForms_main.showWave,...
    'Visible','off');

mainTune = get(waveForms_main.mainTune,'value');
set(waveForms_main.tuneText,'str',mainTune);
pulseSetting = get(waveForms_main.pulseW,'value');
set(waveForms_main.pulseText,'str',pulseSetting);
dutySetting = (100*get(waveForms_main.dutyC,'value'));
set(waveForms_main.dutyText,'str',dutySetting);
syncSetting = get(waveForms_main.syncFreq,'value');
set(waveForms_main.syncText,'str',syncSetting);
fmfreqSetting = get(waveForms_main.fmFreq,'value');
set(waveForms_main.fmfreqText,'str',fmfreqSetting);
fmdepthValue = get(waveForms_main.fmDepth,'value');
fmdepthSetting = (fmdepthValue+1)*100;
set(waveForms_main.fmdepthText,'str',fmdepthSetting);
rmfreqSetting = get(waveForms_main.rmFreq,'value');
set(waveForms_main.rmText,'str',rmfreqSetting);
distValue = get(waveForms_main.distAmount,'value');
distSetting = round((distValue*2)-100);
set(waveForms_main.distText,'str',distSetting);

waveForms_mainGUI = waveForms_main(:);

movegui(waveForms_main.fh,'center')

onStart();
refresh();

end



function [ output_args ] = playSound ( varargin )

global waveForms_mainGUI player mainTune syncOn fmOn distOn ringOn firstRun

pulseW = get(waveForms_mainGUI.pulseW,'value');
dutyC = get(waveForms_mainGUI.dutyC,'value');
syncFrequency = get(waveForms_mainGUI.syncFreq,'value');
fmFreq = get(waveForms_mainGUI.fmFreq,'value');
fmDepth = get(waveForms_mainGUI.fmDepth,'value');
distAmount = get(waveForms_mainGUI.distAmount,'value');
rmFreq = get(waveForms_mainGUI.rmFreq,'value');

button = num2str(varargin{1,3});
nBits = 16;
Fs = 44100;
freq = mainTune;
soundLength = 2;
t = (0:(1/Fs):soundLength);
t2 = (0:(1/Fs):(1/syncFrequency));

sineSound = sin(2*pi*freq.*t)';
triSound = sawtooth(2*pi*freq.*t,dutyC)';
sqrSound = square(2*pi*freq.*t,pulseW)';

rmsSine = sqrt(mean(sineSound.^2));
rmsTri = sqrt(mean(triSound.^2));
rmsSqr = sqrt(mean(sqrSound.^2));

levelSet = min([rmsSine rmsTri rmsSqr]);

switch button
    case '1'
        soundToplay = sineSound;
        rmsFactor = 1;
        rmSound = sin(2*pi*rmFreq.*t)';
        originalSound = sineSound;
        trisqrShape = 0;
    case '2'
        soundToplay = triSound;
        rmsFactor = rmsSine/rmsTri;
        rmSound = sawtooth(2*pi*rmFreq.*t,dutyC)';
        originalSound = triSound;
        trisqrShape = dutyC;
    case '3'
        soundToplay = sqrSound;
        rmsFactor = rmsSine/rmsSqr;
        rmSound = square(2*pi*rmFreq.*t,pulseW)';
        originalSound = sqrSound;
        trisqrShape = pulseW;       
end

if (get(waveForms_mainGUI.waveMixsine,'value') == 1)
    soundToplay = (soundToplay + sineSound)./2;
end
if (get(waveForms_mainGUI.waveMixtri,'value') == 1)
    soundToplay = (soundToplay + triSound)./2;
end
if (get(waveForms_mainGUI.waveMixsqr,'value') == 1)
    soundToplay = (soundToplay + sqrSound)./2;
end

if (syncOn == 1)
    syncSound = square(2*pi*syncFrequency.*t2,50)';
    tempSound = [];
    while (length(tempSound) < length(t))
        tempSound = [tempSound; soundToplay(1:length(syncSound))];
    end
    soundToplay = tempSound(1:length(t));
    freq = syncFrequency;
end

if (fmOn == 1)
    soundToplay = LFM(soundToplay,fmFreq,Fs,fmDepth,button,trisqrShape);
    freq = fmFreq;
end
if (ringOn == 1)
    soundToplay = soundToplay.*rmSound;
else
    rmSound = [];
    originalSound = [];
end
if (distOn == 1)
    distorTion = (distAmount-50)/50;
    k = 2*distorTion/(1-distorTion);
    soundToplay = ((1+k)*(soundToplay))./(1+k*abs(soundToplay));
end
    
drawFigure( t, Fs, freq, soundToplay, rmSound, originalSound );

eqaulSound = (soundToplay.*rmsFactor).*levelSet;
% fade in and out 
mainfadeTime = 10;
mainFade = 0:(1/((round((mainfadeTime/1000)*Fs))-1)):1;
originalGain = ones(1,(length(eqaulSound)-(length(mainFade)*2)));
fadeEnvelope = [mainFade originalGain fliplr(mainFade)]';
fadedWave = (eqaulSound.*fadeEnvelope)*.50;

if (firstRun == 0)
    player = audioplayer(fadedWave, Fs, nBits);
    play(player);
    firstRun = 1;
elseif ~(isplaying(player))
    player = audioplayer(fadedWave, Fs, nBits);
    play(player);
elseif isplaying(player)
    stop(player);
    player = audioplayer(fadedWave, Fs, nBits);
    play(player);
end

end


function [ ] = drawFigure ( t, Fs, freq, soundToplay, rmSound, originalSound)
global waveForms_mainGUI syncOn fmOn ringOn

subplot(2,1,1);plot(t,soundToplay,'k','LineWidth',2);
hold on;

if ((get(waveForms_mainGUI.syncWave,'value') == 1) && (syncOn == 1))
    syncSoundplot = square(2*pi*freq.*t,50)';
    subplot(2,1,1);plot(t,syncSoundplot,'r','LineWidth',2);
end
if ((get(waveForms_mainGUI.fmWave,'value') == 1) && (fmOn == 1))
    fmWaveplot = sin(2*pi*freq.*t)';
    subplot(2,1,1);plot(t,fmWaveplot,'g','LineWidth',2);
end    
if ((get(waveForms_mainGUI.rmWave,'value') == 1) && (ringOn == 1))
    subplot(2,1,1);plot(t,rmSound,'b',t,originalSound,'c','LineWidth',2);
end
hold off;
xlim([0,0.005]);
ylim([-1.1,1.1]);
xlabel('Time (s)');
ylabel('Level');

m = length(soundToplay);
n = pow2(nextpow2(m));
y = fft(soundToplay,n);
f = (0:n-1)*(Fs/n);
power = y.*conj(y)/n;
subplot(2,1,2);loglog(f,power,'LineWidth',2);
xlim([100,20000]);
set(gca,'XTickLabel','100|1000|10000')
ylim([1,10000]);
xlabel('Frequency (Hz)')
ylabel('Power')

refresh();

end

function [] = onStart()

global mainTune

Fs = 44100;
freq = mainTune;
t = (0:(1/Fs):2);
soundToplay = sin(2*pi*freq.*t)';
rmSound = [];
originalSound = [];

drawFigure( t, Fs, freq, soundToplay, rmSound, originalSound )


end




function [ output_args ] = resetWaves ( varargin )

global waveForms_mainGUI syncOn fmOn distOn ringOn mainTune

set(waveForms_mainGUI.tuneText,'str',440);
set(waveForms_mainGUI.mainTune,'value',440);
set(waveForms_mainGUI.pulseText,'str',50);
set(waveForms_mainGUI.dutyText,'str',50);
set(waveForms_mainGUI.syncText,'str',660);
set(waveForms_mainGUI.fmfreqText,'str',880);
set(waveForms_mainGUI.fmdepthText,'str',0);
set(waveForms_mainGUI.rmText,'str',660);
distValue = 50;
set(waveForms_mainGUI.distText,'str',distValue);
distSetting = round((distValue*2)-100);
set(waveForms_mainGUI.distText,'str',distSetting);
set(waveForms_mainGUI.pulseW,'val',50);
set(waveForms_mainGUI.dutyC,'val',0.5);
set(waveForms_mainGUI.syncFreq,'val',440);
set(waveForms_mainGUI.fmFreq,'val',880);
set(waveForms_mainGUI.fmDepth,'val',-1);
set(waveForms_mainGUI.rmFreq,'val',660);
set(waveForms_mainGUI.distAmount,'val',50);
set(waveForms_mainGUI.syncOntext,'Visible','off');
set(waveForms_mainGUI.fmOntext,'Visible','off');
set(waveForms_mainGUI.ringOntext,'Visible','off');
set(waveForms_mainGUI.distOntext,'Visible','off');
syncOn = 0;
fmOn = 0;
ringOn = 0;
distOn = 0;
mainTune = 440;
set(waveForms_mainGUI.syncWave,'value',0);
set(waveForms_mainGUI.fmWave,'value',0);
set(waveForms_mainGUI.rmWave,'value',0);
set(waveForms_mainGUI.waveMixsine,'value',0);
set(waveForms_mainGUI.waveMixtri,'value',0);
set(waveForms_mainGUI.waveMixsqr,'value',0);

onStart();

refresh();

end

function [ output_args ] = showExtra ( varargin )

end

function [ output_args ] = modulations ( varargin )

global waveForms_mainGUI syncOn fmOn distOn ringOn

button = num2str(varargin{1,3});

switch button
    case '5'
        if (syncOn == 0)
            syncOn = 1;
            set(waveForms_mainGUI.syncOntext,'Visible','on');
        else
            syncOn = 0;
            set(waveForms_mainGUI.syncOntext,'Visible','off');
        end
    case '6'
        if (fmOn == 0)
            fmOn = 1;
            set(waveForms_mainGUI.fmOntext,'Visible','on');
        else
            fmOn = 0;
            set(waveForms_mainGUI.fmOntext,'Visible','off');
        end
    case '7'
        if (ringOn == 0)
            ringOn = 1;
            set(waveForms_mainGUI.ringOntext,'Visible','on');
        else
            ringOn = 0;
            set(waveForms_mainGUI.ringOntext,'Visible','off');
        end
    case '8'
        if (distOn == 0)
            distOn = 1;
            set(waveForms_mainGUI.distOntext,'Visible','on');
        else
            distOn = 0;
            set(waveForms_mainGUI.distOntext,'Visible','off');
        end
end

end





function [ output_args ] = setMaintune ( varargin )

global waveForms_mainGUI mainTune

mainTune = get(waveForms_mainGUI.mainTune,'value');
set(waveForms_mainGUI.tuneText,'str',mainTune);

refresh();

end

function [ output_args ] = setPulsewidth ( varargin )

global waveForms_mainGUI

pulseSetting = get(waveForms_mainGUI.pulseW,'value');
set(waveForms_mainGUI.pulseText,'str',pulseSetting);

refresh();

end

function [ output_args ] = setdutyC ( varargin )

global waveForms_mainGUI

dutySetting = (100*get(waveForms_mainGUI.dutyC,'value'));
set(waveForms_mainGUI.dutyText,'str',dutySetting);

refresh();

end

function [ output_args ] = setSyncfreq ( varargin )

global waveForms_mainGUI

pulseSetting = get(waveForms_mainGUI.syncFreq,'value');
set(waveForms_mainGUI.syncText,'str',pulseSetting);

refresh();

end

function [ output_args ] = setFmfreq ( varargin )

global waveForms_mainGUI

fmfreqSetting = get(waveForms_mainGUI.fmFreq,'value');
set(waveForms_mainGUI.fmfreqText,'str',fmfreqSetting);

refresh();

end

function [ output_args ] = setFmdepth ( varargin )

global waveForms_mainGUI

fmdepthValue = get(waveForms_mainGUI.fmDepth,'value');
fmdepthSetting = (fmdepthValue+1)*100;
set(waveForms_mainGUI.fmdepthText,'str',fmdepthSetting);


refresh();

end

function [ output_args ] = setDist ( varargin )

global waveForms_mainGUI

distValue = get(waveForms_mainGUI.distAmount,'value');
distSetting = round((distValue*2)-100);
set(waveForms_mainGUI.distText,'str',distSetting);

refresh();

end

function [ output_args ] = setRing ( varargin )

global waveForms_mainGUI

rmfreqSetting = get(waveForms_mainGUI.rmFreq,'value');
set(waveForms_mainGUI.rmText,'str',rmfreqSetting);

refresh();

end

function [outputY] = LFM(x,fmFreq,Fs,fmDepth,waveForm,dutyC)

[r,c]=size(x);

if (r==1),   % convert row vector to column
    x = x(:);  len = c;
else
    len = r;
end

t = (0:1/Fs:((len-1)/Fs))';
t = t(:,ones(1,size(x,2)));

kf = (fmFreq/Fs)*2*pi/fmDepth;

switch waveForm
    case '1'
        if 0
            % grunden
            outputY = sin(2*pi*fmFreq*t + kf*cumsum(x));
        else
            outputY = sin(kf*cumsum(x));
        end
    case '2'
        outputY = sawtooth(kf*cumsum(x),dutyC);
    case '3'
        outputY = square(kf*cumsum(x),dutyC);
end


end