From e42e05fd2a011e8958174c177cd0b219066d1554 Mon Sep 17 00:00:00 2001 From: fourthline Date: Sat, 9 Aug 2014 09:58:25 +0900 Subject: [PATCH] =?UTF-8?q?time=20=E3=81=AE=E6=89=B1=E3=81=84=E3=82=92=20?= =?UTF-8?q?=E7=A7=92(double)=20->=20=E3=83=9F=E3=83=AA=E7=A7=92=20(long)?= =?UTF-8?q?=20=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fourthline/mabiicco/ui/MMLSeqView.java | 10 +++--- src/fourthline/mmlTools/MMLTempoEvent.java | 34 ++++++++++++++++--- src/fourthline/mmlTools/MMLTrack.java | 12 +++---- .../mmlTools/MMLTempoEventTest.java | 15 ++++---- test/fourthline/mmlTools/MMLTrackTest.java | 4 +-- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/fourthline/mabiicco/ui/MMLSeqView.java b/src/fourthline/mabiicco/ui/MMLSeqView.java index c362370f..72777295 100644 --- a/src/fourthline/mabiicco/ui/MMLSeqView.java +++ b/src/fourthline/mabiicco/ui/MMLSeqView.java @@ -626,14 +626,14 @@ private void startTimeViewUpdateThread() { private void updateTimeView() { long position = pianoRollView.getSequencePlayPosition(); List 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); diff --git a/src/fourthline/mmlTools/MMLTempoEvent.java b/src/fourthline/mmlTools/MMLTempoEvent.java index 560664d0..34e69563 100644 --- a/src/fourthline/mmlTools/MMLTempoEvent.java +++ b/src/fourthline/mmlTools/MMLTempoEvent.java @@ -92,10 +92,10 @@ public static int searchOnTick(List tempoList, long tickOffset) { * 指定したtickオフセット位置の先頭からの時間を返します. * @param tempoList * @param tickOffset - * @return 先頭からの時間(秒) + * @return 先頭からの時間(ms) */ - public static double getTimeOnTickOffset(List tempoList, int tickOffset) { - double totalTime = 0.0; + public static long getTimeOnTickOffset(List tempoList, int tickOffset) { + long totalTime = 0L; int tempo = INITIAL_TEMPO; int currentTick = 0; @@ -107,18 +107,42 @@ public static double getTimeOnTickOffset(List 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 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 diff --git a/src/fourthline/mmlTools/MMLTrack.java b/src/fourthline/mmlTools/MMLTrack.java index 82324194..b9e11dc4 100644 --- a/src/fourthline/mmlTools/MMLTrack.java +++ b/src/fourthline/mmlTools/MMLTrack.java @@ -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; } /** @@ -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); @@ -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; } } diff --git a/test/fourthline/mmlTools/MMLTempoEventTest.java b/test/fourthline/mmlTools/MMLTempoEventTest.java index 02e2b927..a7250967 100644 --- a/test/fourthline/mmlTools/MMLTempoEventTest.java +++ b/test/fourthline/mmlTools/MMLTempoEventTest.java @@ -35,19 +35,21 @@ public void testAppendToListElement() { assertEquals(expectList.toString(), tempoList.toString()); } - private void checkGetTimeOnTickTest(String mml, double expect) { + private void checkGetTimeOnTickTest(String mml, long expect) { ArrayList tempoList = new ArrayList(); 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); } @@ -55,7 +57,7 @@ public void testGetTimeOnTickOffset_0() { @Test public void testGetTimeOnTickOffset_1() { String mml = "cccccccccct60cccccccccc"; - double expect = 15.0; + long expect = 15000; checkGetTimeOnTickTest(mml, expect); } @@ -63,7 +65,7 @@ public void testGetTimeOnTickOffset_1() { @Test public void testGetTimeOnTickOffset_2() { String mml = "t32l1.c"; - double expect = 11.25; + long expect = 11250; checkGetTimeOnTickTest(mml, expect); } @@ -71,9 +73,8 @@ public void testGetTimeOnTickOffset_2() { @Test public void testGetTimeOnTickOffset_3() { String mml = ""; - double expect = 0.0; + long expect = 0; checkGetTimeOnTickTest(mml, expect); } - } diff --git a/test/fourthline/mmlTools/MMLTrackTest.java b/test/fourthline/mmlTools/MMLTrackTest.java index 16917860..d1eb0c89 100644 --- a/test/fourthline/mmlTools/MMLTrackTest.java +++ b/test/fourthline/mmlTools/MMLTrackTest.java @@ -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(); }