わいずふぁくとりいがプログラムの話題をウィキします。

nomeMetro2 <-4->

 ノーム・メトロの主要な処理はこのようになります。

class NomeMetro {
    protected val N_AVERAGE = 10
    protected val RESET_THRESH = 3000 //ms
    protected var __intervals = LongArray(N_AVERAGE)
    protected var __index = 0
    protected var __count = 0
    protected var __last_time: Long = 0

    //平均を求める。
    protected fun average(interval: Long): Long {
        if (__index >= N_AVERAGE) {
            __index = 0
        }
        __intervals[__index++] = interval
        if (__count < N_AVERAGE) {
            ++__count
        }
        var sum: Long = 0
        var i: Int
        i = 0
        while (i < __count) {
            sum += __intervals[i]
            ++i
        }
        return Math.round(sum.toDouble() / __count.toDouble())
    }

    //平均をリセットする。
    protected fun reset() {
        __index = 0
        __count = 0
    }

    //テンポに換算
    protected fun tempo(interval: Long): Int {
        val tempo = 60.0 / interval.toDouble() * 1000.0
        return Math.round(tempo).toInt()
    }

    //画面をタップされたときの処理。
    fun Action(time: Long, average: IntArray): Int {
        //時間間隔(interval)を求める。
        val interval = time - __last_time
        __last_time = time
        if( interval > RESET_THRESH ){
            //3秒以上操作されなかった場合は、それまでに溜まった平均をリセット。
            reset();
            average[0] = 0
            return 0
        }
        //テンポとテンポの平均を返す。
        average[0] = tempo(average(interval))
        return tempo(interval)
    }
}

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu

管理人/副管理人のみ編集できます