Advertisement

03.06.2008 at 06:04PM PST, ID: 23221851 | Points: 500
[x]
Attachment Details

I need to convert Matlab (binary write) code to Java

Tags: Matlab, Java
I am working on a program that automatically creates a midi file. The program is currently written and working in Matlab but my professor wants it in java so that it can run on a computer without Matlab and for the GUI advantages of java.

I have already translated most of the code, but I am very confused about the file that writes the midi file. I have attached the Matlab code for the function that writes the midi file.

I am relatively new to Java and don't know where to start in converting this code. Any help would be greatly appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
function [] = write_midi(melody_notes,note_durations,output_file,key_signature,key_mode,time_signature);
% Synopsis:
% Write the MIDI file
% melody_notes: the sequence of melody notes, in standard MIDI pitches
% note_durations: the sequence of note duration, scaled as 480 ticks per quarter note.
% output_files: name of the MIDI output file
% key_signature: for example 'C', 'G', 'Bb' and such
% key_mode: 0 for major, 1 for minor
% time_signature: first value is numerator, second value is denominator
 
fid = fopen(output_file,'wb') % open the output file for writing
 
nbytes = 21 + 9*length(melody_notes) % number of bytes in the MIDI file
 
% prepare the number of bytes in a format for writing into the MIDI file
byte_values = zeros(1,4)
for i = 1:4,
    byte_values(i) = mod(nbytes,256)
    nbytes = (nbytes - byte_values(i))/256
end
 
% derive the key signature for writing to the MIDI file
 
[n_acc,junk] = keyname_convert(key_signature,key_mode); %keyname_convert returns an int for both n_acc and junk. This method is already written
 
fwrite(fid,'MThd','uint8');
for i = 1:3, fwrite(fid,0,'uint8'); end
fwrite(fid,6,'uint8');
for i = 1:3, fwrite(fid,0,'uint8'); end
fwrite(fid,1,'uint8');
fwrite(fid,1,'uint8'); fwrite(fid,224,'uint8'); % defines 480 ticks per quarter notes
fwrite(fid,'MTrk','uint8');
for i = 1:4, fwrite(fid,byte_values(5-i),'uint8'); end % the number of bytes
fwrite(fid,0,'uint8'); fwrite(fid,192,'uint8'); fwrite(fid,26,'uint8'); % program change channel 1
fwrite(fid,0,'uint8'); fwrite(fid,255,'uint8'); fwrite(fid,89,'uint8'); fwrite(fid,2,'uint8'); % key signature
fwrite(fid,n_acc,'int8'); fwrite(fid,key_mode,'uint8'); % key signatue continued (no. of sharps/flats, major/minor mode)
fwrite(fid,0,'uint8'); fwrite(fid,255,'uint8'); fwrite(fid,88,'uint8'); fwrite(fid,4,'uint8'); % time signature
fwrite(fid,time_signature(1),'uint8'); fwrite(fid,log2(time_signature(2)),'uint8'); fwrite(fid,24,'uint8'); fwrite(fid,8,'uint8'); % time signature continued
for i = 1:length(melody_notes), % write the notes
    fwrite(fid,0,'uint8'); fwrite(fid,144,'uint8'); fwrite(fid,melody_notes(i),'uint8'); fwrite(fid,64,'uint8'); %note on
    boff = varlen(note_durations(i));
    fwrite(fid,boff(1),'uint8'); fwrite(fid,boff(2),'uint8'); fwrite(fid,128,'uint8'); fwrite(fid,melody_notes(i),'uint8'); fwrite(fid,0,'uint8'); % note off
end
fwrite(fid,0,'uint8'); fwrite(fid,255,'uint8'); fwrite(fid,47,'uint8'); fwrite(fid,0,'uint8'); % end of track
fclose(fid)
 
 
% The above code calls the "varlen" function. This is given here:
 
function b = varlen(n);
% Synopsis: b = varlen(n).
% Convert an unsigned integer to a two-byte variable length MIDI format.
% n is assumed smaller than 2^14 - 1.
 
b = zeros(1,2);
b(2) = rem(n,128);
b(1) = (n - b(2))/128 + 128;
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: kmswim03
Question Asked On: 03.06.2008
Participating Experts: 1
Points: 500
Views: 0
Translate:
Loading Advertisement...
03.06.2008 at 11:27PM PST, ID: 21068025

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.07.2008 at 06:51AM PST, ID: 21070574

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080236-EE-VQP-29 / EE_QW_2_20070628