Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
195 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/********************** public functions *********************/ | ||
function playSound(id) | ||
{ | ||
var snd=getObject(4,id); | ||
var f=new GFile(snd); | ||
var sound=undefined; | ||
switch (snd.charCodeAt(7)) | ||
{ | ||
case 0: //straight 8 | ||
sound=doSampled(f); | ||
break; | ||
case 1: //MIDI | ||
break; | ||
case 2: //compressed 4 | ||
sound=doCompressed(f); | ||
break; | ||
} | ||
if (sound==undefined) return 0; | ||
if (soundSupport && !muted) | ||
sound.play(); | ||
return sound.time(); | ||
} | ||
|
||
/********************** private functions *********************/ | ||
|
||
function doSampled(f) | ||
{ | ||
var soundlen=0; | ||
var numParts=f.r16(); | ||
for (var part=0;part<numParts;part++) | ||
{ | ||
f.r16(); //sampleDataOffset | ||
soundlen+=f.r16(); //sampleLen | ||
f.r16(); //type | ||
f.r16(); | ||
} | ||
f.seek(8,f.set); | ||
var freq=f.r16(); | ||
var sound=new Sound(soundlen,freq); | ||
var pos=0; | ||
var ch; | ||
for (var part=0;part<numParts;part++) | ||
{ | ||
f.seek(2+part*8,f.set); | ||
var ofs=f.r16(); | ||
var len=f.r16(); | ||
f.seek(ofs,f.set); | ||
for (var i=0;i<len;i++) | ||
sound.data[pos++]=f.r8(); | ||
} | ||
return sound; | ||
} | ||
var wavtable=[0x7f,0x7e,0x7c,0x78,0x70,0x60,0x40,0x01, | ||
0x80,0x81,0x83,0x87,0x8f,0x9f,0xbf,0xff]; | ||
function doCompressed(f) | ||
{ | ||
var soundlen=0; | ||
var numParts=f.r16(); | ||
for (var part=0;part<numParts;part++) | ||
{ | ||
f.r16(); //sampleDataOffset | ||
soundlen+=f.r16()*2; //sampleLength | ||
f.r16(); //type | ||
f.r16(); //freq | ||
} | ||
f.seek(8,f.set); | ||
var freq=f.r16(); | ||
var sound=new Sound(soundlen,freq); | ||
var pos=0; | ||
for (var part=0;part<numParts;part++) | ||
{ | ||
f.seek(2+part*8,f.set); | ||
var ofs=f.r16(); | ||
var len=f.r16()*2; | ||
f.seek(ofs,f.set); | ||
var ch; | ||
for (var i=0;i<len;i++) | ||
{ | ||
if (i&1) ch>>=4; | ||
else ch=f.r8(); | ||
sound.data[pos++]=wavtable[ch&0xf]; | ||
} | ||
} | ||
return sound; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/********************** public functions *********************/ | ||
|
||
|
||
/********************** private functions *********************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/********************** public functions *********************/ | ||
var soundSupport=false; | ||
var muted=false; | ||
|
||
try { | ||
var type=document.createElement('audio').canPlayType('audio/wav'); | ||
soundSupport=type!='' && type!='no'; | ||
} catch (e) {} | ||
|
||
/** | ||
* @constructor | ||
*/ | ||
function Sound(len,freq) | ||
{ | ||
this.data=new Array(len); | ||
this.len=len; | ||
this.freq=freq; | ||
} | ||
Sound.prototype.time=function() | ||
{ | ||
return this.len/this.freq; | ||
} | ||
Sound.prototype.play=function() | ||
{ | ||
var audio=$(document.createElement('audio')); | ||
$(document.body).append(audio); | ||
audio.attr("src","data:audio/x-wav;base64,"+towav(this)); | ||
var vol=getSetting('volume'); | ||
if (vol==null) vol=50; | ||
audio.attr('volume',vol/100); | ||
audio.bind('ended',function(){ | ||
audio.remove(); | ||
}); | ||
audio.get(0).play(); | ||
} | ||
|
||
/********************** private functions *********************/ | ||
|
||
function encode64(input) | ||
{ | ||
var key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"+ | ||
"0123456789+/="; | ||
var output=''; | ||
var chr1,chr2,chr3; | ||
var enc1,enc2,enc3,enc4; | ||
var i=0; | ||
do { | ||
chr1=input[i++]; | ||
chr2=input[i++]; | ||
chr3=input[i++]; | ||
enc1=chr1>>2; | ||
enc2=(chr1&3)<<4; | ||
if (chr2==undefined) | ||
enc3=enc4=64; | ||
else if (chr3==undefined) | ||
{ | ||
enc2|=chr2>>4; | ||
enc3=(chr2&15)<<2; | ||
enc4=64; | ||
} | ||
else | ||
{ | ||
enc2|=chr2>>4; | ||
enc3=((chr2&15)<<2)|(chr3>>6); | ||
enc4=chr3&63; | ||
} | ||
output+=key.charAt(enc1)+ | ||
key.charAt(enc2)+ | ||
key.charAt(enc3)+ | ||
key.charAt(enc4); | ||
} while (i<input.length); | ||
return output; | ||
} | ||
function towav(snd) | ||
{ | ||
var wav=new Array(44+snd.len); | ||
wav[0]=82; wav[1]=73; wav[2]=70; wav[3]=70; //RIFF | ||
var sublen=36+snd.len; | ||
wav[4]=sublen&0xff; wav[5]=(sublen>>8)&0xff; | ||
wav[6]=(sublen>>16)&0xff; wav[7]=(sublen>>24)&0xff; | ||
wav[8]=87; wav[9]=65; wav[10]=86; wav[11]=69; //WAVE | ||
wav[12]=102; wav[13]=109; wav[14]=116; wav[15]=32; //fmt | ||
wav[16]=0x10; wav[17]=0; wav[18]=0; wav[19]=0; //chunksize | ||
wav[20]=1; wav[21]=0; //pcm | ||
wav[22]=1; wav[23]=0; //mono | ||
wav[24]=snd.freq&0xff; wav[25]=(snd.freq>>8)&0xff; | ||
wav[26]=(snd.freq>>16)&0xff; wav[27]=(snd.freq>>24)&0xff; | ||
wav[28]=wav[24]; wav[29]=wav[25]; | ||
wav[30]=wav[26]; wav[31]=wav[27]; //byte rate | ||
wav[32]=1; wav[33]=0; //block align | ||
wav[34]=8; wav[35]=0; //bits per sample | ||
wav[36]=100; wav[37]=97; wav[38]=116; wav[39]=97; //data | ||
wav[40]=snd.len&0xff; wav[41]=(snd.len>>8)&0xff; | ||
wav[42]=(snd.len>>16)&0xff; wav[43]=(snd.len>>24)&0xff; | ||
for (var i=0;i<snd.len;i++) | ||
wav[44+i]=snd.data[i]; | ||
return encode64(wav); | ||
} |