Unity 8
 All Classes Functions Properties
SoundAmplitudeVisual.qml
1 /*
2  * Copyright (C) 2012, 2013 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.0
18 import HudClient 0.1
19 
20 Item {
21  id: voiceInput
22 
23  readonly property int totalSpots: 30
24  readonly property int totalIdles: 7
25 
26  property real __newPeak: 0
27 
28  VolumePeakDetector {
29  id: peakDetector
30  desiredInterval: peakMover.interval
31  onNewPeak: {
32  __newPeak = volume
33  }
34  }
35 
36  Timer {
37  id: peakMover
38  interval: 10
39  repeat: true
40  onTriggered: {
41  moveBalls()
42  voiceAmplitudes.itemAt(0).amplitude = __newPeak
43  }
44  }
45 
46  Repeater {
47  id: fixedPositionSoundAmplitudes
48  model: totalSpots
49  delegate: SoundAmplitudeDelegate {
50  totalCount: fixedPositionSoundAmplitudes.count
51  ballIndex: index
52  opacity: 1
53  visible: true
54  }
55  }
56 
57  Repeater {
58  id: voiceAmplitudes
59  model: totalSpots
60  delegate: SoundAmplitudeDelegate {
61  totalCount: voiceAmplitudes.count
62  ballIndex: index
63  }
64  }
65 
66  Repeater {
67  id: idleAmplitudes
68  model: totalSpots
69  delegate: SoundAmplitudeDelegate {
70  totalCount: idleAmplitudes.count
71  ballIndex: index
72  amplitude: 0.05
73  visible: idleAmplitudes.enabled
74  }
75  }
76 
77  function setDetectorEnabled(enabled) {
78  idleAmplitudes.enabled = false
79  if (enabled) {
80  peakMover.start()
81  } else {
82  peakDetector.enabled = false
83  peakMover.stop()
84  }
85  }
86 
87  function moveBalls() {
88  var i = 0
89  var amplitude = voiceAmplitudes.itemAt(0).amplitude
90  for (var i = 1; i < totalSpots; ++i) {
91  var item = voiceAmplitudes.itemAt(i)
92  var tempAmplitude = item.amplitude
93  item.amplitude = amplitude
94  amplitude = tempAmplitude
95  }
96  }
97 
98  property int __firstIdle
99  property int __idleCount
100 
101  Timer {
102  id: idleBallsTimer
103  interval: 40
104  onTriggered: moveAndAddIdleBalls()
105  }
106 
107  function moveAndAddIdleBalls() {
108  // Check if we are doing the real thing
109  if (!idleAmplitudes.enabled) {
110  return
111  }
112 
113  for (var i = 0; i < __idleCount; ++i) {
114  var index = __firstIdle - i
115  var item = idleAmplitudes.itemAt(index % totalSpots)
116  var nextItem = idleAmplitudes.itemAt((index + 1) % totalSpots)
117  nextItem.opacity = item.opacity
118  }
119  __firstIdle++
120  idleAmplitudes.itemAt((__firstIdle - __idleCount) % totalSpots).opacity = 0
121 
122  if (__idleCount < totalIdles) {
123  idleAmplitudes.itemAt(0).opacity = 1 - (__idleCount / totalIdles)
124  __idleCount++
125  }
126  idleBallsTimer.start()
127  }
128 
129  function startIdle() {
130  // On the device the peak detector takes quite a lot to start up
131  // so enable it as soon as we know we are going to do voice capture
132  peakDetector.enabled = true
133  for (var i = 0; i < totalSpots; ++i) {
134  idleAmplitudes.itemAt(i).opacity = 0
135  }
136  for (var i = 0; i < totalSpots; ++i) {
137  voiceAmplitudes.itemAt(i).amplitude = 0
138  }
139 
140  idleAmplitudes.enabled = true
141  idleAmplitudes.itemAt(0).opacity = 1
142  __idleCount = 1
143  __firstIdle = 0
144  idleBallsTimer.start()
145  }
146 }