use of com.xenoage.zong.core.music.time.TimeSignature in project Zong by Xenoage.
the class MidiConverter method writeMetronomeTrack.
/**
* Writes the metronome beats into the metronome track.
*/
private void writeMetronomeTrack(int track) {
int strongBeatNote = options.midiSettings.metronomeStrongBeatNote;
int weakBeatNote = options.midiSettings.metronomeWeakBeatNote;
for (int iRep : range(repetitions)) {
val rep = repetitions.get(iRep);
for (int iMeasure : range(rep.start.getMeasure(), rep.end.getMeasure())) {
TimeSignature timeSig = score.getHeader().getTimeAtOrBefore(iMeasure);
Fraction startBeat = (rep.start.getMeasure() == iMeasure ? rep.start.getBeat() : Companion.get_0());
Fraction endBeat = (rep.end.getMeasure() == iMeasure ? rep.end.getBeat() : score.getMeasureBeats(iMeasure));
if (timeSig != null) {
boolean[] accentuation = timeSig.getType().getBeatsAccentuation();
int timeDenominator = timeSig.getType().getDenominator();
long measureStartTick = timeMap.getByRepTime(iRep, Companion.time(iMeasure, Companion.get_0())).tick;
for (int beatNumerator : range(timeSig.getType().getNumerator())) {
// compute start and stop tick
val beat = Companion.fr(beatNumerator, timeDenominator);
val time = Companion.time(iMeasure, beat);
if (false == rep.contains(time))
continue;
long tickStart = measureStartTick + durationToTick(Companion.fr(beatNumerator, timeDenominator), resolution);
long tickStop = tickStart + durationToTick(Companion.fr(1, timeDenominator), resolution);
// write metronome note
int note = (accentuation[beatNumerator] ? strongBeatNote : weakBeatNote);
int velocity = midiMaxValue;
writer.writeNote(track, channel10, tickStart, note, true, velocity);
writer.writeNote(track, channel10, tickStop, note, false, 0);
}
}
}
}
}
use of com.xenoage.zong.core.music.time.TimeSignature in project Zong by Xenoage.
the class TimeMapperTest method getScore.
/**
* Creates the following score (each space is a quarter note)
*
* measures:
* repetitions: voltas/signs: 1───┐2───┐ d.c. senza rep
* barlines: | |: | :| |1 ||
* part 0: staff 0: voice 0: |1 |1 |2 2 |1 |1 ||
* voice 1: | |442 |42 4|1 d |1 || //d = direction
* staff 1: voice 0: |2 44|1 | |1 |1 ||
* part 1: staff 2: voice 0: |1 |1 |2 2 | |2 2 ||
*
* play ranges: 0---------------
* 1---- 2---------
* 3--------- 4---------
*/
private Score getScore() {
val score = new Score();
new PartAdd(score, new Part("p0", null, 2, null), 0, null).execute();
new PartAdd(score, new Part("p1", null, 1, null), 1, null).execute();
new MeasureAdd(score, 5).execute();
// time, barlines, voltas and signs
score.getColumnHeader(0).setTime(new TimeSignature(TimeType.Companion.getTime_4_4()));
score.getColumnHeader(1).setStartBarline(Companion.barlineForwardRepeat(Regular));
score.getColumnHeader(2).setVolta(new Volta(1, range(1, 1), "1", true));
score.getColumnHeader(2).setEndBarline(Companion.barlineBackwardRepeat(Regular, 1));
score.getColumnHeader(3).setVolta(new Volta(1, range(2, 2), "2", true));
score.getColumnHeader(4).setNavigationOrigin(new DaCapo(false));
// staff 0, voice 0
val cursor = new Cursor(score, mp0, true);
cursor.write(e(1));
cursor.write(e(1));
cursor.write(e(2));
cursor.write(e(2));
cursor.write(e(1));
cursor.write(e(1));
// staff 0, voice 1
cursor.setMp(atElement(0, 1, 1, 0));
cursor.write(e(4));
cursor.write(e(4));
cursor.write(e(2));
cursor.write(e(4));
cursor.write(e(2));
cursor.write(e(4));
cursor.write(e(1));
cursor.write(e(1));
new MeasureElementWrite(new Dynamic(DynamicValue.f), score.getMeasure(atMeasure(0, 3)), Companion.get_1$2()).execute();
// staff 1, voice 0
cursor.setMp(atElement(1, 0, 0, 0));
cursor.write(e(2));
cursor.write(e(4));
cursor.write(e(4));
cursor.write(e(1));
cursor.setMp(atElement(1, 2, 0, 0));
cursor.write(e(1));
cursor.write(e(1));
// staff 2, voice 0
cursor.setMp(atElement(2, 0, 0, 0));
cursor.write(e(1));
cursor.write(e(1));
cursor.write(e(2));
cursor.write(e(2));
cursor.setMp(atElement(2, 4, 0, 0));
cursor.write(e(2));
cursor.write(e(2));
return score;
}
use of com.xenoage.zong.core.music.time.TimeSignature in project Zong by Xenoage.
the class VoiceElementWriteTest method testTimeAware.
/**
* Obey the time signature. Fail is written element is too long.
*/
@Test
public void testTimeAware() {
Score score = ScoreFactory.create1Staff4Measures();
score.getHeader().getColumnHeaders().get(1).setTime(new TimeSignature(TimeType.Companion.getTime_3_4()));
// create options
VoiceElementWrite.Options options = new VoiceElementWrite.Options();
options.checkTimeSignature = true;
// measure 0: senza misura: write 100 1/4 chords
Voice voice = score.getVoice(MP.atVoice(0, 0, 0));
for (int i : range(100)) new VoiceElementWrite(voice, atElement(0, 0, 0, i), new Rest(Companion.fr(1, 4)), options).execute();
// measure 2: must work for 3/4, then fail
for (int i : range(3)) new VoiceElementWrite(voice, atElement(0, 2, 0, i), new Rest(Companion.fr(1, 4)), options).execute();
try {
new VoiceElementWrite(voice, atElement(0, 2, 0, 3), new Rest(Companion.fr(1, 4)), options).execute();
fail();
} catch (MeasureFullException ex) {
}
}
use of com.xenoage.zong.core.music.time.TimeSignature in project Zong by Xenoage.
the class ScoreTest method getGapBetweenTest.
@Test
public void getGapBetweenTest() {
Score score = ScoreFactory.create1Staff4Measures();
VoiceElement[] e = new VoiceElement[6];
// 4/4 measure
score.getColumnHeader(0).setTime(new TimeSignature(TimeType.Companion.getTime_4_4()));
// first measure (filled only with 7/8)
// voice 0: 1/4, 1/4, 1/4 (1/4 is missing)
// voice 1: 3/4, 1/8 (1/8 is missing)
score.getVoice(atVoice(0, 0, 0)).addElement(e[0] = new Rest(Companion.fr(1, 4)));
score.getVoice(atVoice(0, 0, 0)).addElement(e[1] = new Rest(Companion.fr(1, 4)));
score.getVoice(atVoice(0, 0, 0)).addElement(e[2] = new Rest(Companion.fr(1, 4)));
new VoiceAdd(score.getMeasure(atMeasure(0, 0)), 1).execute();
score.getVoice(atVoice(0, 0, 1)).addElement(new Rest(Companion.fr(3, 4)));
score.getVoice(atVoice(0, 0, 1)).addElement(new Rest(Companion.fr(1, 8)));
// second measure: 1/4, 3/4
score.getVoice(atVoice(0, 1, 0)).addElement(e[3] = new Rest(Companion.fr(1, 4)));
score.getVoice(atVoice(0, 1, 0)).addElement(e[4] = new Rest(Companion.fr(3, 4)));
// third measure: 4/4
score.getVoice(atVoice(0, 1, 0)).addElement(e[5] = new Rest(Companion.fr(4, 4)));
// test gaps between adjacent elements
assertEquals(Companion.get_0(), score.getGapBetween(e[0], e[1]));
assertEquals(Companion.get_0(), score.getGapBetween(e[1], e[2]));
// 1/8 from second voice
assertEquals(Companion.fr(1, 8), score.getGapBetween(e[2], e[3]));
assertEquals(Companion.get_0(), score.getGapBetween(e[3], e[4]));
assertEquals(Companion.get_0(), score.getGapBetween(e[4], e[5]));
// test some gaps between non-adjacent elements
assertEquals(Companion.fr(1, 4), score.getGapBetween(e[0], e[2]));
// includes 1/8 from second voice
assertEquals(Companion.fr(3, 8), score.getGapBetween(e[1], e[3]));
// includes 1/8 from second voice
assertEquals(Companion.fr(3, 8), score.getGapBetween(e[2], e[4]));
// includes 1/8 from second voice
assertEquals(Companion.fr(5, 8).add(Companion.fr(4, 4)), score.getGapBetween(e[0], e[5]));
// includes 1/8 from second voice
assertEquals(Companion.fr(3, 8).add(Companion.fr(4, 4)), score.getGapBetween(e[1], e[5]));
// test in reverse direction
assertEquals(Companion.fr(-2, 4), score.getGapBetween(e[1], e[0]));
assertEquals(Companion.fr(-2, 4), score.getGapBetween(e[2], e[1]));
// includes 1/8 from second voice
assertEquals(Companion.fr(-5, 8), score.getGapBetween(e[3], e[2]));
// includes 1/8 from second voice
assertEquals(Companion.fr(-23, 8), score.getGapBetween(e[5], e[0]));
// includes 1/8 from second voice
assertEquals(Companion.fr(-21, 8), score.getGapBetween(e[5], e[1]));
}
use of com.xenoage.zong.core.music.time.TimeSignature in project Zong by Xenoage.
the class ScoreHeaderTest method getTimeAtOrBeforeTest.
@Test
public void getTimeAtOrBeforeTest() {
// create a little test score, where there is no time signature
// in the first measure, but a 3/4 time in the second one
// and a 4/4 in the fourth one
Score score = ScoreFactory.create1Staff4Measures();
ScoreHeader header = score.getHeader();
TimeSignature time1 = new TimeSignature(TimeType.Companion.getTime_3_4());
header.getColumnHeader(1).setTime(time1);
TimeSignature time2 = new TimeSignature(TimeType.Companion.getTime_4_4());
header.getColumnHeader(3).setTime(time2);
// test method
assertEquals(TimeSignature.Companion.getImplicitSenzaMisura(), header.getTimeAtOrBefore(0));
assertEquals(time1, header.getTimeAtOrBefore(1));
assertEquals(time1, header.getTimeAtOrBefore(2));
assertEquals(time2, header.getTimeAtOrBefore(3));
}
Aggregations