function beepcode(code)
  %beepcode - create a series of beeps and play on loudspeaker
  %
  % Syntax:
  %   beepcode(code);
  %
  % Parameters:
  %   code - (n,2) array of frequency and duration values [f1, d1].
  %          Frequency is specified in Hz, duration in msecs. Frequency of 0 means pause.
  %          if code is not specified a default value of [1000,0.2] is set.
  %
  % Example:
  %   beepcode([1000,200;0,300;900,200]);

  % Published under GNU General Public License (GPLv3)
  % https://www.gnu.org/licenses/gpl-3.0
  % Copyright 2017, Georg Wiora, Courage + Khazaka electronic GmbH

  if nargin<1
    % Default beep: 0.2 seconds, 1000 Hz
    code = [1000,0.2];
  endif

  % sample frequency / Hz
  fs = 8000;
  
  % total length of sound in ms
  T = sum(code(:,2));
  
  % number of samples
  ns = ceil(T/1000*8000);
  
  % Create target array
  tone = zeros(1,ns);
  % Sample pointer
  ts=1;
  % Create beeps
  for i=1:size(code,1)
    % duration in samples
    dur = floor(code(i,2)/1000*fs);
    % If not pause
    if code(i,1)>0
      % Period in samples
      P = fs / code(i,1);
      % Interpolate a simple rectangle signal to sample rate.
      tone(ts:ts+dur-1) = interp1([0,P-1],[0,1],mod(0:dur-1,P),'nearest','extrap');
    endif
    % Advance pointer
    ts = ts + dur;
  endfor
  % Play sound
  sound(tone,fs);
endfunction