%BINAURAL MASKING LEVEL DIFFERENCE (BMLD)

%
% Guy J Brown
% MAD - Matlab Auditory Demostrations
% (C) University of Sheffield 1998
%
% See the web help for full instructions

% Modified slightly for release V2.1
% to add generic UI handling
% MPC 11/6/99

function bmld(action)

if nargin<1
	action='init';
end

switch action

case 'init'
  
  f=findobj('Tag','bmld_fig');
 if ~isempty(f)
    figure(f);
  else     
     bmld_gui;
		ud.maxDb=80.0;
		ud.samplingFrequency=5000.0;
		ud.duration=1; 
		ud.noiseIntensity=60.0;
		ud.toneFreq=250.0;
		ud.ampscale=1/(db2amp(ud.noiseIntensity)+db2amp(ud.maxDb));

		set(findobj('Tag','Slider1'),'Max',ud.maxDb);
		set(findobj('Tag','Slider2'),'Max',ud.maxDb);
		
		% make noise signal
		rand('state',10);
		ud.no=db2amp(ud.noiseIntensity)*2*(rand(1,ud.duration*ud.samplingFrequency)-0.5);
		
		% make noise with pi phase lag
		lag=zeros(1,floor(ud.samplingFrequency/(2*ud.toneFreq)));
		ud.npi=[lag ud.no(1:(length(ud.no)-length(lag)))];
		
		% make uncorrelated noise
		rand('state',100);
		ud.nu=db2amp(ud.noiseIntensity)*2*(rand(1,ud.duration*ud.samplingFrequency)-0.5);

		set(gcf,'UserData',ud);
		set(findobj('Tag','PresetPopup'),'Value',1);
		bmld('presetChanged');
  end  
		
	case 'intensity1changed'
		ud=get(gcf,'UserData');
		intensity=get(findobj('Tag','Slider1'),'Value');
		dbstring=[num2str(intensity) ' dB'];
		set(findobj('Tag','IntensityText1'),'String',dbstring);
		set(gcf,'UserData',ud);
		computebmld;

	case 'intensity2changed'
		ud=get(gcf,'UserData');
		intensity=get(findobj('Tag','Slider2'),'Value');
		dbstring=[num2str(intensity) ' dB'];
		set(findobj('Tag','IntensityText2'),'String',dbstring);
		set(gcf,'UserData',ud);
		computebmld;
				
	case 'listen1'
		ud=get(gcf,'UserData');
		intensity=get(findobj('Tag','Slider1'),'Value');
		leftSig1=makeSignal(get(findobj('Tag','LeftMenu1'),'Value'),intensity);
		leftSig2=makeSignal(get(findobj('Tag','LeftMenu2'),'Value'),intensity);
		rightSig1=makeSignal(get(findobj('Tag','RightMenu1'),'Value'),intensity);
		rightSig2=makeSignal(get(findobj('Tag','RightMenu2'),'Value'),intensity);
		stimulus=[leftSig1+leftSig2; rightSig1+rightSig2].*ud.ampscale;
		% really need an onset and offset ramp	
		sound(stimulus',ud.samplingFrequency);
		
	case 'listen2'
		ud=get(gcf,'UserData');
		intensity=get(findobj('Tag','Slider2'),'Value');
		leftSig1=makeSignal(get(findobj('Tag','LeftMenu1a'),'Value'),intensity);
		leftSig2=makeSignal(get(findobj('Tag','LeftMenu2a'),'Value'),intensity);
		rightSig1=makeSignal(get(findobj('Tag','RightMenu1a'),'Value'),intensity);
		rightSig2=makeSignal(get(findobj('Tag','RightMenu2a'),'Value'),intensity);
		stimulus=[leftSig1+leftSig2; rightSig1+rightSig2].*ud.ampscale;
		% really need an onset and offset ramp	
		sound(stimulus',ud.samplingFrequency);
		
	case 'presetChanged'
		preset=get(findobj('Tag','PresetPopup'),'Value');
		switch preset
			case 1 % NoSo/NoSpi
				setmenus(1,2,1,2,4,2,1,2);
    		case 2 % NoSo/NpiSo
				setmenus(1,2,1,2,1,5,1,2);
    		case 3 % NmSm/NoSm
				setmenus(3,3,1,2,3,2,1,2);
    		case 4 % NmSm/NpiSm
				setmenus(3,3,1,2,3,5,1,2);
    		case 5 % NoSo/NuSo
				setmenus(1,2,1,2,1,6,1,2);
    		case 6 % NoSo/NuSpi
				setmenus(1,2,1,2,4,6,1,2);
		end

otherwise
  maduievent('bmld',action);

end

function setmenus(left1,left2,right1,right2,left1a,left2a,right1a,right2a);
ud=get(gcf,'UserData');
set(findobj('Tag','LeftMenu1'),'Value',left1);
set(findobj('Tag','LeftMenu2'),'Value',left2);
set(findobj('Tag','RightMenu1'),'Value',right1);
set(findobj('Tag','RightMenu2'),'Value',right2);
set(findobj('Tag','LeftMenu1a'),'Value',left1a);
set(findobj('Tag','LeftMenu2a'),'Value',left2a);
set(findobj('Tag','RightMenu1a'),'Value',right1a);
set(findobj('Tag','RightMenu2a'),'Value',right2a);
set(findobj('Tag','Slider1'),'Value',ud.maxDb/2);
set(findobj('Tag','Slider2'),'Value',ud.maxDb/2);
bmld('intensity1changed');
bmld('intensity2changed');

function sig=makeSignal(num,intensity)
ud=get(gcf,'UserData');
switch num
	case 1	% tone
		pad=zeros(1,ud.duration*ud.samplingFrequency/4);
		tn=bmldtone(ud.toneFreq,db2amp(intensity),ud.samplingFrequency,1000*ud.duration/2);
		sig=[pad tn pad];
	case 2	% noise
		sig=ud.no;
	case 3	% nothing
		sig=zeros(1,ud.duration*ud.samplingFrequency);
	case 4	% tone pi
		pad=zeros(1,ud.duration*ud.samplingFrequency/4);
		tn=bmldtone(ud.toneFreq,db2amp(intensity),ud.samplingFrequency,1000*ud.duration/2);
		lag=zeros(1,floor(ud.samplingFrequency/(2*ud.toneFreq)));
		sig=[pad lag tn(1:(length(tn)-length(lag))) pad];
	case 5	% noise pi
		sig=ud.npi;
	case 6	% uncorrelated noise
		sig=ud.nu;
end
	
function computebmld
ud=get(gcf,'UserData');
bmld = get(findobj('Tag','Slider1'),'Value')-get(findobj('Tag','Slider2'),'Value');
set(findobj('Tag','bmldvalue'),'String',[num2str(bmld) ' dB']);
	
function y=bmldtone(freq,amp,sampleRate,ms)
t=[0:millitosamples(ms,sampleRate)-1];
y=amp*sin(2*pi*t*freq/sampleRate);

