-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.svelte
More file actions
135 lines (124 loc) · 3.47 KB
/
index.svelte
File metadata and controls
135 lines (124 loc) · 3.47 KB
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
<script context="module">
export function preload({ path, params, query }) {
return this.fetch(
`https://content.freshair.radio/ghost/api/canary/content/posts/?key=34b251f66e79e35d36bf9d1629&limit=6&fields=id,title,slug,feature_image&include=authors,tags`
)
.then((r) => r.json())
.then(({ posts }) => {
return { posts };
});
}
</script>
<script>
import { nowplaying } from "./_nowplaying.store.js";
import { navigating } from "./_pagefade.js";
import { audio } from "./_audio.store.js";
import { onMount } from "svelte";
import ShowCover from "./_showcover.svelte";
import Control from "./_control.svelte";
import PostPreview from "./_postpreview.svelte";
let canvas;
export let posts;
let w;
let h;
$: playingLive = $audio.live && $audio.volume == 1;
onMount(() => {
window.canvas = canvas;
let canvasCtx = canvas.getContext("2d");
let dataArray = new Uint8Array($audio.bufferLength);
let bufferLength = $audio.bufferLength;
let frame;
const draw = () => {
frame = requestAnimationFrame(draw);
const WIDTH = w;
const HEIGHT = h;
$audio.analyser.getByteTimeDomainData(dataArray);
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
canvasCtx.strokeStyle = "#fff";
canvasCtx.fillStyle = "#fff";
let sliceWidth = (WIDTH * 1.0) / (bufferLength / 128);
canvasCtx.lineWidth = sliceWidth / 2;
let x = 0;
// let y = 0;
let lastV = 0;
let zeroes = [];
canvasCtx.beginPath();
for (let i = 0; i < bufferLength; i += 128) {
let avg =
dataArray.slice(i, i + 128).reduce((acc, e) => acc + e, 0) / 128;
var v = avg / 128.0;
var y = (v * HEIGHT) / 2;
var otherV = v == 1 ? v : v > 1 ? 1 - (v - 1) : 1 + (1 - v);
var otherY = (otherV * HEIGHT) / 2;
if (i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.moveTo(x, y);
canvasCtx.lineTo(x, otherY);
}
x += sliceWidth;
}
// canvasCtx.lineTo(WIDTH, HEIGHT / 2);
canvasCtx.stroke();
canvasCtx.closePath();
return;
};
let timeout;
const run = () => {
if (playingLive) {
draw();
} else {
timeout = setTimeout(run, 500);
}
};
run();
return () => {
clearTimeout(timeout);
cancelAnimationFrame(frame);
};
});
</script>
<svelte:head>
<title>Freshair Radio</title>
</svelte:head>
<section
class="transition-opacity duration-300 {$navigating
? 'opacity-0'
: 'opacity-1'}"
>
<h1 class="text-4xl text-white py-6 px-4 font-thin lowercase">
listen live now!
</h1>
<div
class="h-72 lg:h-96 rounded-3xl bg-opacity-25 bg-gray-800 flex mx-4"
bind:clientHeight={h}
bind:clientWidth={w}
>
<Control
tailwind="z-20 my-auto mx-auto w-20 h-20 lg:w-32 lg:h-32 bg-gray-800 rounded-full"
click={() => {
console.log("click", playingLive);
if (playingLive) {
audio.pauseLive();
} else {
audio.playLive();
}
}}
playing={playingLive}
/>
<canvas
class="waveform absolute top-0"
bind:this={canvas}
width={w}
height={h}
/>
</div>
<h1 class="text-4xl text-white py-6 px-4 font-thin lowercase">
recent posts
</h1>
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 px-4 pb-4">
{#each posts as post}
<PostPreview {post} />
{/each}
</div>
</section>