-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
181 lines (166 loc) · 6.47 KB
/
script.js
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
function error(text) {
$('.column').addClass('hidden');
$('.container').append('<p class="no-support">' + text + '</p>');
}
jQuery(function ($) {
'use strict'
var supportsAudio = !!document.createElement('audio').canPlayType;
if (supportsAudio) {
var audioCtx;
var track;
var pannerR, pannerL;
var splitter;
var listener;
// initialize plyr
var player = new Plyr('#audio1', {
controls: [
'restart',
'play',
'progress',
'current-time',
'duration',
'mute',
'volume'
]
});
$('[data-plyr~="play"]').on('click', function () {
init();
});
// initialize playlist and controls
var index = 0,
playing = false,
tracks = [{
"track": 1,
"name": "Eagles - Hotel California (Binaurial version by binaulab.com)",
"duration": "6:32",
"file": "https://cors-anywhere.herokuapp.com/https://music.wixstatic.com/mp3/a84226_890273adef0845e38a6e550081227f9e.mp3"
}, {
"track": 2,
"name": "Queen - Bohemian Rhapsody (Binaurial version by binaulab.com)",
"duration": "6:10",
"file": "https://cors-anywhere.herokuapp.com/https://onedrive.live.com/download?cid=0B73C60A829CBF3E&resid=B73C60A829CBF3E%2119828&authkey=AJ4bOO9hG5QdUlQ"
}, {
"track": 3,
"name": "WILLIAM TELL (MOUTH POPPING) (Hugo Zuccarrelli’s Aldebaran)",
"duration": "1:17",
"file": "https://cors-anywhere.herokuapp.com/http://s3.amazonaws.com/srobbin-binaural/William_Tell.mp3"
}, {
"track": 4,
"name": "HAIRCUT-HAIRDRIER (Hugo Zuccarrelli’s Aldebaran)",
"duration": "1:07",
"file": "https://cors-anywhere.herokuapp.com/http://s3.amazonaws.com/srobbin-binaural/Haircut-Hairdrier.mp3"
}, {
"track": 5,
"name": "MATCHES-MATCHBOX SHAKES (Hugo Zuccarrelli’s Aldebaran)",
"duration": "1:14",
"file": "https://cors-anywhere.herokuapp.com/http://s3.amazonaws.com/srobbin-binaural/Matches-Matchbox_Shakes.mp3"
}],
buildPlaylist = $(tracks).each(function(key, value) {
var trackNumber = value.track,
trackName = value.name,
trackDuration = value.duration;
if (trackNumber.toString().length === 1) {
trackNumber = '0' + trackNumber;
}
$('#plList').append('<li> \
<div class="plItem"> \
<span class="plNum">' + trackNumber + '.</span> \
<span class="plTitle">' + trackName + '</span> \
<span class="plLength">' + trackDuration + '</span> \
</div> \
</li>');
}),
trackCount = tracks.length,
npAction = $('#npAction'),
npTitle = $('#npTitle'),
audio = $('#audio1').on('play', function () {
playing = true;
npAction.text('Now Playing...');
}).on('pause', function () {
playing = false;
npAction.text('Paused...');
}).on('ended', function () {
npAction.text('Paused...');
if ((index + 1) < trackCount) {
index++;
loadTrack(index);
audio.play();
} else {
audio.pause();
index = 0;
loadTrack(index);
}
}).get(0),
li = $('#plList li').on('click', function () {
var id = parseInt($(this).index());
if (id !== index) {
playTrack(id);
}
}),
init = function () {
if(!audioCtx) {
//We shoud
const AudioContext = window.AudioContext || window.webkitAudioContext;
if (!AudioContext) {
error("Web audio API not supported");
return;
}
audioCtx = new AudioContext();
pannerR = audioCtx.createPanner();
pannerR.panningModel = 'HRTF';
pannerR.rolloffFactor = 0;
pannerR.coneInnerAngle = 0;
pannerR.coneOuterAngle = 90;
pannerR.coneOuterGain = 0.0;
pannerR.setPosition(1, 0, 0);
pannerR.setOrientation(-1, 0, 0);
pannerL = audioCtx.createPanner();
pannerL.panningModel = 'HRTF';
pannerL.rolloffFactor = 0;
pannerL.coneInnerAngle = 0;
pannerL.coneOuterAngle = 90;
pannerL.coneOuterGain = 0.0;
pannerL.setPosition(-1, 0, 0);
pannerL.setOrientation(1, 0, 0);
splitter = audioCtx.createChannelSplitter(2);
listener = audioCtx.listener;
track = audioCtx.createMediaElementSource(audio);
pannerL.connect(audioCtx.destination);
splitter.connect(pannerL, 0);
pannerR.connect(audioCtx.destination);
splitter.connect(pannerR, 1);
track.connect(splitter);
}
},
loadTrack = function (id) {
$('.plSel').removeClass('plSel');
$('#plList li:eq(' + id + ')').addClass('plSel');
npTitle.text(tracks[id].name);
index = id;
audio.src = tracks[id].file;
},
playTrack = function (id) {
loadTrack(id);
init();
audio.play();
};
loadTrack(index);
if (!window.DeviceOrientationEvent) {
$('#orientation-not-supported').removeClass("hidden");
$('#coordinates').addClass("hidden");
} else {
window.addEventListener('deviceorientation', function(event) {
document.getElementById('beta').innerHTML = Math.round(event.beta);
document.getElementById('gamma').innerHTML = Math.round(event.gamma);
document.getElementById('alpha').innerHTML = Math.round(event.alpha);
document.getElementById('is-absolute').innerHTML = event.absolute ? "true" : "false";
if (listener) {
var angle = event.alpha / 180.0 * Math.PI;
listener.setOrientation(Math.sin(angle),0,-Math.cos(angle),0,1,0);
}
});
}
} else {
error($('#audio1').text());
}
});