Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  MIDI note duration  (Read 2649 times)
0 Members and 1 Guest are viewing this topic.
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Posted 2010-12-28 14:41:57 »

Hi,

I try to deal with MIDI music. I load a midi file with javax.sound.midi.* classes and I want to convert them in my own format. Basicly I want the time when the note begin and the duration of the note. The code below works for some midi but for most of them, duration of note are wrong (seems like playing for ever). I don't understand what I have miss  Huh.

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  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
public static Score load(InputStream is) throws InvalidMidiDataException, IOException
  {
    Score score = new Score();

    Sequence sequence = MidiSystem.getSequence(is);
    long totalTick = sequence.getTickLength();
    long totalTime = sequence.getMicrosecondLength();

    float fDivisionType = sequence.getDivisionType();

    if (fDivisionType == Sequence.PPQ)
    {
      System.out.println("PPQ");
    }
    else if (fDivisionType == Sequence.SMPTE_24)
    {
      System.out.println("SMPTE, 24 frames per second");
    }
    else if (fDivisionType == Sequence.SMPTE_25)
    {
      System.out.println("SMPTE, 25 frames per second");
    }
    else if (fDivisionType == Sequence.SMPTE_30DROP)
    {
      System.out.println("SMPTE, 29.97 frames per second");
    }
    else if (fDivisionType == Sequence.SMPTE_30)
    {
      System.out.println("SMPTE, 30 frames per second");
    }

    if (sequence.getDivisionType() == Sequence.PPQ)
    {
      System.out.println(" ticks per beat");
    }
    else
    {
      System.out.println(" ticks per frame");
    }

    Track[] tracks = sequence.getTracks();
    for (int nTrack = 0; nTrack < tracks.length; nTrack++)
    {
      Staff staff = score.createStaff();
      Track   track = tracks[nTrack];

      for (int nEvent = 0; nEvent < track.size(); nEvent++)
      {
   MidiEvent event = track.get(nEvent);
        long time = event.getTick();
        MidiMessage message = event.getMessage();

        if (message instanceof ShortMessage)
   {
     ShortMessage message2 = (ShortMessage) message;

          if(message2.getCommand() == ShortMessage.PROGRAM_CHANGE)
          {
            staff.setInstrument(message2.getData1());
          }
          else if(message2.getCommand() == ShortMessage.NOTE_ON)
          {
            int midi = message2.getData1();
            int velocity = message2.getData2();

            if(message2.getChannel() == Staff.CHANNEL_DRUMS) { staff.setInstrument(Staff.DRUMS);}

            int i = staff.getNotesCount()-1;
            while((i>=0)&&(staff.getNote(i).getPitch() != midi)) { i--; }

            if(i>=0)
            {
              Note n = staff.getNote(i);
              if(n.getDuration() == -1)
              {
                n.setDuration((int)(time-n.getTime()));
              }
            }

            Note note = staff.createNote();
            note.setPitch(midi);
            //note.setVelocity(velocity);
           note.setTime((int)time);
            note.setDuration(-1);

            staff.validate();
          }
          else if(message2.getCommand() == ShortMessage.NOTE_OFF)
          {
            int midi = message2.getData1();
            int velocity = message2.getData2();

            int i = staff.getNotesCount()-1;
            while((i>=0)&&(staff.getNote(i).getPitch() != midi)) { i--; }

            if(i>=0)
            {
              Note n = staff.getNote(i);
              if(n.getDuration() == -1)
              {
                n.setDuration((int)(time-n.getTime()));
              }
            }
           
            staff.validate();
          }
          else if(message2.getCommand() == ShortMessage.PITCH_BEND)
          {
            // Pitch bend : skip
         }
          else if(message2.getCommand() == ShortMessage.CONTROL_CHANGE)
          {
            int control = message2.getData1();

            if(control == 0x06)
            {
              // Data Entry (coarse) : skip ?
           }
            else if(control == 0x07)
            {
              // Volume (coarse) : skip
           }
            else if(control == 0x0A)
            {
              // Pan (coarse) : skip
           }
            else if(control == 0x64)
            {
              // Registered Param LSB : skip ?
           }
            else if(control == 0x65)
            {
              // Registered Param MSB : skip ?
           }
            else
            {
              System.out.println("Control : "+String.format("%02x",control));
            }
          }
          else
          {
            System.out.println("ShortMessage : "+String.format("%02x",message2.getCommand()));
          }
        }
        else if(message instanceof MetaMessage)
        {
          MetaMessage message2 = (MetaMessage)message;

          if(message2.getType() == 0x2F)
          {
            for(int i=0;i<staff.getNotesCount();i++)
            {
              Note n = staff.getNote(i);
              if(n.getDuration() == -1)
              {
                n.setDuration((int)(time-n.getTime()));
              }
            }
          }
          else if(message2.getType() == 0x51)
          {
            byte[]   abData = message2.getData();

            int   nTempo = ((abData[0] & 0xFF) << 16) | ((abData[1] & 0xFF) << 8) | (abData[2] & 0xFF);
       score.setBPM( (int)convertTempo(nTempo));
          }
          else if(message2.getType() == 0x03)
          {
            String str = new String(message2.getData());

            System.out.println("Name : "+str);
          }
          else if(message2.getType() == 0x7F)
          {
            // Proprietary Event : skip
         }
          else if(message2.getType() == 0x59)
          {
            // Key Signature : skip
         }
          else if(message2.getType() == 0x20)
          {
            // MIDI Channel : ?
         }
          else if(message2.getType() == 0x21)
          {
            // MIDI Port : skip
         }
          else if(message2.getType() == 0x01)
          {
            // Text : skip
         }
          else if(message2.getType() == 0x58)
          {
            // Time Signature : skip
         }
          else
          {
            System.out.println("MetaMessage : "+String.format("%02x", message2.getType()));
          }
        }
      }

      long baseTime = (60l*1000000l)/score.getBPM()/Note.QUARTER;

      for(int i=0;i<staff.getNotesCount();i++)
      {
        Note n = staff.getNote(i);

        // Dirty hack -_-

        long time = n.getTime();
        long duration = n.getDuration();

        long t = (time*totalTime)/totalTick;
        time = (int)(t / baseTime);
       
        t = (duration*totalTime)/totalTick;
        duration = (int)(t / baseTime);

        n.setTime((int)time);
        n.setDuration((int)duration);
      }
    }

    // Converte DRUMs channels

    ArrayList<Staff> drums = new ArrayList<Staff>();

    for(int i=0;i<score.getStaffsCount();i++)
    {
      Staff st = score.getStaff(i);

      if(st.getInstrument() == Staff.DRUMS)
      {
        score.removeStaff(st);
        drums.add(st);
      }
    }

    for(int i=0;i<drums.size();i++)
    {
      Staff st = drums.get(i);

      for(int j = 0;j<st.getNotesCount();j++)
      {
        Note n = st.getNote(j);

        Staff sd = score.getStaffWithInstrumen(Staff.DRUMS+n.getPitch());

        if(sd == null)
        {
          sd = score.createStaff();
          sd.setInstrument(Staff.DRUMS+n.getPitch());
        }

        Note nd = sd.createNote();
        nd.setPitch(  DRUM_NOTE[n.getPitch()] );
        nd.setDuration( n.getDuration() );
        nd.setTime( n.getTime() );
      }
    }

    score.validate();
    return score;
  }
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #1 - Posted 2010-12-28 17:37:55 »

... why I allways find out the problem - after several days of search - right after I post in this forum...  Grin

OK. I just find out that a NoteOn event with a velocity of 0 is in fact a NoteOff. Of course now it sounds far better  Wink
Offline ShannonSmith
« Reply #2 - Posted 2010-12-28 17:49:42 »

I was burned by this myself a few times. It was done for bandwidth optimisation reasons as you could send a series of NoteOn's without having to send the message type byte for each one.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!