-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsuiSoundbox.js
152 lines (138 loc) · 4.37 KB
/
gsuiSoundbox.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
"use strict";
class GSSoundbox {
#ctx = null;
#elem = null;
#ctxDest = null;
#buffers = new Map();
#stopItself = false;
#nextId = 0;
constructor() {
this.#ctx = new AudioContext();
this.#ctxDest = this.#ctx.createGain();
this.#ctxDest.connect( this.#ctx.destination );
}
// .........................................................................
init( el ) {
this.#elem = el;
el.classList.add( "gsuiSoundbox" );
document.body.addEventListener( "dragover", e => e.preventDefault(), false );
document.body.addEventListener( "drop", e => {
e.preventDefault();
this.#ctx.resume();
GSUgetFilesDataTransfert( e.dataTransfer.items ).then( fs => this.#loadFiles( fs ) );
}, false );
el.addEventListener( "mousedown", e => {
if ( e.target.classList.contains( "gsuiSoundbox-cell" ) ) {
if ( e.button === 0 ) {
this.#playFile( e.target.dataset.id );
} else if ( e.button === 2 ) {
this.#stopFile( this.#stopItself ? null : e.target.dataset.id );
}
}
}, false );
el.addEventListener( "contextmenu", e => e.preventDefault(), false );
el.addEventListener( "transitionend", e => {
if ( e.target.classList.contains( "gsuiSoundbox-cell-cursor" ) ) {
e.target.remove();
}
}, false );
}
clear() {
this.#buffers.forEach( obj => obj.absnList.forEach( absn => absn.stop() ) );
this.#buffers.clear();
this.#elem.querySelectorAll( ".gsuiSoundbox-cell" ).forEach( c => c.remove() );
}
stopItself( b ) {
this.#stopItself = b;
}
setGain( v ) {
this.#ctxDest.gain.value = v;
}
// .........................................................................
#playFile( id ) {
const absn = this.#ctx.createBufferSource();
const obj = this.#buffers.get( id );
if ( this.#stopItself ) {
this.#stopFile();
}
absn.buffer = obj.buffer;
absn.connect( this.#ctxDest );
absn.start();
obj.absnList.push( absn );
this.#createCursor( id, obj.buffer.duration );
}
#stopFile( id ) {
const cursors = this.#elem.querySelectorAll( ( id ? `[data-id="${ id }"] ` : "" ) + ".gsuiSoundbox-cell-cursor" );
cursors.forEach( el => el.remove() );
if ( id ) {
this.#buffers.get( id ).absnList.forEach( absn => absn.stop() );
} else {
this.#buffers.forEach( obj => obj.absnList.forEach( absn => absn.stop() ) );
}
}
#createCursor( id, dur ) {
const cell = this.#elem.querySelector( `[data-id="${ id }"] .gsuiSoundbox-cell-wave` );
const cursor = GSUcreateElement( "div", { class: "gsuiSoundbox-cell-cursor" } );
cursor.style.transitionDuration = `${ dur * 3 }s`;
cell.append( cursor );
setTimeout( () => cursor.style.left = "300%", 1 );
}
#getNextId() {
return `${ this.#nextId++ }`;
}
#loadFiles( files ) {
const proms = Array.from( files ).map( f => {
return new Promise( res => {
const id = this.#getNextId();
this.#loadFile( id, f ).then( buf => {
if ( buf ) {
const cell = GSSoundbox.#createCell( id, f.name, buf.duration );
this.#buffers.set( id, {
buffer: buf,
absnList: [],
} );
GSSoundbox.#drawWave( id, buf, cell.querySelector( "svg" ) );
res( cell );
}
res( null );
} );
} );
} );
Promise.all( proms ).then( cells => this.#elem.append( ...cells.filter( Boolean ) ) );
}
#loadFile( id, file ) {
return new Promise( res => {
const rd = new FileReader();
rd.onload = e => {
this.#ctx.decodeAudioData( e.target.result ).then( res, () => res( null ) );
};
rd.readAsArrayBuffer( file );
} );
}
static #createCell( id, name, dur ) {
const title = `${ name } (${ GSSoundbox.#formatDuration( dur ) })`;
return GSUcreateElement( "button", { class: "gsuiSoundbox-cell", "data-id": id, title },
GSUcreateElement( "div", { class: "gsuiSoundbox-cell-wave" },
GSUcreateElementSVG( "svg" ),
),
GSUcreateElement( "div", { class: "gsuiSoundbox-cell-info" },
GSUcreateElement( "div", { class: "gsuiSoundbox-cell-title" }, GSSoundbox.#formatTitle( name ) ),
GSUcreateElement( "div", { class: "gsuiSoundbox-cell-duration" }, GSSoundbox.#formatDuration( dur ) ),
),
);
}
static #formatTitle( name ) {
const lastPnt = name.lastIndexOf( "." );
return lastPnt > -1
? name.substring( 0, lastPnt )
: name;
}
static #formatDuration( dur ) {
return `${ dur.toFixed( 3 ) }s`;
}
static #drawWave( id, buf, svg ) {
const uiWave = new gsuiWaveform( svg );
uiWave.$setResolution( 300, 60 );
uiWave.drawBuffer( buf );
}
}