Skip to content

Commit

Permalink
time の扱いを 秒(double) -> ミリ秒 (long) に変更
Browse files Browse the repository at this point in the history
  • Loading branch information
fourthline committed Aug 9, 2014
1 parent e80dfcf commit e42e05f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/fourthline/mabiicco/ui/MMLSeqView.java
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,14 @@ private void startTimeViewUpdateThread() {
private void updateTimeView() {
long position = pianoRollView.getSequencePlayPosition();
List<MMLTempoEvent> tempoList = mmlScore.getTempoEventList();
double time = MMLTempoEvent.getTimeOnTickOffset(tempoList, (int)position);
long time = MMLTempoEvent.getTimeOnTickOffset(tempoList, (int)position);
int totalTick = mmlScore.getTotalTickLength();
double totalTime = MMLTempoEvent.getTimeOnTickOffset(tempoList, totalTick);
long totalTime = MMLTempoEvent.getTimeOnTickOffset(tempoList, totalTick);
int tempo = MMLTempoEvent.searchOnTick(tempoList, (int)position);

String str = String.format("time %d:%04.1f/%d:%04.1f (t%d)",
(int)(time/60), (time%60),
(int)(totalTime/60), (totalTime%60),
String str = String.format("time %d:%02d.%d/%d:%02d.%d (t%d)",
(time/60/1000), (time/1000%60), (time/100%10),
(totalTime/60/1000), (totalTime/1000%60), (totalTime/100%10),
tempo);
if (timeView != null) {
timeView.setText(str);
Expand Down
34 changes: 29 additions & 5 deletions src/fourthline/mmlTools/MMLTempoEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public static int searchOnTick(List<MMLTempoEvent> tempoList, long tickOffset) {
* 指定したtickオフセット位置の先頭からの時間を返します.
* @param tempoList
* @param tickOffset
* @return 先頭からの時間(
* @return 先頭からの時間(ms
*/
public static double getTimeOnTickOffset(List<MMLTempoEvent> tempoList, int tickOffset) {
double totalTime = 0.0;
public static long getTimeOnTickOffset(List<MMLTempoEvent> tempoList, int tickOffset) {
long totalTime = 0L;

int tempo = INITIAL_TEMPO;
int currentTick = 0;
Expand All @@ -107,18 +107,42 @@ public static double getTimeOnTickOffset(List<MMLTempoEvent> tempoList, int tick

int currentTempo = tempoEvent.getTempo();
if (tempo != currentTempo) {
totalTime += (currentTempoTick - currentTick) * 60 / tempo;
totalTime += (currentTempoTick - currentTick) * 60 / tempo * 1000;
currentTick = currentTempoTick;
}

tempo = currentTempo;
}

totalTime += (tickOffset - currentTick) * 60 / tempo;
totalTime += (tickOffset - currentTick) * 60 / tempo * 1000;
totalTime /= 96.0;
return totalTime;
}

/**
* 指定した時間からtickオフセットを返します.
* @param tempoList
* @param ms
* @return 先頭からの時間(ms)
*/
public static long getTickOffsetOnTime(List<MMLTempoEvent> tempoList, long time) {
int tempo = INITIAL_TEMPO;
long pointTime = 0;
long tick = 0;
for (MMLTempoEvent tempoEvent : tempoList) {
long tempoTime = getTimeOnTickOffset(tempoList, tempoEvent.getTickOffset());
if (time <= tempoTime) {
break;
}
pointTime = tempoTime;
tempo = tempoEvent.getTempo();
tick = tempoEvent.getTickOffset();
}

tick += (time - pointTime) * 96 * tempo / 60 / 1000;
return tick;
}

/**
* テンポリスト中の最大テンポ値を取得します.
* @param tempoList
Expand Down
12 changes: 6 additions & 6 deletions src/fourthline/mmlTools/MMLTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ private String tailFix(String s) {
*/
public double getPlayTime() {
int totalTick = (int)getMaxTickLength();
double playTime = MMLTempoEvent.getTimeOnTickOffset(globalTempoList, totalTick);
long playTime = MMLTempoEvent.getTimeOnTickOffset(globalTempoList, totalTick);

return playTime;
return playTime/1000.0;
}

/**
Expand All @@ -206,7 +206,7 @@ public double getPlayTime() {
* @return 時間(秒)
*/
public double getMabinogiTime() {
double partTime[] = new double[mmlParts.size()];
long partTime[] = new long[mmlParts.size()];

int melodyTick = (int)mmlParts.get(0).getTickLength();
partTime[0] = MMLTempoEvent.getTimeOnTickOffset(globalTempoList, melodyTick);
Expand All @@ -223,13 +223,13 @@ public double getMabinogiTime() {
partTime[i] = MMLTempoEvent.getTimeOnTickOffset(globalTailTempo, tick);
}

double maxTime = 0.0;
for (double time : partTime) {
long maxTime = 0;
for (long time : partTime) {
if (maxTime < time) {
maxTime = time;
}
}

return maxTime;
return maxTime/1000.0;
}
}
15 changes: 8 additions & 7 deletions test/fourthline/mmlTools/MMLTempoEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,46 @@ public void testAppendToListElement() {
assertEquals(expectList.toString(), tempoList.toString());
}

private void checkGetTimeOnTickTest(String mml, double expect) {
private void checkGetTimeOnTickTest(String mml, long expect) {
ArrayList<MMLTempoEvent> tempoList = new ArrayList<MMLTempoEvent>();
MMLEventList eventList = new MMLEventList(mml, tempoList);

int tick = (int) eventList.getTickLength();
System.out.println("tick: " + tick);
assertEquals(expect, MMLTempoEvent.getTimeOnTickOffset(tempoList, tick), 0.0001);
long time = MMLTempoEvent.getTimeOnTickOffset(tempoList, tick);
assertEquals(expect, time);
assertEquals(tick, MMLTempoEvent.getTickOffsetOnTime(tempoList, time));
}

@Test
public void testGetTimeOnTickOffset_0() {
String mml = "t60cccccccccct120cccccccccc";
double expect = 15.0;
long expect = 15000;

checkGetTimeOnTickTest(mml, expect);
}

@Test
public void testGetTimeOnTickOffset_1() {
String mml = "cccccccccct60cccccccccc";
double expect = 15.0;
long expect = 15000;

checkGetTimeOnTickTest(mml, expect);
}

@Test
public void testGetTimeOnTickOffset_2() {
String mml = "t32l1.c";
double expect = 11.25;
long expect = 11250;

checkGetTimeOnTickTest(mml, expect);
}

@Test
public void testGetTimeOnTickOffset_3() {
String mml = "";
double expect = 0.0;
long expect = 0;

checkGetTimeOnTickTest(mml, expect);
}

}
4 changes: 2 additions & 2 deletions test/fourthline/mmlTools/MMLTrackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ private void checkPlayTimeAndMabinogiTime(String mml) {
double expectMabinogiTime = tools.getMabinogiTime();
System.out.printf("playTime: %f, mabinogiTime: %f\n", expectPlayTime, expectMabinogiTime);

assertEquals(expectPlayTime, track.getPlayTime(), 0.00001);
assertEquals(expectMabinogiTime, track.getMabinogiTime(), 0.00001);
assertEquals(expectPlayTime, track.getPlayTime(), 0.001);
assertEquals(expectMabinogiTime, track.getMabinogiTime(), 0.001);
} catch (UndefinedTickException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit e42e05f

Please sign in to comment.