% set up GUI
fig=createmadfig('audiometer'); 
fig=createmadmenus(fig);

ud=get(fig,'UserData');
ud.freqs=[250 500 1000 2000 4000 8000];
ud.calib=[59 53 47 45 49 50];   % for David's headphones and powermac 3400
ud.HLref=70;  % hearing level at which calibration offsets were obtained

maxlevel=60;
minlevel=-20;

ud.resultsAxes = ...
axes('Parent',fig,...
'Position',[0.1 0.1 0.5 0.8],...
'DrawMode','fast',...
'NextPlot','replacechildren',...
'Box','on',...
'XLim', [200 10000],...
'XTick', ud.freqs,...
'YLim', [minlevel maxlevel],...
'YDir','reverse',...
'XGrid','on', ...
'YGrid','on', ...
'XScale','log');

xlabel('Frequency (Hz)')   
ylabel('Hearing Level (dB)')   

uicontrol('Parent',fig, ...
'Position',[0.6 0.8 0.18 0.1], ...
'String','ear:', ...
'HorizontalAlignment','right',...
'Style','text');

ud.earmenu=uicontrol('Parent',fig, ...
'Position',[0.8 0.8 0.15 0.1], ...
'String', 'left|right', ...
'Style','popupmenu');

uicontrol('Parent',fig, ...
'Position',[0.6 0.7 0.18 0.1], ...
'String','frequency:', ...
'HorizontalAlignment','right',...
'Style','text');

ud.freqmenu=uicontrol('Parent',fig, ...
'Position',[0.8 0.7 0.15 0.1], ...
'String',num2cell(ud.freqs), ...
'Style','popupmenu',...
'Value',1);

uicontrol('Parent',fig, ...
'Position',[0.6 0.48 0.18 0.07], ...
'String','level:', ...
'HorizontalAlignment','right',...
'Style','text');

sliderstep=1/(maxlevel-minlevel);

ud.levelSlider=uicontrol('Parent',fig, ...
'Position',[0.85 0.35 0.05 0.35], ...
'Style','slider', ...
'Callback','audiometer levelChanged',...
'Min',minlevel,...
'Max',maxlevel,...
'Value',round(mean([minlevel maxlevel])),...
'SliderStep',[sliderstep 10*sliderstep]); 

ud.levelText=uicontrol('Parent',fig, ...
'Position',[0.8 0.48 0.05 0.07], ...
'Style','text', ...
'String',getuicontrolvalue(ud.levelSlider,'int'),...
'Tag','text',...
'HorizontalAlignment','left');

uicontrol('Parent',fig, 'Position',[0.7 0.2 0.25 0.1], ...
'Style','pushbutton', ...
'Tag','button',...
'Callback','audiometer playsound',...
'String','play');       

ud.recordbutton=uicontrol('Parent',fig, 'Position',[0.7 0.1 0.25 0.05], ...
'Style','pushbutton', ...
'Tag','button',...
'Callback','audiometer plot',...
'String','record threshold',...
'Enable','off');       

set(gcf,'UserData',ud);

