forked from directmusic/wav2png
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
657 lines (579 loc) · 23.3 KB
/
index.html
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>wav2png - audio to image converter</title>
<meta name="keywords" content="wav2png, audio2image, audio, image, converter, photoshop filters, datamoshing, joseph lyncheski, directmusic">
<!-- FACEBOOK -->
<meta property="og:title" content="wav2png - audio to image converter">
<meta property="og:site_name" content="wav2png - audio to image converter">
<meta property="og:url" content="https://directmusic.me/wav2png">
<meta property="og:description" content="convert audio to images for datamoshing audio fun.">
<!--<meta property="og:image" content="http://static.tumblr.com/b2cbxhu/X2Nofa3eh/directpresspic.jpg">-->
<!-- TWITTER -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@directofficial" />
<meta name="twitter:title" content="wav2png - audio to image converter" />
<meta name="twitter:description" content="convert audio to images for datamoshing audio fun." />
<style>
@import url('https://fonts.googleapis.com/css2?family=Source+Code+Pro:wght@200;300;400;500&display=swap');
:root {
--bg: rgb(0, 0, 0);
--bg-accent: rgb(43, 48, 55);
--text: rgb(255, 255, 255);
--color: rgb(172, 192, 222);
--color-transparent: rgba(172, 192, 222, 0.15);
--accent: rgb(86, 96, 111);
--accent-transparent: rgba(86, 96, 111, 0.5);
--btn-active: rgba(0, 0, 0, 0);
}
* {
margin: 0;
padding: 0
}
body {
width: 100vw;
height: 100vh;
background-color: var(--bg);
color: var(--text);
font-family: 'Source Code Pro', monospace;
overflow-x: hidden;
}
a {
color: var(--color);
text-decoration: none;
border-bottom: 0.1em solid var(--color);
}
a:hover {
color: var(--color);
text-decoration: none;
border-bottom: 0.1em solid var(--color);
}
button {
font-family: 'Source Code Pro', monospace;
font-size: 1em;
color: var(--text);
background: var(--accent-transparent);
border: none;
padding: 1em;
margin-top: 0.5em;
}
button:hover {
background: var(--color-transparent);
}
button:active {
background: var(--btn-active);
}
@media only screen and (min-width: 600px) {
.grid_container {
display: grid;
padding: 1em;
grid-gap: 1em;
width: calc(100vw - 3em);
grid-template-columns: 50% 50%;
grid-template-areas:
"image audio";
}
}
@media only screen and (max-width: 600px) {
.grid_container {
display: grid;
padding: 1em;
grid-gap: 1em;
width: calc(100vw - 2em);
grid-template-columns: 100%;
grid-template-areas:
"image"
"audio";
}
}
/* png2wav wav2png text */
.title_text {
color: var(--color);
margin-bottom: 0.5em;
font-size: 1.25em;
}
.canvas_container {
display: inline-block;
}
.canvas_container .bg_text {
position: relative;
}
.canvas_container .bg_text:after {
position: absolute;
font-size: 1.4em;
width: calc(100% - 3rem);
height: calc(17rem - 3rem);
border: var(--color) dashed 0.5rem;
text-align: center;
padding: 13rem 1rem 1rem 1rem;
}
.canvas_container canvas {
height: 30em;
width: 100%;
z-index: 1;
position: relative;
}
/* png2wav */
#imageContainer {
grid-area: "image";
padding: 1em;
background: var(--bg-accent);
transition: 0.5s background-color;
}
#imageContainer .bg_text:after {
content: "Drag .wav here";
}
/* wav2png */
#audioContainer {
grid-area: "audio";
padding: 1em;
background: var(--bg-accent);
transition: 0.5s background-color;
}
#audioContainer .bg_text:after {
content: "Drag .png here";
}
#loading {
background: var(--accent-transparent);
position: fixed;
line-height: 100vh;
text-align: center;
font-size: 3em;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
opacity: 0;
transition: 0.5s opacity;
pointer-events: none;
z-index: 100;
}
#notchrome {
background: var(--accent-transparent);
position: fixed;
text-align: center;
font-size: 3em;
padding-top: 10vh;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: none;
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
}
.faq {
max-width: 50em;
padding: 1em 2em 1em 2em;
border-bottom: var(--accent-transparent) solid 1px;
}
.faq p {
text-indent: 1em;
margin-left: 1em;
}
h2 {
padding: 1em 0 0.5em 0;
}
.changelog {
max-width: 50em;
padding: 1em 2em 1em 2em;
opacity: 0.75;
}
.tooltip {
position: relative;
display: inline-block;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 20em;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
padding: 1em;
position: absolute;
z-index: 10;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
.block {
background-color: var(--bg-accent);
padding: 1em;
margin: 1rem 0 0 1rem;
float: left;
}
/* nothing can be to the right of this */
.hbox {
display: inline-block;
width: 100%;
}
/* TITLE */
#title {
word-wrap: break-word;
}
.subtext {
font-size: 0.75em;
opacity: 0.5;
}
#madewithlove {
opacity: 0.9;
}
#madewithlove li::after {
content: " | ";
}
#madewithlove li:last-child:after {
content: "";
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline-block;
}
</style>
</head>
<body>
<div class="hbox">
<div class="block">
<span id="title">wav2png</span> <span class="subtext">&back</span>
</div>
<div class="block">
<ul id="madewithlove">
<li>made with ❤️ by</li>
<li><a href="https://twitter.com/directofficial" target="_blank">@directofficial</a></li>
<li><a href="https://minimeters.app" target="_blank"> get minimeters</a></li>
<li><a href="https://www.paypal.com/paypalme/directmusic" target="_blank"> donate on paypal</a></li>
</ul>
</div>
</div>
<div class="hbox">
<div class="block">
<span>choose channel:
<div class="tooltip">ⓘ <span class="tooltiptext">Pick the channel which will be converted to PNG.</span>
</div>
</span>
<input type="radio" id="left" name="channel" value="0" checked>
<label for="left">left</label>
<input type="radio" id="right" name="channel" value="1">
<label for="right">right</label>
</div>
</div>
<canvas width="3000" height="500" id="imgRenderingCanvasLeft" hidden></canvas>
<div class="grid_container">
<div id="imageContainer">
<div id="imgDropHandler">
<div class="title_text">
wav > png:
<div class="tooltip">ⓘ <span class="tooltiptext">Drag a .wav file here to convert it to a PNG.
<br><br>
This takes the left channel of the .wav file and assigns color values based off each sample.
<br><br>
Download the PNG file and open it in an image editor. Then once you're finished editing it
drag it back to this page!</span>
</div>
<input type="file" id="wavFileInput">
</div>
<div class="canvas_container">
<div class="bg_text"></div>
<canvas width="3000" height="500" id="audioDisplay"></canvas>
</div>
<button onclick="downloadChannel('left', 'channel', leftChannelCanvas)">Download .png file</button>
</div>
</div>
<div id="audioContainer">
<div id="audioDropHandler">
<div class="title_text">
png > wav:
<div class="tooltip">ⓘ <span class="tooltiptext">Drag a .png file here to convert it to a .wav
file. <br><br>
This essentially converts the image back to audio. Process it how you please before dragging
it into here.<br><br>
You can drag any image into here but it may sound like trash or be extremely loud so be
careful and turn down your speakers.</span>
</div>
<input type="file" id="pngFileInput">
</div>
<div class="canvas_container">
<div class="bg_text"></div>
<canvas width="3000" height="500" id="pngDisplay"></canvas>
</div>
<button onclick="downloadAudio();">Download .wav file</button>
</div>
</div>
</div>
<div id="loading">
Loading...
</div>
<!-- Displays if the user is using Safari :( -->
<div id="notchrome">
wav2png does not support your browser.<br>
<span style="font-size: 0.5em">if you think you're experiencing a glitch let me know on twitter
@directofficial</span>
</div>
<div class="hbox">
<div class="faq">
<h2>what is this?</h2>
<p>wav2png is a way to convert audio files to png to allow for the use of various photo filters on audio.
this
page also allows you to convert back to audio for use in your songs and projects.</p>
<h2>what image filters work best?</h2>
<p>i love playing with photoshop's blur filters like field blur and radial blur!</p>
<h2>you're going to steal my song huh?</h2>
<p>no way. the conversion is performed in your browser. the site is completely static. you can download it
and use it offline.</p>
<h2>oh cool thanks</h2>
<p>no problem. if you use this in a tune i'd love to know about it. hit me up on twitter <a href="https://twitter.com/directofficial">(@directofficial)</a>. or if you feel generous donate on
paypal :)</p>
<h2>i have an idea!</h2>
<p>sweet. wav2png is open source and available on <a href="https://github.com/directmusic/wav2png">GitHub</a>. feel free to open an issue! or if you want
to code
it yourself i welcome pull requests.</p>
</div>
<div class="changelog">
<h2>changelog</h2>
<p>06/05/2022 - Fix regression: Audio speed is correct on systems running at sample rates other than 44.1khz.</p>
<p>06/03/2022 - better mobile layout, cleaned up some code, rewrote wav file handler, first public version
availble on <a href="https://github.com/directmusic/wav2png">GitHub</a>.</p>
<p>05/13/2021 - added file form input to (kinda) support mobile users</p>
<p>04/30/2021 - jpg to wav conversion support</p>
<p>04/27/2021 - channel selector addition + firefox support (kinda)</p>
<p>04/27/2021 - initial release</p>
</div>
<footer class="changelog">
made by joseph lyncheski.
</footer>
</div>
<script>
// if (!window.chrome && !(navigator.userAgent.indexOf("Firefox") > 0)) {
// notchrome.style.display = "block";
// }
audioContainer.ondragover = audioContainer.ondragenter = function (evt) {
evt.preventDefault();
audioContainer.style.backgroundColor = "var(--accent)";
}
audioContainer.ondragleave = function (evt) {
evt.preventDefault();
audioContainer.style.backgroundColor = "var(--bg-accent)";
}
imgDropHandler.ondragover = imgDropHandler.ondragenter = function (evt) {
evt.preventDefault();
imageContainer.style.backgroundColor = "var(--accent)";
};
imgDropHandler.ondragleave = function (evt) {
evt.preventDefault();
imageContainer.style.backgroundColor = "var(--bg-accent)";
}
// Handles the input file element
class hackedFileThing {
constructor() {
this.dataTransfer;
}
preventDefault() {
console.log("File chosen; not dropped. No need to prevent default");
};
}
function handleFiles() {
var fileList = new hackedFileThing;
fileList.dataTransfer = this;
console.log(fileList.dataTransfer);
fileInput(fileList);
}
// Handles the drop zone
imgDropHandler.ondrop = audioContainer.ondrop = fileInput = function (evt) {
//restore colors
audioContainer.style.backgroundColor = "var(--bg-accent)";
imageContainer.style.backgroundColor = "var(--bg-accent)";
loading.style.opacity = "1";
evt.preventDefault();
let file = evt.dataTransfer.files[0];
let reader = new FileReader();
if (evt.dataTransfer.files.length > 0) {
if (file.name.toLowerCase().includes(".png") || file.name.toLowerCase().includes(".jpg") || file.name.toLowerCase().includes(".jpeg")) {
reader.readAsDataURL(file);
reader.onload = function (e2) {
// Start PNG Process
img2audio(e2.target.result);
}
} else if (file.name.includes(".wav")) {
reader.readAsArrayBuffer(file);
reader.onload = function (e2) {
// Start WAV Process
audio2img(e2.target.result);
}
} else {
console.error("FILE FORMAT INVALID");
loading.style.opacity = "0";
}
} else {
console.error("no files were dragged in. did you try to drag a link in?");
loading.style.opacity = "0";
}
};
const wavInput = document.getElementById("wavFileInput");
wavInput.addEventListener("change", handleFiles, false);
const pngInput = document.getElementById("pngFileInput");
pngInput.addEventListener("change", handleFiles, false);
function clamp(num, min, max) {
return num <= min ? min : num >= max ? max : num;
}
let leftChannelCanvas = document.getElementById("imgRenderingCanvasLeft");
var leftCtx = leftChannelCanvas.getContext("2d");
var audioCanvas = document.getElementById("audioDisplay");
var ctx = audioCanvas.getContext("2d");
var pngCanvas = document.getElementById("pngDisplay");
var pngContext = pngCanvas.getContext("2d");
const audioCtx = new AudioContext({
sampleRate: 44100,
});
let buffer = null;
let leftChannelAudio = new Array();
const audio2img = (undecodedAudio) => {
audioCtx.decodeAudioData(undecodedAudio, function (data) {
// success
buffer = data;
let leftChannelPixels = new Array();
var channelSelector = document.getElementsByName('channel');
let selectedChannel = 0;
for (i = 0; i < channelSelector.length; i++) {
if (channelSelector[i].checked) {
selectedChannel = i;
}
}
console.log(selectedChannel);
console.log("Number Of Channels: " + buffer.numberOfChannels);
if (selectedChannel > buffer.numberOfChannels - 1) {
selectedChannel = 0;
console.error("this audio file does not have a right channel. defaulting to left");
}
let bufL = buffer.getChannelData(selectedChannel);
// let buf = (buffer.getChannelData(0) + buffer.getChannelData(1))/ 2;
for (let i = 0; i < buffer.length; i++) {
leftChannelPixels.push(((clamp(bufL[i], -1, 1) + 1) * 0.5) * 768); //
leftChannelAudio.push(bufL[i]);
}
// console.log(stride.value);
buildImage(leftChannelPixels, 3000, ctx);
buildImage(leftChannelPixels, 3000, leftCtx);
loading.style.opacity = "0";
}, function () {
// error
console.error("COULDN'T CONVERT AUDIO TO IMAGE. CORRECT FORMAT?");
});
}
let link = document.createElement("a");
function buildImage(audioArray, stride, context) {
let imgHeight = audioArray.length / stride;
let imageData = context.createImageData(stride, imgHeight);
let index = 0;
for (let y = 0; y < imgHeight; y++) {
for (let x = 0; x < stride; x++) {
imageData.data[4 * stride * y + 4 * x + 0] = clamp(audioArray[index], 0, 255);
imageData.data[4 * stride * y + 4 * x + 1] = clamp(audioArray[index] - 256, 0, 255);
imageData.data[4 * stride * y + 4 * x + 2] = clamp(audioArray[index] - 512, 0, 255);
imageData.data[4 * stride * y + 4 * x + 3] = 255
index++;
}
}
// resize canvas
// the main canvas should just be a display
context.canvas.width = stride;
context.canvas.height = imgHeight;
context.putImageData(imageData, 0, 0);
}
function downloadChannel(filename, channel, currentCanvas) {
link.download = filename + "_" + channel + ".png";
currentCanvas.toBlob(function (blob) {
link.href = URL.createObjectURL(blob);
link.click();
}, "image/png");
}
const SAMPLE_RATE = 44100;
const BITRATE = 16;
const N_CHANNELS = 1;
const WAV_HEADER_SIZE = 44; // 44 bytes
function build_wav_header(buf, audio_length) {
// RIFF
return buf;
}
function write_wav_file(samples) {
let buffer = new ArrayBuffer(WAV_HEADER_SIZE + samples.length * 2);
let data = new DataView(buffer);
data.setUint8(0, 'R'.charCodeAt(0));
data.setUint8(1, 'I'.charCodeAt(0));
data.setUint8(2, 'F'.charCodeAt(0));
data.setUint8(3, 'F'.charCodeAt(0));
data.setUint32(4, 36 + samples.length * N_CHANNELS, true);
// WAV
data.setUint8(8, 'W'.charCodeAt(0));
data.setUint8(9, 'A'.charCodeAt(0));
data.setUint8(10, 'V'.charCodeAt(0));
data.setUint8(11, 'E'.charCodeAt(0));
// fmt
data.setUint8(12, 'f'.charCodeAt(0));
data.setUint8(13, 'm'.charCodeAt(0));
data.setUint8(14, 't'.charCodeAt(0));
data.setUint8(15, ' '.charCodeAt(0));
data.setUint32(16, 16, true);
data.setUint16(20, 1, true); // Audio format 1 = PCM
data.setUint16(22, N_CHANNELS, true);
data.setUint32(24, SAMPLE_RATE, true);
data.setUint32(28, SAMPLE_RATE * N_CHANNELS * BITRATE / 8, true);
data.setUint16(32, N_CHANNELS * BITRATE / 8, true);
data.setUint16(34, BITRATE, true);
data.setUint8(36, 'd'.charCodeAt(0));
data.setUint8(37, 'a'.charCodeAt(0));
data.setUint8(38, 't'.charCodeAt(0));
data.setUint8(39, 'a'.charCodeAt(0));
data.setUint32(40, samples.length * 2 * N_CHANNELS, true);
console.log("samples length: ", samples.length);
for (let i = 0; i < samples.length; i++) {
let sample = samples[i];
data.setInt16(WAV_HEADER_SIZE + i * 2, sample < 0 ? sample * 0x8000 : sample * 0x7FFF, true);
}
return data;
}
// AUDIO
let currentAudioBlob = null;
function exportWAV(type, floatAudioBuffer, audioLength) {
var channel = 0;
var i = 0, offset = 0, newbuffers = [];
newbuffers[channel] = new Float32Array(audioLength);
newbuffers[channel].set(floatAudioBuffer);
var interleaved = newbuffers[0];
var dataview = write_wav_file(floatAudioBuffer);
var audioBlob = new Blob([dataview], { type: type });
currentAudioBlob = audioBlob;
this.postMessage(audioBlob);
}
function downloadAudio() {
link.download = "audio.wav";
link.href = URL.createObjectURL(currentAudioBlob);
link.click();
}
const img2audio = (imgBlob) => {
var img = new Image();
img.src = imgBlob;
img.onload = function () {
console.log(img.width);
pngContext.canvas.width = img.width;
pngContext.canvas.height = img.height;
pngContext.drawImage(img, 0, 0);
let imageData = pngContext.getImageData(0, 0, img.width, img.height).data;
let newImageData = Array();
for (let i = 0; i < imageData.length; i += 4) {
newImageData.push((((imageData[i] + imageData[i + 1] + imageData[i + 2]) / 768) - 0.5) * 2);
}
loading.style.opacity = "0";
exportWAV("audio/wav", newImageData, newImageData.length);
};
}
</script>
</body>
</html>