~aacid/unity8/revert_r388

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2012, 2013 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.0
import HudClient 0.1

Item {
    id: voiceInput

    readonly property int totalSpots: 30
    readonly property int totalIdles: 7

    property real __newPeak: 0

    VolumePeakDetector {
        id: peakDetector
        desiredInterval: peakMover.interval
        onNewPeak: {
            __newPeak = volume
        }
    }

    Timer {
        id: peakMover
        interval: 10
        repeat: true
        onTriggered: {
            moveBalls()
            voiceAmplitudes.itemAt(0).amplitude = __newPeak
        }
    }

    Repeater {
        id: fixedPositionSoundAmplitudes
        model: totalSpots
        delegate: SoundAmplitudeDelegate {
            totalCount: fixedPositionSoundAmplitudes.count
            ballIndex: index
            opacity: 1
            visible: true
        }
    }

    Repeater {
        id: voiceAmplitudes
        model: totalSpots
        delegate: SoundAmplitudeDelegate {
            totalCount: voiceAmplitudes.count
            ballIndex: index
        }
    }

    Repeater {
        id: idleAmplitudes
        model: totalSpots
        delegate: SoundAmplitudeDelegate {
            totalCount: idleAmplitudes.count
            ballIndex: index
            amplitude: 0.05
            visible: idleAmplitudes.enabled
        }
    }

    function setDetectorEnabled(enabled) {
        idleAmplitudes.enabled = false
        if (enabled) {
            peakMover.start()
        } else {
            peakDetector.enabled = false
            peakMover.stop()
        }
    }

    function moveBalls() {
        var i = 0
        var amplitude = voiceAmplitudes.itemAt(0).amplitude
        for (var i = 1; i < totalSpots; ++i) {
            var item = voiceAmplitudes.itemAt(i)
            var tempAmplitude = item.amplitude
            item.amplitude = amplitude
            amplitude = tempAmplitude
        }
    }

    property int __firstIdle
    property int __idleCount

    Timer {
        id: idleBallsTimer
        interval: 40
        onTriggered: moveAndAddIdleBalls()
    }

    function moveAndAddIdleBalls() {
        // Check if we are doing the real thing
        if (!idleAmplitudes.enabled) {
            return
        }

        for (var i = 0; i < __idleCount; ++i) {
            var index = __firstIdle - i
            var item = idleAmplitudes.itemAt(index % totalSpots)
            var nextItem = idleAmplitudes.itemAt((index + 1) % totalSpots)
            nextItem.opacity = item.opacity
        }
        __firstIdle++
        idleAmplitudes.itemAt((__firstIdle - __idleCount) % totalSpots).opacity = 0

        if (__idleCount < totalIdles) {
            idleAmplitudes.itemAt(0).opacity = 1 - (__idleCount / totalIdles)
            __idleCount++
        }
        idleBallsTimer.start()
    }

    function startIdle() {
        // On the device the peak detector takes quite a lot to start up
        // so enable it as soon as we know we are going to do voice capture
        peakDetector.enabled = true
        for (var i = 0; i < totalSpots; ++i) {
            idleAmplitudes.itemAt(i).opacity = 0
        }
        for (var i = 0; i < totalSpots; ++i) {
            voiceAmplitudes.itemAt(i).amplitude = 0
        }

        idleAmplitudes.enabled = true
        idleAmplitudes.itemAt(0).opacity = 1
        __idleCount = 1
        __firstIdle = 0
        idleBallsTimer.start()
    }
}