I'm trying to create a MIDI reading program in Java. The goal is to save the notes from the MIDI file and the notes' time signatures using each notes' ticks and subtracting them to find the difference of the values, then convert them into corresponding time values.
My program's example output would be:
Tick at @27576
Channel: 2
Note = AS1 key = 34
Tick at @27600
Channel: 2
Note = AS1 key = 34
Tick at @27624
Channel: 2
Note = AS1 key = 34
Tick at @29952
//and so on
The tick values would then be inserted into an ArrayList called noteTimings
and the note values would be inserted into an ArrayList called noteKeyValues
So, in the example output - noteTimings
would have the values: [27576, 27600, 27624, 29952]
Now, what I'm trying to accomplish is subtracting the last element with the preceding one (eg 29952 - 27624) and insert that value into a new ArrayList. This will go on until every element has been iterated through in the for loop.
My for loop:
ArrayList<Integer> newNoteTimings = new ArrayList<>();
for (int i = noteTimings.size() - 1; i >= 1; i--) {
for (int j = i - 1; j >= 0; j--) {
newNoteTimings.add((noteTimings.get(i) - noteTimings.get(j)));
}
}
System.out.println(newNoteTimings);
Expected results:
2328
24
24
Actual results:
2328
2352
2376
Is there something I'm overlooking? Any help would be appreciated!
You can reverse the list and perform the subtraction from the beginning, e.g.:
List<Integer> list = new ArrayList<>();
list.add(27576);
list.add(27600);
list.add(27624);
list.add(29952);
//Reverse the list
Collections.reverse(list);
List<Integer> result = new ArrayList<>();
for(int i=0 ; i<list.size() - 1 ; i++) {
result.add(list.get(i) - list.get(i+1));
}
System.out.println(result);