Advertisement
Guest User

MIDI2MPC

a guest
Apr 28th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.92 KB | None | 0 0
  1. % infile='C:\Musik\MIDI_archive\lolo2.mid';
  2. infile='C:\Musik\NSF\Kid Icarus 01.mid';
  3.  
  4. q=find(infile=='\');
  5.  
  6. if numel(q)>0
  7.     songname=infile((q(end)+1):(end-4));
  8. else
  9.     songname=infile(1:(end-4));
  10. end
  11.  
  12. beattime=8;
  13.  
  14. %b = mushroom = drum
  15. %p = star = piano
  16. %l = airplane = guitar
  17. instr={'p','p','p','p','l','l','l','l'};
  18.  
  19. %usually 4 channels. channel 2 seems to be channel 1 but offset...
  20. % wantncs=[1 2 3]; %use nc if you want all instruments
  21. wantncs=[1 2 3 4]; %use [0 1 2 9] if you want all instruments
  22.  
  23. midi=readmidi(infile);
  24. N = midiInfo(midi,0);
  25.  
  26. clc;
  27.  
  28. %how many frames each beat lasts... rounds each beat to fit. use 1 for 1 beat per frame
  29. %you probably have to check each song separately
  30.  
  31. bpm=round(60*60/beattime); %always this value for NES games, one per frame
  32.  
  33. %convert start time to frames = takt
  34. % F1=N(
  35. %
  36. % %find music speed
  37.  
  38. % Nd=N(2:end,5)-N(1:(end-1),5);
  39. % Nd(Nd==0)=[];
  40. %
  41. % t=min(Nd);
  42.  
  43.  
  44. %get all channels
  45. C=unique(N(:,2));
  46.  
  47. nc=numel(C);
  48.  
  49. %create main matrix F
  50. totf=round(60*max(N(:,6)));
  51.  
  52. numf=ceil(totf/beattime);
  53.  
  54. F=NaN(numf,nc); %0 if channel not played on this frame, otherwise midi note
  55.  
  56. %ok, loop through N now, row per row
  57.  
  58. n=size(F,1);
  59.  
  60. F1=round(60*N(:,5)/beattime)+1;
  61. F2=round(60*N(:,6)/beattime)+1;
  62.  
  63. ns=size(N,1);
  64.  
  65. for i=1:ns
  66.     nowch=N(i,2);
  67.     nownote=N(i,3);
  68.     nowcol=find(C==nowch);
  69.    
  70.     f1=F1(i);
  71.     f2=F2(i);
  72.    
  73.     if f2>=f1
  74.         F(f1,nowcol)=nownote;
  75.     end
  76. end
  77.  
  78.  
  79. L=cell(72,1);
  80. L(72)={'a'}; %C
  81. L(71)={'b'}; %B
  82. L(70)={'c#'}; %A#
  83. L(69)={'c'}; %A
  84. L(68)={'d#'}; %G#
  85. L(67)={'d'}; %G
  86. L(66)={'e#'}; %F#
  87. L(65)={'e'}; %F
  88. L(64)={'f'}; %E
  89.  
  90. L(63)={'g#'}; %D#
  91. L(62)={'g'}; %D
  92.  
  93. L(61)={'h#'}; %C#
  94. L(60)={'h'}; %C
  95.  
  96. L(59)={'i'}; %B
  97.  
  98. L(58)={'j#'}; %A#
  99. L(57)={'j'}; %A
  100.  
  101. L(56)={'k#'}; %G#
  102. L(55)={'k'}; %G
  103.  
  104. L(54)={'l#'}; %F#
  105. L(53)={'l'}; %F
  106.  
  107. L(52)={'m'}; %E
  108.  
  109. L(51)={'n#'}; %D#
  110. L(50)={'n'}; %D
  111.  
  112. L(49)={'o#'}; %C#
  113. L(48)={'o'}; %C
  114.  
  115. L(47)={'p'}; %B
  116.  
  117.  
  118. %ok, all music is stored now. dump to output file!
  119.  
  120. % outfile=['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' outname '.txt'];
  121.  
  122. fidarr=fopen(['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' songname ']MarioPaint.txt'],'w');
  123.  
  124. arrlist='C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\MarioPaintArrList.txt';
  125.  
  126. fidnew=fopen(arrlist,'r');
  127. t=fgetl(fidnew);
  128. s0=strfind(t,songname);
  129. fclose(fidnew);
  130. if numel(s0)==0
  131.     fid2=fopen(arrlist,'a');
  132.     fprintf(fid2,[songname ']MarioPaint*']);
  133.     fclose(fid2);
  134. end
  135.  
  136. arr_end=']MarioPaint';
  137.  
  138. nowfile_ind=1;
  139. outfile=['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' songname num2str2(nowfile_ind) arr_end '.txt'];
  140. fprintf(fidarr,[songname num2str2(nowfile_ind) '\r']);
  141. fid=fopen(outfile,'w');
  142. beatstr='4/4*';
  143.  
  144. fprintf(fid,beatstr);
  145.  
  146.  
  147.  
  148. %letter a = 72 in midi data, high C
  149.  
  150.  
  151. %a=C3
  152. %b=B3
  153. %c=A3
  154. %d=G2
  155. %e=F2
  156. %f=E2
  157. %g=D2
  158. %h=C2
  159. %i=B2
  160. %j=A2
  161. %k=G1
  162. %l=F1
  163. %m=E1
  164. %n=D1
  165. %o=C1
  166.  
  167. %slutar på ton B
  168.  
  169.  
  170. % if nc>6
  171. %     error('too many channels');
  172. % end
  173.  
  174.  
  175. % n=128; %testing
  176.  
  177.  
  178.  
  179. %3 = drumroll usually. d, star, seems to work well
  180.  
  181.  
  182.  
  183.  
  184.  
  185. % n=600;
  186.  
  187. totcols=0;
  188. for i1=1:n %for all frames
  189.     k=0;
  190.     %     tempstr=[];
  191.     for i2=wantncs %for all channels
  192.         v=F(i1,i2);
  193.        
  194.        
  195.         if isfinite(v) %if a note on this frame
  196.             if v<47
  197.                 %                             vold=v;
  198.                 v=v+12*ceil((47-v)/12);
  199.                 %                             disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
  200.             end
  201.            
  202.             if v>72
  203.                 %             vold=v;
  204.                 v=v-12*ceil((v-72)/12);
  205.                 %             disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
  206.             end
  207.             %             tempstr=[tempstr char(instr(i2)) char(L(v)) '+'];
  208.             fprintf(fid,[char(instr(i2)) char(L(v)) '+']);
  209.             k=k+1;
  210.         end
  211.     end
  212.     %     if k>0
  213.     %     if k>0
  214.     fprintf(fid,[repmat('+',1,6-k) 'q:']);
  215.     %     else
  216.     %         fprintf(fid,':::::');
  217.     %     end
  218.     %     else
  219.     %         fprintf(fid,':');
  220.     %     end
  221.     totcols=totcols+1;
  222.     if totcols==384
  223.         fprintf(fid,['"%%' num2str(bpm)]);
  224.         fclose(fid);
  225.         nowfile_ind=nowfile_ind+1;
  226.         outfile=['C:\Spel\mariopaintcomposerpc\Mario Paint Composer PC\Prefs\' songname num2str2(nowfile_ind) arr_end '.txt'];
  227.         fprintf(fidarr,[songname num2str2(nowfile_ind)  '\r']);
  228.         disp(['now on file ' outfile]);
  229.         fid=fopen(outfile,'w');
  230.         fprintf(fid,beatstr);
  231.         totcols=0;
  232.     end
  233. end
  234.  
  235. %     if totcols>384
  236. %         disp(['Total beats is ' num2str(totcols) '. All after 384 has been cut off']);;
  237. %     end
  238.  
  239. maxc=384*ceil(totcols/384);
  240.  
  241. % if totcols<384
  242. %     maxc=384;
  243. % elseif totcols<768
  244. %     maxc=768;
  245. % end
  246.  
  247. fprintf(fid,repmat(':',1,maxc-totcols));
  248.  
  249. % %last note...
  250. % fprintf(fid,[':"' num2str(bpm) ':']); %for some reason (?)
  251. %
  252. % %last note
  253. % k=0;
  254. % for i2=1:nc %for all channels
  255. %     v=F(end,i2);
  256. %     if isfinite(v) %if a note on this frame
  257. %         if v<47
  258. % %                         vold=v;
  259. %             v=v+12*ceil((47-v)/12);
  260. % %                         disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
  261. %         end
  262. %
  263. %         if v>72
  264. % %                         vold=v;
  265. %             v=v-12*ceil((v-72)/12);
  266. % %                         disp(['v old: ' num2str(vold) ' v new ' num2str(v)]);
  267. %         end
  268. % %         v
  269. %         fprintf(fid,[char(96+i2) char(L(v)) '+']);
  270. %         k=k+1;
  271. %     end
  272. % end
  273. % fprintf(fid,repmat('+',1,6-k));
  274. fprintf(fid,['"%%' num2str(bpm)]);
  275. fclose(fid);
  276. fclose(fidarr);
  277.  
  278.  
  279.  
  280. fclose all;
  281. % winopen(outfile);
  282.  
  283. %     1 track number
  284. %     2 channel number
  285. %     3 note number (midi encoding of pitch)
  286. %     4 velocity
  287. %     5 start time (seconds)
  288. %     6 end time (seconds)
  289. %     message number of note_on
  290. %     message number of note_off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement