How to combine tabs #654
-
UPDATE: After learning the basics of Midi, I somehow managed to make it work. My initial approach causes the two tracks to be in the same midi channel, and that's why the instruments sounded wrong. Fixing the channels solved my issue. My approach is kinda hacky so I'll try diving into the code for a more proper way. ORIGINAL: So far I tried to load the two tabs individually, and then push the second tab's tracks to the first tab (see pseudo code below for example).
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Sorry for the late answer, it seems I somehow missed this question completely. Combining the tracks of different files might not be so easy. There are quite some things which are expected to be consistent across the tracks. One main rule is: the number of bars within the staves of a track must be the same as the number of masterbars: https://alphatab.net/docs/reference/score Here an untested example of how you could organize your code: function createEmptyBar(staff) {
const bar = new Bar();
staff.addBar(bar);
let voiceCount = 1;
if(bar.index > 0) {
bar.clef = bar.previousBar.clef;
voiceCount = bar.previousBar.voices.length;
}
for(let i = 0; i < voiceCount) {
const voice = new Voice();
bar.addVoice(voice);
const beat = new Beat();
beat.isEmpty = true;
voice.addBeat(beat);
}
return bar;
}
function mergeTrack(target, sourceTrack) {
// consolidate track
for(const staff in sourceTrack.staves) {
if(staff.bars.length > target.masterBars.length) {
// too many bars, need to truncate
staff.bars.length = target.masterBars.length;
} else {
// too less bars, need to create some empty ones
while(staff.bars.length < target.masterBars.length) {
addEmptyBar(staff);
}
}
// fix other things like midi channel
}
target.addTrack(sourceTrack);
}
function mergeScores(settings, target, source) {
for(const sourceTrack of source.tracks) {
mergeTrack(target, sourceTrack); // merge track by track
}
target.finish(settings); // important to call to reconsolidate the data model.
} Or an easier alternative could be to send the scores through the JSON serializer. There you could work more on a simplified JSON base and the serializer takes already care of some consolidation steps. Might cost more CPU though. https://alphatab.net/docs/guides/lowlevel-apis#serialize-data-model-fromto-json |
Beta Was this translation helpful? Give feedback.
Sorry for the late answer, it seems I somehow missed this question completely. Combining the tracks of different files might not be so easy. There are quite some things which are expected to be consistent across the tracks. One main rule is: the number of bars within the staves of a track must be the same as the number of masterbars: https://alphatab.net/docs/reference/score
Here an untested example of how you could organize your code: