Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Sketchometry.activity/js/audio.js
blob: 3bc41604f042da0a552212d9af68547ed8d83756 (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
/*
    Copyright 2011 / 2012

        Alfred Wassermann
        Michael Gerhaeuser,
        Carsten Miller,
        Matthias Ehmann,
        Heiko Vogel

    This file is part of the JSXGraph GUI project.
    This code isn't licensed yet.
*/

(function () {

    'use strict';
    
    var s;
    
    GUI.Audio = {

        /**
         * List of available sounds.
         * @type Object
         */
        sounds: {
            'click': 'audio-click',
            'bubble': 'audio-bubble',
            'plastic': 'audio-plastic'
        },

        levels: [0, 0.23, 0.55, 1],

        /**
         * Sound enabled?
         * @type Boolean
         * @default true
         */
        enabled: true,

        /**
         * Dummy player to extract supported codecs etc.
         * @type Node
         */
        dummy: document.getElementById('audio-dummy'),

        /**
         * Used sound format, depends on the browser. Possible values: 'mp3' and 'ogg'.
         * @type String
         * @default 'mp3'
         */
        format: 'mp3',

        /**
         * Plays a sound.
         * @param what What sound to play. Currently only 'click' is available.
         */
        play: function (what) {
            var volume = GUI.Settings.get('volume');
                //base = 6*Math.E;

            if (GUI.Settings.get('sound') && GUI.Audio.sounds[what] && GUI.Audio[what] && volume > 0) {
                //GUI.Audio[what].volume = volume / 3;
                //GUI.Audio[what].volume = (Math.pow(base, volume*0.33333)-1)/(base-1);
                GUI.Audio[what].volume = this.levels[volume];
                GUI.Audio[what].play();
            }
        }
    };
    
    // determine codec
    if (GUI.Audio.dummy) {
        GUI.Audio.format = (typeof GUI.Audio.dummy.canPlayType === "function" && GUI.Audio.dummy.canPlayType("audio/mpeg") !== "") ? 'mp3' : 'ogg';

        // load audio files
        for (s in GUI.Audio.sounds) {
            if (GUI.Audio.sounds.hasOwnProperty(s)) {
                GUI.Audio[s] = document.getElementById(GUI.Audio.sounds[s]);
                GUI.Audio[s].src = 'audio/' + s + '.' + GUI.Audio.format;
            }
        }
    }

    /**
     * Browser independent vibrate function.
     */
    GUI.Audio.vibrate = (function(){
        if (navigator.vibrate) {
            return function (dur) {
                if (GUI.Settings.get('vibrate')) {
                    return navigator.vibrate(dur);
                }
            };
        }
        
        if (navigator.mozVibrate) {
            return function (dur) {
                if (GUI.Settings.get('vibrate')) {
                    return navigator.mozVibrate(dur);
                }
            };
        } 
        
        if (navigator.webkitVibrate) {
            return function (dur) {
                if (GUI.Settings.get('vibrate')) {
                    return navigator.webkitVibrate(dur);
                }
            };
        } 
        
        if (navigator.oVibrate) {
            return function (dur) {
                if (GUI.Settings.get('vibrate')) {
                    return navigator.oVibrate(dur);
                }
            };
        } 
        
        if (navigator.msVibrate) {
            return function (dur) {
                if (GUI.Settings.get('vibrate')) {
                    return navigator.msVibrate(dur);
                }
            };
        }
        
        return function (dur) { };
    }());
    
}());