@grame/faust-web-component 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/scope.ts DELETED
@@ -1,205 +0,0 @@
1
- // Based on https://codepen.io/ContemporaryInsanity/pen/Mwvqpb
2
-
3
- export class Scope {
4
- container: HTMLElement
5
- canvas: HTMLCanvasElement
6
- ctx: CanvasRenderingContext2D
7
-
8
- constructor(container: HTMLElement) {
9
- this.container = container
10
- this.container.classList.add("scope")
11
-
12
- this.canvas = document.createElement("canvas")
13
- this.canvas.style.transformOrigin = "top left"
14
- this.ctx = this.canvas.getContext("2d")!
15
- this.onResize = this.onResize.bind(this)
16
-
17
- this.container.appendChild(this.canvas)
18
- this.onResize()
19
- window.addEventListener("resize", this.onResize)
20
- }
21
-
22
- get canvasWidth() { return this.canvas.width / devicePixelRatio }
23
- set canvasWidth(canvasWidth) {
24
- this.canvas.width = Math.floor(canvasWidth * devicePixelRatio)
25
- this.canvas.style.width = `${canvasWidth}px`
26
- }
27
-
28
- get canvasHeight() { return this.canvas.height / devicePixelRatio }
29
- set canvasHeight(canvasHeight) {
30
- this.canvas.height = Math.floor(canvasHeight * devicePixelRatio)
31
- this.canvas.style.height = `${canvasHeight}px`
32
- }
33
-
34
- renderScope(toRender: { analyser: AnalyserNode, style: string, edgeThreshold: number }[] = []) {
35
- // grid
36
- this.ctx.fillStyle = "white"
37
- this.ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight)
38
- this.ctx.lineWidth = 1
39
- this.ctx.strokeStyle = "rgba(200, 200, 200, 0.5)"
40
- this.ctx.fillStyle = "rgba(200, 200, 200, 0.5)"
41
- this.ctx.beginPath()
42
-
43
- const numHorzSteps = 8
44
- const horzStep = this.canvasWidth / numHorzSteps
45
- for (let i = horzStep; i < this.canvasWidth; i += horzStep) {
46
- this.ctx.moveTo(i, 0)
47
- this.ctx.lineTo(i, this.canvasHeight)
48
- }
49
-
50
- const numVertSteps = 4
51
- const vertStep = this.canvasHeight / numVertSteps
52
- for (let i = 0; i < this.canvasHeight; i += vertStep) {
53
- this.ctx.moveTo(0, i)
54
- this.ctx.lineTo(this.canvasWidth, i)
55
- }
56
- this.ctx.stroke()
57
-
58
- // 0 line
59
- this.ctx.strokeStyle = "rgba(100, 100, 100, 0.5)"
60
- this.ctx.beginPath()
61
- this.ctx.lineWidth = 2
62
- this.ctx.moveTo(0, this.canvasHeight / 2)
63
- this.ctx.lineTo(this.canvasWidth, this.canvasHeight / 2)
64
- this.ctx.stroke()
65
-
66
- // waveforms
67
- toRender.forEach(({ analyser, style = "rgb(43, 156, 212)", edgeThreshold = 0 }) => {
68
- if (analyser === undefined) { return }
69
-
70
- const timeData = new Float32Array(analyser.frequencyBinCount)
71
- let risingEdge = 0
72
-
73
- analyser.getFloatTimeDomainData(timeData)
74
-
75
- this.ctx.lineWidth = 2
76
- this.ctx.strokeStyle = style
77
-
78
- this.ctx.beginPath()
79
-
80
- while (timeData[risingEdge] > 0 &&
81
- risingEdge <= this.canvasWidth &&
82
- risingEdge < timeData.length) {
83
- risingEdge++
84
- }
85
-
86
- if (risingEdge >= this.canvasWidth) { risingEdge = 0 }
87
-
88
-
89
- while (timeData[risingEdge] < edgeThreshold &&
90
- risingEdge <= this.canvasWidth &&
91
- risingEdge< timeData.length) {
92
- risingEdge++
93
- }
94
-
95
- if (risingEdge >= this.canvasWidth) { risingEdge = 0 }
96
-
97
- for (let x = risingEdge; x < timeData.length && x - risingEdge < this.canvasWidth; x++) {
98
- const y = this.canvasHeight - (((timeData[x] + 1) / 2) * this.canvasHeight)
99
- this.ctx.lineTo(x - risingEdge, y)
100
- }
101
-
102
- this.ctx.stroke()
103
- })
104
-
105
- // markers
106
- this.ctx.fillStyle = "black"
107
- this.ctx.font = "11px Courier"
108
- this.ctx.textAlign = "left"
109
- const numMarkers = 4
110
- const markerStep = this.canvasHeight / numMarkers
111
- for (let i = 0; i <= numMarkers; i++) {
112
- this.ctx.textBaseline =
113
- i === 0 ? "top"
114
- : i === numMarkers ? "bottom"
115
- : "middle"
116
-
117
- const value = ((numMarkers - i) - (numMarkers / 2)) / numMarkers * 2
118
- this.ctx.textAlign = "left"
119
- this.ctx.fillText(value + "", 5, i * markerStep)
120
- this.ctx.textAlign = "right"
121
- this.ctx.fillText(value + "", this.canvasWidth - 5, i * markerStep)
122
- }
123
- }
124
-
125
- renderSpectrum(analyser: AnalyserNode) {
126
- const freqData = new Uint8Array(analyser.frequencyBinCount)
127
-
128
- analyser.getByteFrequencyData(freqData)
129
-
130
- this.ctx.fillStyle = "white"
131
- this.ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight)
132
-
133
- this.ctx.lineWidth = 2
134
- this.ctx.strokeStyle = "rgb(43, 156, 212)"
135
- this.ctx.beginPath()
136
-
137
- for (let i = 0; i < freqData.length; i++) {
138
- const x = (Math.log(i / 1)) / (Math.log(freqData.length / 1)) * this.canvasWidth
139
- const height = (freqData[i] * this.canvasHeight) / 256
140
- this.ctx.lineTo(x, this.canvasHeight - height)
141
- }
142
- this.ctx.stroke()
143
-
144
- const fontSize = 12
145
-
146
- // frequencies
147
- function explin(value: number, inMin: number, inMax: number, outMin: number, outMax: number) {
148
- inMin = Math.max(inMin, 1)
149
- outMin = Math.max(outMin, 1)
150
- return Math.log10(value / inMin) / Math.log10(inMax / inMin) * (outMax - outMin) + outMin
151
- }
152
-
153
- const nyquist = analyser.context.sampleRate / 2;
154
- [0, 100, 300, 1000, 3000, 10000, 20000].forEach(freq => {
155
- const minFreq = 20
156
- const x = freq <= 0
157
- ? fontSize - 5
158
- : explin(freq, minFreq, nyquist, 0, this.canvasWidth)
159
-
160
- this.ctx.fillStyle = "black"
161
- this.ctx.textBaseline = "middle"
162
- this.ctx.textAlign = "right"
163
- this.ctx.font = `${fontSize}px Courier`
164
- this.ctx.save()
165
- this.ctx.translate(x, this.canvasHeight - 5)
166
- this.ctx.rotate(Math.PI * 0.5)
167
- this.ctx.fillText(`${freq.toFixed(0)}hz`, 0, 0)
168
- this.ctx.restore()
169
- });
170
-
171
- [0, -3, -6, -12].forEach(db => {
172
- const x = 5
173
- const amp = Math.pow(10, db * 0.05)
174
- const y = (1 - amp) * this.canvasHeight
175
-
176
- this.ctx.fillStyle = "black"
177
- this.ctx.textBaseline = "top"
178
- this.ctx.textAlign = "left"
179
- this.ctx.font = `${fontSize}px Courier`
180
- this.ctx.fillText(`${db.toFixed(0)}db`, x, y)
181
- })
182
- }
183
-
184
- onResize() {
185
- this.canvasWidth = 0
186
- this.canvasHeight = 0
187
-
188
- const rect = this.container.getBoundingClientRect()
189
- const style = getComputedStyle(this.container)
190
-
191
- let borderLeft = style.getPropertyValue("border-left-width")
192
- let left = borderLeft === "" ? 0 : parseFloat(borderLeft)
193
- let borderRight = style.getPropertyValue("border-right-width")
194
- let right = borderRight === "" ? 0 : parseFloat(borderRight)
195
- this.canvasWidth = rect.width - left - right
196
-
197
- let borderTop = style.getPropertyValue("border-top-width")
198
- let top = borderTop === "" ? 0 : parseFloat(borderTop)
199
- let borderBottom = style.getPropertyValue("border-bottom-width")
200
- let bottom = borderBottom === "" ? 0 : parseFloat(borderBottom)
201
- this.canvasHeight = rect.height - top - bottom
202
-
203
- this.ctx.scale(devicePixelRatio, devicePixelRatio)
204
- }
205
- }
package/src/vite-env.d.ts DELETED
@@ -1 +0,0 @@
1
- /// <reference types="vite/client" />
package/tsconfig.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "useDefineForClassFields": true,
5
- "module": "ESNext",
6
- "lib": ["ES2020", "DOM", "DOM.Iterable"],
7
- "skipLibCheck": true,
8
-
9
- /* Bundler mode */
10
- "moduleResolution": "bundler",
11
- "allowImportingTsExtensions": true,
12
- "resolveJsonModule": true,
13
- "isolatedModules": true,
14
- "noEmit": true,
15
-
16
- /* Linting */
17
- "strict": true,
18
- "noUnusedLocals": true,
19
- "noUnusedParameters": true,
20
- "noFallthroughCasesInSwitch": true
21
- },
22
- "include": ["src"]
23
- }
package/vite.config.js DELETED
@@ -1,13 +0,0 @@
1
- import { resolve } from 'path'
2
- import { defineConfig } from 'vite'
3
-
4
- export default defineConfig({
5
- build: {
6
- lib: {
7
- entry: resolve(__dirname, "src/main.ts"),
8
- name: "faust_web_component",
9
- formats: ["iife"],
10
- fileName: () => "faust-web-component.js",
11
- },
12
- },
13
- })