summaryrefslogtreecommitdiffstats
path: root/demos/mobile/guitartuner/src/application.qml
blob: 7a078451e272cc707a7363b3f06b5d9cd48ef02b (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 1.0
import "mycomponents"

/* The base canvas for all QML drawing. */
Rectangle {
    id: application

    property int targetNoteIndex: 0
    property alias frequency: noteChooser.currentFrequency
    //Data provided to C++.
    property bool isInput: true
    property bool isMuted: false
    property bool isAuto: true
    property alias maxVoiceDifference: voiceDifferenceMeter.maxValue
    property real volume: 0.5
    property real sensitivity: 0.5

    //Signals to C++.
    signal volumeChanged(real volume)
    signal microphoneSensitivityChanged(real sensitivity)
    signal targetFrequencyChanged(real frequency)
    signal modeChanged(bool isInput)
    signal muteStateChanged(bool isMuted)

    //Slots for signals coming from C++.
    function voiceDifferenceChanged(difference) {
        if (isAuto) timer.running = true;
        voiceDifferenceMeter.valueChanged(difference);
        noteImage.glowing = false
    }
    function correctFrequencyObtained() {
        noteImage.glowing = true
    }
    function lowVoice() {
        noteImage.glowing = false
    }

    //Private function for changing the target frequency automatically.
    function calculateTargetFrequency(difference) {
        var tempDifference = Math.abs(difference);
        var tempIndex = targetNoteIndex
        while (!(difference < 0 && tempIndex == 0) &&
               tempDifference >= notes.get(tempIndex-(difference<0)).interval/2) {
            tempDifference -= notes.get(tempIndex-(difference<0)).interval;
            tempIndex += difference/Math.abs(difference);
        }
        if (tempIndex != targetNoteIndex) {
            targetNoteIndex = tempIndex
            noteChooser.currentFrequency = notes.get(targetNoteIndex).frequency;
            targetFrequencyChanged(frequency);
        }
    }

    width: 360; height: 640
    color: "black"

    //Provides data for functions.
    NotesModel {id: notes}

    /* A timer for changing the target frequency automatically.
     * This is needed for avoiding recursion. */
    Timer {
        id: timer

        interval: 1
        onTriggered: calculateTargetFrequency(voiceDifferenceMeter.value)
    }

    //A meter for showing the difference between current and target frequency.
    Meter {
        id: voiceDifferenceMeter

        maxValue: 12
        minValue: -maxValue
        height: imageSize.height/background.sourceSize.height*parent.height
        width: imageSize.width/background.sourceSize.width*parent.width
        anchors {
            topMargin: 100/background.sourceSize.height*parent.height
            horizontalCenter: parent.horizontalCenter
            top: parent.top
        }
    }

    Image {
        id: background

        anchors.fill: parent
        smooth: true
        source: "./mycomponents/images/guitartuner_skin.png"
    }

    //A button for quitting the application.
    Image {
        id: quitButton

        width: sourceSize.width/background.sourceSize.width*parent.width
        height: sourceSize.height/background.sourceSize.height*parent.height
        source: "./mycomponents/images/power.png"
        smooth: true
        KeyNavigation.up: volumeAdjuster
        KeyNavigation.down: modeButton
        Keys.onEnterPressed: Qt.quit()
        anchors{
            leftMargin: 297/background.sourceSize.width*parent.width
            left: parent.left;
            topMargin: 17/background.sourceSize.height*parent.height
            top: parent.top
        }

        MouseArea {
            anchors.fill: parent
            onClicked: Qt.quit()
        }
    }

    //An image for showing the target note.
    Image {
        id: noteImage

        property bool glowing: false

        width: sourceSize.width/background.sourceSize.width*parent.width
        height: sourceSize.height/background.sourceSize.height*parent.height
        source: glowing ? notes.get(targetNoteIndex).glowSource : notes.get(targetNoteIndex).bigSource

        anchors {
            topMargin: 273/background.sourceSize.height*parent.height
            top: parent.top
            horizontalCenter: parent.horizontalCenter
        }
    }

    //A button for choosing the input/output mode.
    Image {
        id: modeButton

        function buttonPressed() {
            isInput = !isInput
            modeChanged(isInput)
            if (isInput) {
                soundIcons.source = "./mycomponents/images/sensitivity.png"
                source = "./mycomponents/images/voicemode_off.png"
                volumeAdjuster.setValue(sensitivity)
            }
            else {
                //Change off from "auto" mode
                if (isAuto) {
                    noteChooser.pushButton(targetNoteIndex)
                }
                if (isMuted) {
                    soundIcons.source = "./mycomponents/images/volume_off.png";
                }
                else
                    soundIcons.source = "./mycomponents/images/volume.png"
                source = "./mycomponents/images/voicemode_on.png"
                volumeAdjuster.setValue(volume)
            }
        }

        width: sourceSize.width/background.sourceSize.width*parent.width
        height: sourceSize.height/background.sourceSize.height*parent.height
        smooth: true
        source: "./mycomponents/images/voicemode_off.png"
        KeyNavigation.up: quitButton
        KeyNavigation.down: noteChooser
        Keys.onEnterPressed: buttonPressed()
        anchors {
            leftMargin: 16/background.sourceSize.width*parent.width
            left: parent.left
            topMargin: 353/background.sourceSize.height*parent.height
            top: parent.top
        }

        MouseArea {
            anchors.fill: parent
            onPressed: {
                parent.focus = true
                parent.scale = 0.95
            }
            onReleased: {
                parent.scale = 1/0.95
            }
            onClicked: parent.buttonPressed()
        }
    }

    //Buttons for choosing the target note.
    NoteButtonView {
        id: noteChooser

        width: parent.width*0.95; height: width/model.count
        onNoteSelected: {
            if (note == "Auto") {
                if (!isAuto) {
                    isAuto = true
                }
                if (!isInput) {
                    modeButton.buttonPressed()
                }
            }
            else {
                timer.running = false;
                isAuto = false
                targetNoteIndex = index
                targetFrequencyChanged(frequency)
            }
            focus = true
        }
        KeyNavigation.up: modeButton
        KeyNavigation.down: soundIcons
        anchors {
            horizontalCenter: parent.horizontalCenter
            topMargin: 454/background.sourceSize.height*parent.height
            top: parent.top
        }
    }

    //An element for showing the mode and changing the mute state.
    Image {
        id: soundIcons

        function stateChanged() {
            isMuted = !isMuted
            muteStateChanged(isMuted)
            if (isMuted) {
                source = "qrc:/src/mycomponents/images/volume_off.png"
            }
            else {
                source = "qrc:/src/mycomponents/images/volume.png"
            }
        }

        width: sourceSize.width/background.sourceSize.width*parent.width
        height: sourceSize.height/background.sourceSize.height*parent.height
        smooth: true
        source: "./mycomponents/images/sensitivity.png"
        Keys.onEnterPressed: stateChanged()
        KeyNavigation.up: noteChooser
        KeyNavigation.down: quitButton
        KeyNavigation.left: volumeAdjuster
        KeyNavigation.right: volumeAdjuster
        anchors {
            leftMargin: 42/background.sourceSize.width*parent.width
            left: parent.left
            topMargin: 565/background.sourceSize.height*parent.height
            top: parent.top
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (!isInput) {
                    parent.stateChanged()
                }
                parent.focus = true
            }
        }
    }

    //An element for adjusting volume.
    Adjuster {
        id: volumeAdjuster

        max: 1
        value: 0.5
        width: 222/background.sourceSize.width*parent.width
        height: parent.height*0.1
        onFocusChangedByClick: focus = true
        onArrowPressedWhenValueOverLimits: soundIcons.focus = true
        KeyNavigation.up: modeButton
        KeyNavigation.down: quitButton
        anchors {
            leftMargin: 98/background.sourceSize.width*parent.width
            left: parent.left
            verticalCenter: soundIcons.verticalCenter
        }
        onValueChanged: {
            if (isInput) {
                sensitivity = value;
                microphoneSensitivityChanged(1-sensitivity)
            }
            else {
                volume = value
                volumeChanged(volume)
            }
        }
    }
}