function music = NoiseMusic(noise, notes, notelength)

%% improved  Wed Mar 12 18:47:29 PST 2003:
%%   faster pace  (notes 1/4 sec duration).
%%   selection of notes so that they do not go below human audible range
%%   selection of only "whole tones" instead of half tones

if (nargin<2); notes = 100; end        % default number of notes is 100

Fs = 8192; % for now
if (nargin<3); notelength = Fs/4; end  % default note length is 1/4 sec.

n = size(noise,1);
if (n < notes); notes=n; end

maxnoise = max(abs(noise));

music = zeros( notes * notelength, 1 );

% turn the input vector "noise" into a sequence of notes, which can be played

c = 2^(1/12)  % permits half tones
c2 = c^2      % avoids  half tones

for s=1:notes
%  f = 440 * (noise(s)/maxnoise + 3/2);  % 440 Hz = "A"
%  f = 440.00 * c  ^ floor(24 * noise(s)/maxnoise);  % 440.00 Hz = "A"
%  f = 261.63 * c  ^ floor(24 * noise(s)/maxnoise);  % 261.63 Hz = "C"
   f = 261.63 * c2 ^ floor(24 * noise(s)/maxnoise);  % 261.63 Hz = "C"
   t = (1:notelength)/notelength;
   music( (1+(s-1)*notelength):s*notelength ) = sin(2*pi*f*t);
end;

sound(music);

