-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-api.js
76 lines (54 loc) · 1.63 KB
/
server-api.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
import Shared32Array from './shared32array.js'
const API_URL = !location.port ? location.origin : 'http://localhost:3000'
const mode = 'cors'
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
export const URL = API_URL
export const recent = async () => {
const url = API_URL + '/recent'
const res = await fetch(url, { mode, headers })
const json = await res.json()
return json
}
export const load = async (title) => {
const url = title[0] === '.' ? title : API_URL + '/' + title
const res = await fetch(url, { mode, headers })
const json = await res.json()
return json
}
export const save = async (projectJson) => {
const url = API_URL + '/p' // + projectJson.title
const res = await fetch(url, {
method: 'POST',
mode,
headers,
body: JSON.stringify(projectJson, null, 2)
})
const json = await res.json()
return json
}
let samples = new Map
export const fetchSample = async (audio, remoteUrl) => {
const url = getFetchUrl(remoteUrl)
let sample = samples.get(remoteUrl)
if (!sample) {
const res = await fetch(url)
const arrayBuffer = await res.arrayBuffer()
const audioBuffer = await audio.decodeAudioData(arrayBuffer)
const floats = Array(audioBuffer.numberOfChannels).fill(0)
.map((_, i) => audioBuffer.getChannelData(i))
sample = floats.map(buf => {
const shared = new Shared32Array(buf.length)
shared.set(buf)
return shared
})
samples.set(remoteUrl, sample)
}
return sample
}
export const getFetchUrl = (remoteUrl) => {
const url = API_URL + '/fetch?url=' + encodeURIComponent(remoteUrl)
return url
}