function result = morseSinWavSamples(inputMorseStr,ditDur,freq,sampleRate)
%morseSinWavSamples return sine wave samples for Morse Code String
%
% Examples:
%    >> aBead = convertMorseAthruF('A BEAD')
%    aBead =
%    . ---       --- . . .   .   . ---   --- . .
%    >> samples = morseSinWavSamples(aBead,1/15,500,44100);
%    >> wavwrite(0.9 * samples,44100,16,'A_BEAD.wav');
%  
%    A_BEAD.wav should now be a Morse Code file containing 
%    the Morse Code for 'A BEAD'

  legalChars = '.- ';
  
  k=1;

  dit = sineWavVector(sampleRate,freq,ditDur);
  dah = sineWavVector(sampleRate,freq,ditDur*3);
  space = zeros(1,floor(sampleRate * ditDur));
  
  result = [];
  
  while (k <= length(inputMorseStr))

    switch(inputMorseStr(k))
      
     case '.'
      
      % add the dit vector 
      if length(result) == 0
	result = dit;
      else
	result = [ result dit(2:end) ];
      end;
      k = k+1;
      
     case '-'
      if ( k+2 > length(inputMorseStr) || ...
	   strcmp(inputMorseStr(k:k+2),'---')==0 )
	error(['Expecting two more -- after - in position ' num2str(k)]);
      end;
      
      % add the dit vector 
      if length(result) == 0
	result = dah;
      else
	result = [ result dah(2:end) ];
      end;
      k = k+3;
      
     case ' '

      % add the space vector 
      if length(result) == 0
	result = space;
      else
	result = [ result space(2:end) ];
      end;
      k = k+1;
      
     otherwise
       error(['Illegal character: "' inputMorseStr(k) ...
	      '" at position: ' num2str(k)  ...
	      ' in inputMorseStr'])
    end % switch
    
  end % while
  return;
  
end
