function showSound(wavfilename)
% Open an interactive graphics window to explore a wav file

[d, fs]= wavread(wavfilename);
nSamples= length(d);

close all
figure
title(wavfilename)

xL=1;  xR=length(d);  width=xR-xL;  aThird= width/3;
buttonTop=-1.1;  buttonBottom=-1.9;  buttonHorizEdge= -1.5;
myPlot(xL:xR, d)

[x,y]= ginput(1);
while y<buttonTop && y>buttonBottom && x>xL && x<xR
    if y<buttonHorizEdge  % sound actions
        if x<xL+aThird           % clicked Play File
            sound(d(1:nSamples), fs)
            pauseTime= nSamples/fs;
        elseif x<xR-aThird       % clicked Play Segment
            sound(d(xL:xR), fs)
            pauseTime= (xR-xL+1)/fs;
        else                     % Surprise button!
            pauseTime= myEcho(d(xL:xR), fs);
        end
        pause(pauseTime)
    else                  % graphical actions
        if x < xL+aThird          % clicked Go Left
            xL= max(1, xL-ceil(aThird));
            xR= min(xL+width, length(d));
        elseif x > xR-aThird      % clicked Go Right
            xR= min(length(d), xR+ceil(aThird));
            xL= max(1, xR-width);
        elseif y>-1.3             % clicked Zoom In
            xL= xL+floor(aThird);
            xR= xR-floor(aThird);
        else                      % clicked Zoom Out
            xL= max(1, xL-ceil(aThird));
            xR= min(length(d), xR+ceil(aThird));
        end
        myPlot(xL:xR, d(xL:xR))
        width=xR-xL;  aThird= width/3;
    end
    [x,y]= ginput(1);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myPlot(x, y)
% plot with y-axis fixed at [-1.5, 1] and x-axis tight.
% Draw "buttons" for interactive graphics.

bottom1= -1.5;  % bottom of first row of buttons
bottom2= -1.9;  % bottom of second row of buttons

if length(y)>50
    plot(x,y)
else
    plot(x,y,'-o')
end
axis([x(1) x(end) -2 1])
%axis manual
set(gca, 'ytick', -2:.5:1)
set(gca, 'yticklabel', {'', '', '-1.0', '-0.5', '0.0', '0.5', '1.0'})
xlabel('Sample Number')
ylabel('                 Sound Signal Value')

hold on

% Draw "buttons" for interactive graphics
width= x(end)-x(1);
aThird= width/3;
DrawRect(x(1),bottom1, width,0.4, 'y')
DrawRect(x(1)+aThird,bottom1, aThird,0.2, 'g')
DrawRect(x(1)+aThird,-1.3, aThird,0.2, 'm')
HA='HorizontalAlignment';  VA='VerticalAlignment';  F='fontweight';
text(x(1),-1.3, '  Go Left', HA, 'left', F, 'bold')
text(x(end),-1.3, 'Go Right  ', HA, 'right', F, 'bold')
text(x(1)+width/2,-1.2, 'Zoom In', HA, 'center', F, 'bold')
text(x(1)+width/2,-1.4, 'Zoom Out', HA, 'center', F, 'bold')
DrawRect(x(1),bottom2, width,.4, 'b')
DrawRect(x(1)+aThird,bottom2, aThird,.4, 'b')
text(x(1)+aThird/2,-1.7, 'Play File', HA, 'center', F, 'bold', 'color', 'w')
text(x(1)+width/2,-1.7, 'Play Segment', HA, 'center', F, 'bold', 'color', 'w')
text(x(end)-aThird/2,-1.7, 'Surprise!', HA, 'center', F, 'bold', ...
     'fontsize', 14, 'color', 'w')

hold off
shg


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function t= myEcho(d, fs)
% Make an echo out of sound data d.  fs is original sampling frequency.
% t is the time required to play the sound with echo added.

currentMax= max(abs(d));  % max signal in current (echo) segment
echoD= d;
k=0;  maxK=10;            % k is #echos so far

while  currentMax>.05 && k<maxK
    k= k+1;
    echoD= echoD*.7;  % reduce volume by factor of .3
    d= [d; echoD];
    currentMax= max(abs(echoD));
end

sound(d, fs)
t= length(d)/fs;
    


