File size: 3,860 Bytes
3deaf02
075b025
3deaf02
075b025
 
3deaf02
 
 
 
 
075b025
3deaf02
075b025
 
3deaf02
075b025
3deaf02
075b025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67158c5
 
3deaf02
 
 
 
67158c5
 
 
 
075b025
67158c5
 
 
 
 
3deaf02
 
 
075b025
67158c5
3deaf02
 
075b025
 
 
3deaf02
 
 
075b025
 
 
 
 
 
3deaf02
 
 
 
075b025
 
3deaf02
075b025
 
 
 
67158c5
 
075b025
 
 
3deaf02
339a63f
075b025
3deaf02
075b025
3deaf02
 
075b025
 
 
3deaf02
 
 
 
 
075b025
 
 
3deaf02
 
 
 
 
075b025
 
 
 
 
3deaf02
 
075b025
3deaf02
 
 
 
 
 
 
075b025
3deaf02
 
 
 
 
075b025
3deaf02
075b025
3deaf02
 
 
 
 
67158c5
075b025
3deaf02
 
 
 
075b025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3deaf02
 
 
075b025
 
3deaf02
 
 
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
<script lang="ts">
import { afterUpdate, onMount } from 'svelte';
import { fade } from 'svelte/transition';
import { audioBlob, notesImage, style } from './stores';
import { styles } from './config.json';

let currentTime: number;
let duration: number;
let paused = true;

let player: HTMLDivElement;
let visualisation: HTMLImageElement;
let imageWidth: number;
let imageHeight: number;

const updateDimensions = (): void => {
  imageWidth = visualisation && visualisation.clientWidth;
  imageHeight = visualisation && visualisation.clientHeight;
};

onMount(() => {
  updateDimensions();

  if ('mediaSession' in navigator) {
    navigator.mediaSession.setActionHandler('play', () => (paused = false));
    navigator.mediaSession.setActionHandler('pause', () => (paused = true));
    navigator.mediaSession.setActionHandler('stop', () => {
      paused = true;
      currentTime = 0;
    });
  }
});

afterUpdate((): void => {
  updateDimensions();
});

const mouseMove = (event: MouseEvent): void => {
  if (!duration) {
    return;
  }

  if (!event.buttons) {
    return;
  }

  const { left, right } = player.getBoundingClientRect();
  currentTime = (duration * (event.clientX - left)) / (right - left);
};

const touchMove = (event: TouchEvent): void => {
  if (!duration) {
    return;
  }

  const { left, right } = player.getBoundingClientRect();
  currentTime = (duration * (event.touches[0].clientX - left)) / (right - left);
};

const keyDown = (e: KeyboardEvent): void => {
  e.preventDefault();

  if (e.code === 'Space') {
    paused = !paused;
  }
  if (e.code === 'ArrowLeft') {
    currentTime = currentTime >= 1 ? currentTime - 1 : 0;
  }
  if (e.code === 'ArrowRight') {
    currentTime = currentTime <= duration - 1 ? currentTime + 1 : duration;
  }
};
</script>

{#if $audioBlob}
  <section transition:fade>
    <img class="visualisation" src={$notesImage} alt="" bind:this={visualisation} />
    <div
      bind:this={player}
      class="player"
      style:width={imageWidth + 'px'}
      style:height={imageHeight + 'px'}
      on:mousemove={mouseMove}
      on:touchmove|preventDefault={touchMove}
      on:keydown={keyDown}
      on:click={() => (paused = !paused)}
      tabindex="0"
    >
      <audio bind:currentTime bind:duration bind:paused src={$audioBlob} />
      <div class="handle" style:transform="translate({imageWidth * (currentTime / duration)}px, -2%)" />
      {#if paused}
        <img class="play-button" src="static/play.svg" alt="Play button" draggable="false" transition:fade />
      {/if}
    </div>
    <a href={$audioBlob} download={`${styles[$style]} Composition - AI Guru ft. Hugging Face.wav`} class="download"
      >Download</a
    >
  </section>
{/if}

<style>
section {
  display: flex;
  flex-direction: column;
  position: relative;
  border: 2px solid hsl(0 0% 80%);
  border-radius: 0.375rem;
  padding: 1rem;
}

.player {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  cursor: pointer;
}
.visualisation {
  width: min(100%, 512px);
  margin: auto;
}

audio {
  width: 100%;
  margin: 1rem auto;
}
.play-button {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 20%;
  aspect-ratio: 1 / 1;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 0 5px black);
  pointer-events: none;
  cursor: pointer;
}
.handle {
  position: absolute;
  left: 0;
  top: 0;
  height: 104%;
  width: 0.2rem;
  border-radius: 0.1rem;
  background-color: white;
  cursor: pointer;
  transform: translate(0, -2%);
}

a.download {
  display: block;
  font-size: 1.2rem;
  font-family: 'Lato', sans-serif;
  font-weight: 700;
  color: hsl(0 0% 97%);
  background: transparent;
  border: 3px solid hsl(0 0% 97%);
  border-radius: 0.375rem;
  padding: 0.5rem 1rem;
  cursor: pointer;
  margin: 1rem auto auto;
}

@media (min-width: 600px) {
  section {
    padding: 2rem;
  }
}
</style>