Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % infile='C:\Musik\MIDI_archive\lolo2.mid';
- infile='C:\Musik\NSF\Kid Icarus 01.mid';
- q=find(infile=='\');
- if numel(q)>0
- songname=infile((q(end)+1):(end-4));
- else
- songname=infile(1:(end-4));
- end
- beattime=8;
- %b = mushroom = drum
- %p = star = piano
- %l = airplane = guitar
- instr={'p','p','p','p','l','l','l','l'};
- %usually 4 channels. channel 2 seems to be channel 1 but offset...
- % wantncs=[1 2 3]; %use nc if you want all instruments
- wantncs=[1 2 3 4]; %use [0 1 2 9] if you want all instruments
- midi=readmidi(infile);
- N = midiInfo(midi,0);
- clc;
- %how many frames each beat lasts... rounds each beat to fit. use 1 for 1 beat per frame
- %you probably have to check each song separately
- bpm=round(60*60/beattime); %always this value for NES games, one per frame
- %convert start time to frames = takt
- % F1=N(
- %
- % %find music speed
- % Nd=N(2:end,5)-N(1:(end-1),5);
- % Nd(Nd==0)=[];
- %
- % t=min(Nd);
- %get all channels
- C=unique(N(:,2));
- nc=numel(C);
- %create main matrix F
- totf=round(60*max(N(:,6)));
- numf=ceil(totf/beattime);
- F=NaN(numf,nc); %0 if channel not played on this frame, otherwise midi note
- %ok, loop through N now, row per row
- n=size(F,1);
- F1=round(60*N(:,5)/beattime)+1;
- F2=round(60*N(:,6)/beattime)+1;
- ns=size(N,1);
- for i=1:ns
- nowch=N(i,2);
- nownote=N(i,3);
- nowcol=find(C==nowch);
- f1=F1(i);
- f2=F2(i);
- if f2>=f1
- F(f1,nowcol)=nownote;
- end
- end
- L=cell(72,1);
- L(72)={'a'}; %C
- L(71)={'b'}; %B
- L(70)={'c#'}; %A#
- L(69)={'c'}; %A
- L(68)={'d#'}; %G#
- L(67)={'d'}; %G
- L(66)={'e#'}; %F#
- L(65)={'e'}; %F
- L(64)={'f'}; %E
- L(63)={'g#'}; %D#
- L(62)={'g'}; %D
- L(61)={'h#'}; %C#
- L(60)={'h'}; %C
- L(59)={'i'}; %B
- L(58)={'j#'}; %A#
- L(57)={'j'}; %A
- L(56)={'k#'}; %G#
- L(55)={'k'}; %G
- L(54)={'l#'}; %F#
- L(53)={'l'}; %F
- L(52)={'m'}; %E
- L(51)={'n#'}; %D#
- L(50)={'n'}; %D
- L(49)={'o#'}; %C#
- L(48)={'o'}; %C
- L(47)={'p'}; %B
- %ok, all music is stored now. dump to output file!
- % outfile=['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' outname '.txt'];
- fidarr=fopen(['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' songname ']MarioPaint.txt'],'w');
- arrlist='C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\MarioPaintArrList.txt';
- fidnew=fopen(arrlist,'r');
- t=fgetl(fidnew);
- s0=strfind(t,songname);
- fclose(fidnew);
- if numel(s0)==0
- fid2=fopen(arrlist,'a');
- fprintf(fid2,[songname ']MarioPaint*']);
- fclose(fid2);
- end
- arr_end=']MarioPaint';
- nowfile_ind=1;
- outfile=['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' songname num2str2(nowfile_ind) arr_end '.txt'];
- fprintf(fidarr,[songname num2str2(nowfile_ind) '\r']);
- fid=fopen(outfile,'w');
- beatstr='4/4*';
- fprintf(fid,beatstr);
- %letter a = 72 in midi data, high C
- %a=C3
- %b=B3
- %c=A3
- %d=G2
- %e=F2
- %f=E2
- %g=D2
- %h=C2
- %i=B2
- %j=A2
- %k=G1
- %l=F1
- %m=E1
- %n=D1
- %o=C1
- %slutar på ton B
- % if nc>6
- % error('too many channels');
- % end
- % n=128; %testing
- %3 = drumroll usually. d, star, seems to work well
- % n=600;
- totcols=0;
- for i1=1:n %for all frames
- k=0;
- % tempstr=[];
- for i2=wantncs %for all channels
- v=F(i1,i2);
- if isfinite(v) %if a note on this frame
- if v<47
- % vold=v;
- v=v+12*ceil((47-v)/12);
- % disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
- end
- if v>72
- % vold=v;
- v=v-12*ceil((v-72)/12);
- % disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
- end
- % tempstr=[tempstr char(instr(i2)) char(L(v)) '+'];
- fprintf(fid,[char(instr(i2)) char(L(v)) '+']);
- k=k+1;
- end
- end
- % if k>0
- % if k>0
- fprintf(fid,[repmat('+',1,6-k) 'q:']);
- % else
- % fprintf(fid,':::::');
- % end
- % else
- % fprintf(fid,':');
- % end
- totcols=totcols+1;
- if totcols==384
- fprintf(fid,['"%%' num2str(bpm)]);
- fclose(fid);
- nowfile_ind=nowfile_ind+1;
- outfile=['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' songname num2str2(nowfile_ind) arr_end '.txt'];
- fprintf(fidarr,[songname num2str2(nowfile_ind) '\r']);
- disp(['now on file ' outfile]);
- fid=fopen(outfile,'w');
- fprintf(fid,beatstr);
- totcols=0;
- end
- end
- % if totcols>384
- % disp(['Total beats is ' num2str(totcols) '. All after 384 has been cut off']);;
- % end
- maxc=384*ceil(totcols/384);
- % if totcols<384
- % maxc=384;
- % elseif totcols<768
- % maxc=768;
- % end
- fprintf(fid,repmat(':',1,maxc-totcols));
- % %last note...
- % fprintf(fid,[':"' num2str(bpm) ':']); %for some reason (?)
- %
- % %last note
- % k=0;
- % for i2=1:nc %for all channels
- % v=F(end,i2);
- % if isfinite(v) %if a note on this frame
- % if v<47
- % % vold=v;
- % v=v+12*ceil((47-v)/12);
- % % disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
- % end
- %
- % if v>72
- % % vold=v;
- % v=v-12*ceil((v-72)/12);
- % % disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
- % end
- % % v
- % fprintf(fid,[char(96+i2) char(L(v)) '+']);
- % k=k+1;
- % end
- % end
- % fprintf(fid,repmat('+',1,6-k));
- fprintf(fid,['"%%' num2str(bpm)]);
- fclose(fid);
- fclose(fidarr);
- fclose all;
- % winopen(outfile);
- % 1 track number
- % 2 channel number
- % 3 note number (midi encoding of pitch)
- % 4 velocity
- % 5 start time (seconds)
- % 6 end time (seconds)
- % message number of note_on
- % message number of note_off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement