Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
maxi-js-emscripten
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
3
Issues
3
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Daniel Clarke
maxi-js-emscripten
Commits
efdb9bd5
Commit
efdb9bd5
authored
Feb 05, 2016
by
Jakub Fiala
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
loadSample now works with base64-encoded samples
parent
ced57668
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
370 additions
and
22 deletions
+370
-22
.gitignore
.gitignore
+2
-1
maxiLib/maxiLib.js
maxiLib/maxiLib.js
+129
-2
maxiLib/maximilian_examples_web/12_SamplePlayer.html
maxiLib/maximilian_examples_web/12_SamplePlayer.html
+4
-5
maxiLib/maximilian_examples_web/23_SamplePlayer_base64.html
maxiLib/maximilian_examples_web/23_SamplePlayer_base64.html
+106
-0
src/js/maxi_webAudio.js
src/js/maxi_webAudio.js
+129
-14
No files found.
.gitignore
View file @
efdb9bd5
...
...
@@ -6,4 +6,5 @@ maxiLib/sed_addText.sh
maxiLib/sed_replace.sh
maxiLib/sed_text_stuff/
maximilian_examples_cpp/
old_stuff/
\ No newline at end of file
old_stuff/
.DS_Store
maxiLib/maxiLib.js
View file @
efdb9bd5
...
...
@@ -8493,8 +8493,8 @@ Module.asmLibraryArg = {
var asm = (function(global,env,buffer) {
'use asm';
var Int8View = global.Int8Array;
var Int16View = global.Int16Array;
var Int32View = global.Int32Array;
...
...
@@ -33344,6 +33344,133 @@ maximJs.maxiTools.getArrayAsVectorDbl = (function(arrayIn) {
}
return vecOut;
});
maximJs.maxiTools.getBase64 = function(str) {
if (str.indexOf(';base64,') != -1 ) {
var dataStart = str.indexOf(';base64,') + 8;
return str.slice(dataStart).match(/^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$/) ? str.slice(dataStart) : false;
}
else return false;
}
maximJs.maxiTools._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
maximJs.maxiTools.removePaddingFromBase64 = function(input) {
var lkey = maximJs.maxiTools._keyStr.indexOf(input.charAt(input.length - 1));
if(lkey == 64){
return input.substring(0,input.length - 1);
}
return input;
}
maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
var data = [];
var context;
if(!contextIn){
context = new (window.AudioContext || window.webkitAudioContext)();
} else {
context = contextIn;
}
samplePlayer.clear();
var b64 = maximJs.maxiTools.getBase64(url);
if (b64) {
var ab_bytes = (b64.length/4) * 3;
var arrayBuffer = new ArrayBuffer(ab_bytes);
var bytes = parseInt((b64.length / 4) * 3, 10);
var uarray;
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var j = 0;
uarray = new Uint8Array(arrayBuffer);
b64 = b64.replace(/[^A-Za-z0-9\+\/\=]/g, "");
for (i=0; i<bytes; i+=3) {
enc1 = this._keyStr.indexOf(b64.charAt(j++));
enc2 = this._keyStr.indexOf(b64.charAt(j++));
enc3 = this._keyStr.indexOf(b64.charAt(j++));
enc4 = this._keyStr.indexOf(b64.charAt(j++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
uarray[i] = chr1;
if (enc3 != 64) uarray[i+1] = chr2;
if (enc4 != 64) uarray[i+2] = chr3;
}
context.decodeAudioData(
arrayBuffer,
function(buffer) {
data = buffer.getChannelData(0);
if(data){
var myBufferData = new Module.VectorDouble();
for(var n = 0; n < data.length; n++){
myBufferData.push_back(data[n]);
}
samplePlayer.setSample(myBufferData);
}
},
function(buffer) {
console.log("Error decoding source!");
}
);
}
else {
var request = new XMLHttpRequest();
request.addEventListener("load",
function (evt) {
console.log("The transfer is complete.");
});
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function() {
context.decodeAudioData(
request.response,
function(buffer) {
data = buffer.getChannelData(0);
if(data){
var myBufferData = new Module.VectorDouble();
for(var n = 0; n < data.length; n++){
myBufferData.push_back(data[n]);
}
samplePlayer.setSample(myBufferData);
}
},
function(buffer) {
console.log("Error decoding source!");
}
);
};
request.send();
}
};
maximJs.maxiTools.loadSample = (function(url, samplePlayer, contextIn) {
var data = [];
var context;
maxiLib/maximilian_examples_web/12_SamplePlayer.html
View file @
efdb9bd5
<!--
Copyright 2010, Google Inc.
All rights reserved.
...
...
@@ -54,7 +53,7 @@ maximJs.maxiTools.loadSample("audio/beat2.wav", samplePlayer);
maximJs
.
maxiAudio
.
play
=
function
(){
// this is necessary as file loading may not complete in setup
if
(
samplePlayer
.
isReady
()){
if
(
samplePlayer
.
isReady
()){
output
=
samplePlayer
.
play
();
//just play the file. Looping is default for all play functions.
// output=samplePlayer.play(0.69) ;//play the file with a speed setting. 1. is normal speed.
// output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
...
...
@@ -72,7 +71,7 @@ maximJs.maxiAudio.play = function(){
<h1>
Sample Example
</h1>
<p>
</p>
<pre
class=
"prettyprint lang-js linenums:true"
id=
"quine"
style=
"border:4px solid #88c"
>
...
...
@@ -85,7 +84,7 @@ loadSample("./beat2.wav", samplePlayer);
maximJs.maxiAudio.play = function(){
// this is necessary as file loading may not complete in setup
if(samplePlayer.isReady()){
if(samplePlayer.isReady()){
output = samplePlayer.play();//just play the file. Looping is default for all play functions.
// output=samplePlayer.play(0.69) ;//play the file with a speed setting. 1. is normal speed.
// output=samplePlayer.play(0.5,0,44100);//linear interpolationplay with a frequency input, start point and end point. Useful for syncing.
...
...
@@ -93,4 +92,4 @@ maximJs.maxiAudio.play = function(){
}
}
</pre>
</body></html>
</body></html>
\ No newline at end of file
maxiLib/maximilian_examples_web/23_SamplePlayer_base64.html
0 → 100644
View file @
efdb9bd5
This source diff could not be displayed because it is too large. You can
view the blob
instead.
src/js/maxi_webAudio.js
View file @
efdb9bd5
/*
Copyright notice for the base64 to arraybuffer conversion algorithm.
Copyright (c) 2011, Daniel Guerrero
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// might adopt some strange functions from Module?
// var maximJs = Module;
...
...
@@ -71,7 +95,7 @@ maximJs.maxiArray = function maxiArray(){
// // reset length var
// this.length = this.vec.size();
// }
// }
// };
};
...
...
@@ -145,6 +169,29 @@ maximJs.maxiTools.getArrayAsVectorDbl = function(arrayIn){
return
vecOut
;
};
maximJs
.
maxiTools
.
getBase64
=
function
(
str
)
{
//check if the string is a data URI
if
(
str
.
indexOf
(
'
;base64,
'
)
!=
-
1
)
{
//see where the actual data begins
var
dataStart
=
str
.
indexOf
(
'
;base64,
'
)
+
8
;
//check if the data is base64-encoded, if yes, return it
// taken from
// http://stackoverflow.com/a/8571649
return
str
.
slice
(
dataStart
).
match
(
/^
([
A-Za-z0-9+
\/]{4})
*
([
A-Za-z0-9+
\/]{4}
|
[
A-Za-z0-9+
\/]{3}
=|
[
A-Za-z0-9+
\/]{2}
==
)
$/
)
?
str
.
slice
(
dataStart
)
:
false
;
}
else
return
false
;
}
maximJs
.
maxiTools
.
_keyStr
=
"
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
"
;
maximJs
.
maxiTools
.
removePaddingFromBase64
=
function
(
input
)
{
var
lkey
=
maximJs
.
maxiTools
.
_keyStr
.
indexOf
(
input
.
charAt
(
input
.
length
-
1
));
if
(
lkey
==
64
){
return
input
.
substring
(
0
,
input
.
length
-
1
);
}
return
input
;
}
maximJs
.
maxiTools
.
loadSample
=
function
(
url
,
samplePlayer
,
contextIn
)
{
var
data
=
[];
var
context
;
...
...
@@ -158,19 +205,45 @@ maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
samplePlayer
.
clear
();
// Load asynchronously
var
request
=
new
XMLHttpRequest
();
request
.
addEventListener
(
"
load
"
,
function
(
evt
)
{
console
.
log
(
"
The transfer is complete.
"
);
});
request
.
open
(
"
GET
"
,
url
,
true
);
request
.
responseType
=
"
arraybuffer
"
;
//check if url is actually a base64-encoded string
var
b64
=
maximJs
.
maxiTools
.
getBase64
(
url
);
if
(
b64
)
{
//convert to arraybuffer
//modified version of this:
// https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js
var
ab_bytes
=
(
b64
.
length
/
4
)
*
3
;
var
arrayBuffer
=
new
ArrayBuffer
(
ab_bytes
);
var
bytes
=
parseInt
((
b64
.
length
/
4
)
*
3
,
10
);
var
uarray
;
var
chr1
,
chr2
,
chr3
;
var
enc1
,
enc2
,
enc3
,
enc4
;
var
i
=
0
;
var
j
=
0
;
uarray
=
new
Uint8Array
(
arrayBuffer
);
b64
=
b64
.
replace
(
/
[^
A-Za-z0-9
\+\/\=]
/g
,
""
);
for
(
i
=
0
;
i
<
bytes
;
i
+=
3
)
{
//get the 3 octects in 4 ascii chars
enc1
=
this
.
_keyStr
.
indexOf
(
b64
.
charAt
(
j
++
));
enc2
=
this
.
_keyStr
.
indexOf
(
b64
.
charAt
(
j
++
));
enc3
=
this
.
_keyStr
.
indexOf
(
b64
.
charAt
(
j
++
));
enc4
=
this
.
_keyStr
.
indexOf
(
b64
.
charAt
(
j
++
));
chr1
=
(
enc1
<<
2
)
|
(
enc2
>>
4
);
chr2
=
((
enc2
&
15
)
<<
4
)
|
(
enc3
>>
2
);
chr3
=
((
enc3
&
3
)
<<
6
)
|
enc4
;
uarray
[
i
]
=
chr1
;
if
(
enc3
!=
64
)
uarray
[
i
+
1
]
=
chr2
;
if
(
enc4
!=
64
)
uarray
[
i
+
2
]
=
chr3
;
}
request
.
onload
=
function
()
{
context
.
decodeAudioData
(
request
.
response
,
arrayBuffer
,
function
(
buffer
)
{
// source.buffer = buffer;
// source.loop = true;
...
...
@@ -193,10 +266,52 @@ maximJs.maxiTools.loadSample = function(url, samplePlayer, contextIn) {
function
(
buffer
)
{
console
.
log
(
"
Error decoding source!
"
);
}
);
}
else
{
// Load asynchronously
var
request
=
new
XMLHttpRequest
();
request
.
addEventListener
(
"
load
"
,
function
(
evt
)
{
console
.
log
(
"
The transfer is complete.
"
);
});
request
.
open
(
"
GET
"
,
url
,
true
);
request
.
responseType
=
"
arraybuffer
"
;
request
.
onload
=
function
()
{
context
.
decodeAudioData
(
request
.
response
,
function
(
buffer
)
{
// source.buffer = buffer;
// source.loop = true;
// source.start(0);
data
=
buffer
.
getChannelData
(
0
);
if
(
data
){
var
myBufferData
=
new
Module
.
VectorDouble
();
// Module.vectorTools.clearVectorDbl(myBufferData);
for
(
var
n
=
0
;
n
<
data
.
length
;
n
++
){
myBufferData
.
push_back
(
data
[
n
]);
}
samplePlayer
.
setSample
(
myBufferData
/*, context.sampleRate*/
);
}
},
function
(
buffer
)
{
console
.
log
(
"
Error decoding source!
"
);
}
);
};
};
request
.
send
();
}
request
.
send
();
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment