-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.js
300 lines (239 loc) · 6.25 KB
/
music.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
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
// Fetching Elements
const playBtn = document.getElementsByClassName("play")[0];
const playIcon = document.getElementById("play_pause");
const songs = document.getElementById("song");
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
//setting initial state of player pause
let isPlaying = false;
var width = 4; //Widh of each vertical bar is 4px
var X = 0; //Initial position of canvas
var max = 80; //maximum height of waveform
var min = -100; //minimum height of waveform
var h = 0 //Height of each vertical bar initially.
//Drawing initial pattern
ctx.beginPath();
for(var i=0; i<=233; i++)
{
//Manually created function used to create pattern of waveform
var h = pattern(i,h)
ctx.fillStyle = "gray";
//Inital color is made gray
ctx.fillRect(X, (h>0?180:200),width, (h<20)? ((h<20 && h>0)?20:(h<-20?h:-20)) :h);
//contional operator is used to maintain a certain height of verticle bars.
X = X + width+2; //Every time bar will move left by 6px.
//maximum width is 1400 so bars will cover width of (w<=1400)
}
let Position = 0;
//Position represent current time of audio on scale of 0 to 100
let duration = songs.duration;
songs.addEventListener('loadedmetadata', (event) => {
duration = event.srcElement.duration;
})
console.log(songs.duration)
var last_pos = -1;
//Listening to event every time when music is playing.
//To Overwrite the canvas in multiple color.
songs.addEventListener('timeupdate', (event) => {
//console.log(event.srcElement.currentTime)
current_time = event.srcElement.currentTime;
Position = Math.floor((current_time*100)/duration);
//Changing pause to play when music is finished.
if(Position == 100)
{
playIcon.classList.replace("fa-pause","fa-play");
}
//If last position is same to new position then paint event will not fire to
//ensure that it do not stall the performance of app.
if(last_pos != Position)
{
paint(Position)
}
last_pos = Position;
});
//Painting canvas in red color till current position of music
function paint(Position)
{
//Observing status of current time on console.
console.log("Paint",Position)
var X1 = 0;
var h1 = 0;
for(var l=0; l<233; l++)
{
ctx.beginPath();
//Manually created function used to create pattern of waveform
var h1 = pattern(l,h1)
if(l<(Position*2.33))
{
ctx.fillStyle="red";
}
else
{
ctx.fillStyle="gray";
}
ctx.fillRect(X1, (h1>0?180:200),width, (h1<20)? ((h1<20 && h1>0)?20:(h1<-20?h1:-20)) :h1);
//contional operator is used to maintain a certain height of verticle bars.
ctx.fill();
//Moving forward horizontally with 6px distance.
X1 = X1 + width+2;
}
}
//To play music
const playMusic = () => {
isPlaying = true;
playIcon.classList.replace("fa-play","fa-pause")
songs.play();
//console.log("play")
}
//To pause music
const pauseMusic = () => {
isPlaying = false;
songs.pause();
playIcon.classList.replace("fa-pause","fa-play")
//console.log("pause")
}
//To seek music
const seekMusic = (t) => {
songs.currentTime = duration*(t/100);
console.log(songs.currentTime)
}
//play button can evoke two functions using conditional operator
playBtn.addEventListener("click", () => {
isPlaying? pauseMusic(): playMusic();
});
//To fetch the position of music string, listening to mousedown Event.
canvas.addEventListener("mousedown", (event) => {
var total_length = 1400;
var local_position = event.clientX-18;
var covered_inper = Math.floor(((local_position*100)/total_length));
seekMusic(covered_inper)
})
//Function to generate a waveform in certain fashion
function pattern(i,h)
{
if(i<=25)
{
h = h+2;
}
else if(i>25 && i<=50)
{
h = h-2;
}
else if(i>50 && i<=75)
{
h = h-2;
}
else if(i>75 && i<=100)
{
h = h+2;
}
else if(i>100 && i<=125)
{
h = h+2;
}
else if(i>125 && i<=150)
{
h = h-2;
}
else if(i>150 && i<=175)
{
h = h-2;
}
else if(i>175 && i<=200)
{
h = h+2;
}
else if(i>200 && i<=225)
{
h = h+2;
}
else if(i>225 && i<=250)
{
h = h-2;
}
return h;
}
//Building Static Flags
//Static Flags_1
ctx.beginPath();
ctx.rect(200, 70, 3, 100);
ctx.rect(150, 70, 100, 20);
//Static Flags_2
ctx.rect(400, 40, 3, 140);
ctx.rect(350, 30, 100, 20);
ctx.fillStyle = "lightgreen";
ctx.fill();
//Static Flags_3
ctx.beginPath();
ctx.rect(1100, 20, 3, 150);
ctx.fillStyle="lightgreen";
ctx.fill();
//Static Flags_4
ctx.beginPath();
ctx.rect(1180, 100, 3, 70);
ctx.rect(1180, 100, 80, 20);
ctx.fillStyle="purple";
ctx.fill();
//Static Flags_5
ctx.beginPath();
ctx.rect(1270, 60, 3, 110);
ctx.rect(1105, 60, 175, 20);
ctx.fillStyle="brown";
ctx.fill();
//Static Flags_6
ctx.beginPath();
ctx.rect(1300, 20, 3, 150);
ctx.rect(1090, 10, 220, 20);
ctx.fillStyle="green";
ctx.fill();
//Static board circle Flag_1
ctx.beginPath();
ctx.arc(201, 178, 8, 0, 2 * Math.PI);
ctx.fillStyle="lightgreen";
ctx.fill();
//Static board circle Flag_2
ctx.beginPath();
ctx.arc(401, 178, 8, 0, 2 * Math.PI);
ctx.fillStyle="lightgreen";
ctx.fill();
//Static board circle Flag_3
ctx.beginPath();
ctx.arc(1101, 178, 8, 0, 2 * Math.PI);
ctx.fillStyle="lightgreen";
ctx.fill();
//Static board circle Flag_4
ctx.beginPath();
ctx.arc(1181, 178, 8, 0, 2 * Math.PI);
ctx.fillStyle="purple";
ctx.fill();
//Static board circle Flag_5
ctx.beginPath();
ctx.arc(1272, 178, 8, 0, 2 * Math.PI);
ctx.fillStyle="brown";
ctx.fill();
//Static board circle Flag_6
ctx.beginPath();
ctx.arc(1301, 178, 8, 0, 2 * Math.PI);
ctx.fillStyle="green";
ctx.fill();
//Static Heading Flags_1
ctx.beginPath();
ctx.fillStyle="white";
ctx.font = "10pt sans-serif";
ctx.fillText("Introduction", 162, 85);
ctx.beginPath();
ctx.fillStyle="white";
ctx.font = "10pt sans-serif";
ctx.fillText("One_six", 370, 45);
ctx.beginPath();
ctx.fillStyle="white";
ctx.font = "10pt sans-serif";
ctx.fillText("Rapport Building - Empathy", 1112, 75);
ctx.beginPath();
ctx.fillStyle="white";
ctx.font = "10pt sans-serif";
ctx.fillText("Rapport Building - Energy", 1100, 25);
ctx.beginPath();
ctx.fillStyle="white";
ctx.font = "10pt sans-serif";
ctx.fillText("Profile", 1200, 115);