%
% [midi_versions] = destroy_structure_midi_excerpt(fname, input_dir,
% output_dir, start_time_sec, excerpt_length_sec);
%
% reads in midi file fname from the input_dir, and generates several
% scrambled versions in addition to an unmanipulated version of an
% excerpt of the file
% user specifies the start time of the excerpt and its duration
%
% This function uses the MIDI Toolbox, freely available here:
% https://www.jyu.fi/hum/laitokset/musiikki/en/research/coe/materials/miditoolbox/
%
% The toolbox must be installed and in your path for this function to work.
%
% June-20-2009 Josh McDermott
% comments added Dec-31-2010
%
% this version generated stimuli in Fedorenko, Behr, & Kanwisher, 2011, PNAS
% and Fedorenko, McDermott, Norman-Haignere, & Kanwisher, submitted

function [midi_versions] = destroy_structure_midi_excerpt_orig(fname, input_dir, output_dir, start_time_sec, excerpt_length_sec)

prefixes = {'unscram_', 'scram_p_', 'scram_r_', 'scram_pr_'};

nmat = readmidi([input_dir '/' fname]);
tempo=gettempo(nmat);
nmat(:,1) = nmat(:,1)-nmat(1,1); %get rid of initial pause if there is one
nmat(:,6) = nmat(:,6)-nmat(1,6);

%move to start time
start_index = min(find(nmat(:,6)>=start_time_sec));
temp = nmat(start_index:size(nmat,1),:);
temp(:,1) = temp(:,1)-temp(1,1);
temp(:,6) = temp(:,6)-temp(1,6);

%chop off everything after excerpt length
end_index = min(find(temp(:,6)>excerpt_length_sec));
nmat = temp(1:end_index,:);

writemidi(nmat, [output_dir '/' prefixes{1} fname], 120, tempo);

rand_pitch=1;
rand_dur=1;
rand_onset=1;

%randomize pitch
if rand_pitch
    inst_notes = find(nmat(:,3)~=10); %3rd column indicates the MIDI
                                      %channel; 10 is percussion channel
                                      %so we leave its "pitch" alone
    all_notes = nmat(inst_notes,4); %4th column contains the pitch in semitones
    aton_notes = all_notes+6*rand(size(all_notes))-3; %add random value to
                                                    %blur tonal pitch distribution
    aton_notes = round(aton_notes); %round to integer
    notes_sorted = sort(aton_notes);
    x=rand(size(notes_sorted));
    x(:,2)=[1:size(all_notes,1)];
    y=sortrows(x,1); 

    nmat_at=nmat;
    nmat_at(inst_notes,4)=notes_sorted(y(:,2)); %randomly assign pitches to notes in piece
    writemidi(nmat_at, [output_dir '/' prefixes{2} fname], 120, tempo); %save
                                                                   %pitch-scrambled version
else
    nmat_at=nmat;
end

beats_per_sec = mode(nmat(:,2)./nmat(:,7));
%jitter note onset
if rand_onset
    jitter = rand(size(nmat_at(:,1)));
    nmat_at2=nmat_at;
    nmat_at2(:,1) = nmat_at2(:,1)+jitter;
    nmat_at2(:,6) = nmat_at2(:,1)/beats_per_sec; % columns 1 and 6 both have
                                                 % onset, but in
                                                 % different units
    nmat_at2(:,1) = sort(nmat_at2(:,1)); %make sure jittering does not mess up order
    nmat_at2(:,6) = sort(nmat_at2(:,6));
    %writemidi(nmat_at2, ['scram_po_' fname], 120, tempo);
    nmat_at4 = nmat_at2;
    nmat_at4(:,4) = nmat(:,4); %use unscrambled pitch
    %writemidi(nmat_at4, ['scram_o_' fname], 120, tempo);
else
    nmat_at2=nmat_at;
end

%randomize duration
if rand_dur
    all_durs_beats = nmat(:,2);
    clear x y
    x=rand(length(all_durs_beats),1);
    x(:,2)=[1:length(all_durs_beats)];
    y=sortrows(x,1);

    nmat_at3=nmat_at2;
    nmat_at3(:,2)=all_durs_beats(y(:,2)); %randomly assign durations to notes
    nmat_at3(:,7)=nmat_at3(:,2)/beats_per_sec; %columns 2 and 7 both
                                               %represent duration, just in diff units
    writemidi(nmat_at3, [output_dir '/' prefixes{4} fname], 120, tempo);
    nmat_at5 = nmat_at3;
    nmat_at5(:,4) = nmat(:,4); %use correct pitch
    writemidi(nmat_at5, [output_dir '/' prefixes{3} fname], 120, tempo);
end

                %unscram    _p       _r     _pr
midi_versions = [{nmat nmat_at nmat_at5 nmat_at3}];
