-
Notifications
You must be signed in to change notification settings - Fork 3
/
audioTest.html
413 lines (394 loc) · 9.91 KB
/
audioTest.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
<html>
<script type="text/javascript" src="php.default.min.js"></script>
<script type="text/javascript">
function base64_encode (data,isutf8) {
// Encodes string using MIME base64 algorithm
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/base64_encode // + original by: Tyler Akins (http://rumkin.com)
// + improved by: Bayron Guevara
// + improved by: Thunder.m
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Pellentesque Malesuada // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Rafał Kukawski (http://kukawski.pl)
// - depends on: utf8_encode
// * example 1: base64_encode('Kevin van Zonneveld');
// * returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA==' // mozilla has this native
// - but breaks in 2.0.0.12!
//if (typeof this.window['atob'] == 'function') {
// return atob(data);
//}
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
enc = "",
tmp_arr = [];
if (!data) {
return data;
}
do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);
bits = o1 << 16 | o2 << 8 | o3;
h1 = bits >> 18 & 0x3f; h2 = bits >> 12 & 0x3f;
h3 = bits >> 6 & 0x3f;
h4 = bits & 0x3f;
// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);
enc = tmp_arr.join('');
var r = data.length % 3;
return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
}
// From http://baagoe.com/en/RandomMusings/javascript/
// Johannes Baagøe <baagoe@baagoe.com>, 2010
function Mash() {
var n = 0xefc8249d;
var mash = function(data) {
data = data.toString();
for (var i = 0; i < data.length; i++) {
n += data.charCodeAt(i);
var h = 0.02519603282416938 * n;
n = h >>> 0;
h -= n;
h *= n;
n = h >>> 0;
h -= n;
n += h * 0x100000000; // 2^32
}
return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
};
mash.version = 'Mash 0.9';
return mash;
}
function Alea() {
return (function(args) {
// Johannes Baagøe <baagoe@baagoe.com>, 2010
var s0 = 0;
var s1 = 0;
var s2 = 0;
var c = 1;
if (args.length == 0) {
args = [+new Date];
}
var mash = Mash();
s0 = mash(' ');
s1 = mash(' ');
s2 = mash(' ');
for (var i = 0; i < args.length; i++) {
s0 -= mash(args[i]);
if (s0 < 0) {
s0 += 1;
}
s1 -= mash(args[i]);
if (s1 < 0) {
s1 += 1;
}
s2 -= mash(args[i]);
if (s2 < 0) {
s2 += 1;
}
}
mash = null;
var random = function() {
var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
s0 = s1;
s1 = s2;
return s2 = t - (c = t | 0);
};
random.uint32 = function() {
return random() * 0x100000000; // 2^32
};
random.fract53 = function() {
return random() +
(random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
};
random.version = 'Alea 0.9';
random.args = args;
return random;
} (Array.prototype.slice.call(arguments)));
};
function toBit(length,bits,endianness)
{
var bigPart=length & (~255);
var tempSmallPart=length & (~bigPart);
bigPart=bigPart >> 8;
var buf=String.fromCharCode(tempSmallPart);
while(bigPart>255)
{
var tempLen=bigPart;
bigPart=bigPart & (~255);
tempSmallPart=tempLen & (~bigPart);
bigPart=bigPart >> 8;
if(endianness=="little")
{
buf=buf+String.fromCharCode(tempSmallPart);
}
else
{
buff=String.fromCharCode(tempSmallPart)+buf;
}
}
if(endianness=="little")
{
buf=buf+String.fromCharCode(bigPart);
}
else
{
buff=String.fromCharCode(bigPart)+buf;
}
if(buf.length<(bits/8))
{
var end=(bits/8)-buf.length;
for(var x=0;x<end;x++)
{
if(endianness=="little")
{
buf=buf+String.fromCharCode(0);
}
else
{
buff=String.fromCharCode(0)+buf;
}
}
}
return buf;
}
function getLengthInChars(length)
{
var bigPart=length & (~255);
var tempSmallPart=length & (~bigPart);
bigPart=bigPart >> 8;
var buf=String.fromCharCode(tempSmallPart);
while(bigPart>255)
{
var tempLen=bigPart;
bigPart=bigPart & (~255);
tempSmallPart=tempLen & (~bigPart);
bigPart=bigPart >> 8;
buf=buf+String.fromCharCode(tempSmallPart);
}
buf=buf+String.fromCharCode(bigPart);
if(buf.length<=8)
{
var end=8-buf.length;
for(var x=0;x<end;x++)
{
buf=buf+String.fromCharCode(0);
}
}
else
{
return "";
}
return buf;
}
function makefile()
{
var innerFile=document.getElementById("file");
if(!innerFile.files[0])
{
return;
}
innerFile=innerFile.files[0];
var reader = new FileReader();
reader.onload=function(event)
{
makeImg(event.target.result,innerFile.name);
}
reader.readAsBinaryString(innerFile);
}
function makeImg(file,fileName)
{
var fileLen=file.length;
var nameLen=fileName.length;
if(nameLen>255)
{
nameLen=255;
fileName=fileName.substr(0,255);
}
var outerImg=document.getElementById("img");
if(!outerImg.files[0])
{
return;
}
var imgdat=outerImg.files[0];
var reader = new FileReader();
reader.onload=function(event)
{
try
{
var imageData=event.target.result;
headerObj=readHeader(imageData);
bodyArr=readBody(imageData,headerObj);
var headerString=imageData.substr(0,44);
var newData= new Array();
var pass=prompt("please enter a password");
var rng = new Alea(pass);
for(var i = 0; i<bodyArr[0].length; i++)
{
newData[i]=i;
}
var bodString="";
var bytes=String.fromCharCode(nameLen)+fileName+getLengthInChars(fileLen)+file;
if(bodyArr[0].length/4<bytes.length)
{
alert("innerFile too big: "+(bodyArr.length/4)+": "+bytes.length)
return;
}
var skipped=0;
for(var i = 0; i <= bytes.length; i++)
{
var randnum=Math.floor(rng()*bodyArr[0].length);
randnum=randnum-(randnum%4);
var randByte=bytes.charCodeAt(i);
if(newData[randnum]==-1)
{
if(skipped>10000000)
{
throw("your hands up");
}
i--;
skipped++;
continue;
}
for(var y=0;y<bodyArr.length;y++)
{
bodyArr[y][randnum]=((bodyArr[y][randnum] & (~3)) | ((randByte >> 6)&3));
bodyArr[y][randnum+1]=((bodyArr[y][randnum+1] & (~3)) | ((randByte>>4)&3));
bodyArr[y][randnum+2]=((bodyArr[y][randnum+2] & (~3)) | ((randByte>>2)&3));
bodyArr[y][randnum+3]=((bodyArr[y][randnum+3] & (~3)) | ((randByte)&3));
}
newData[randnum]=-1
}
for(var x=0;x<bodyArr[0].length;x++)
{
for(var y=0;y<bodyArr.length;y++)
{
bodString+=toBit(bodyArr[y][x],headerObj.BPS,"little");
}
}
fileString="data:audio/x-wav;base64,"+base64_encode(headerString+bodString);
var theif=document.createElement("iframe");
theif.src=fileString;
theif.style.display="none";
document.getElementsByTagName("body")[0].appendChild(theif);
}
catch(e)
{
alert(e);
}
}
reader.readAsBinaryString(imgdat);
}
function readHeader(file)
{
var index=0;
var retObj={"index":index};
if(getString(file,retObj.index,4)!="RIFF")
{
throw("RIFF file format not detected");
}
retObj.index+=4;
retObj.index+=4;//dont need to read the length here
if(getString(file,retObj.index,4)!="WAVE")
{
throw("RIFF file format not detected");
}
retObj.index+=4;
if(getString(file,retObj.index,4)!="fmt ")
{
throw("RIFF file format not detected");
}
retObj.index+=4;
if (file.charCodeAt(retObj.index) != 16 || file.charCodeAt(retObj.index+1) || file.charCodeAt(retObj.index+2) || file.charCodeAt(retObj.index+3)) {
throw("PCM format not detected");
}
retObj.index+=4;
if (file.charCodeAt(retObj.index) != 1 || file.charCodeAt(retObj.index+1)) {
throw("PCM format not detected");
}
retObj.index+=2;
retObj.channels=getLittleEndianNum(file,retObj.index,2)
retObj.index+=2;
retObj.sampleRate=getLittleEndianNum(file,retObj.index,4)
retObj.index+=4;
retObj.byteRate=getLittleEndianNum(file,retObj.index,4)
retObj.index+=4;
retObj.blockAlign=getLittleEndianNum(file,retObj.index,2)
retObj.index+=2;
retObj.BPS=getLittleEndianNum(file,retObj.index,2)
retObj.index+=2;
if(retObj.byteRate != ((retObj.sampleRate * retObj.channels * retObj.BPS) >> 3))
{
throw("wtf!");
}
if(retObj.blockAlign != ((retObj.channels * retObj.BPS) >> 3))
{
throw("wtf mate!");
}
if(getString(file,retObj.index,4)!="data")
{
throw("RIFF file format not detected");
}
retObj.index+=4;
retObj.subChunkSize=getLittleEndianNum(file,retObj.index,4)
retObj.index+=4;
retObj.num_samp = (retObj.subChunkSize << 3) / (retObj.channels * retObj.BPS);
return retObj;
}
function readBody(file,headerObj)
{
var buffer;
var data=[];
for(var x=0;x<headerObj.channels;x++)
{
data[x]=new Uint32Array(headerObj.num_samp);
}
for (var i=0; i != headerObj.num_samp; ++i) {
for (var j=0; j != headerObj.channels; ++j) {
var tmp = 0;
for (var k=0; k != headerObj.BPS; k+=8) {
buffer=file.charCodeAt(headerObj.index);
tmp += buffer << k;
headerObj.index++;
}
data[j][i] = conv_bit_size(tmp, headerObj.BPS);
}
}
return data;
}
function conv_bit_size(bytes,bps)
{
var max = (1 << (bps)) - 1;
return ((bytes > max)?bytes - (max<<1) : bytes);
}
function getLittleEndianNum(buffer,index,length)
{
var num=0;
for(var x=index,u=0;x<index+length;x++,u++)
{
num+=buffer.charCodeAt(x)<<(8*u);
}
return num;
}
function getString(buffer,start,length)
{
var retbuff=""
for(var x=start;x<start+length;x++)
{
retbuff+=buffer[x];
}
return retbuff;
}
</script>
<body>
<p>select Outer Audio File (wav):</p>
<p><input type="file" id="img"></p>
<p>select innerFile:</p>
<p><input type="file" id="file"></p>
<button onclick="makefile()">Do it!</button>
<div style="width:1024,height:768;" id="animDiv"></div>
</body>
</html>