changeset 2217:cb27ece65a5c

Review for Issue #2. Should be a working implementation. See issue post for usage.
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 12 Apr 2016 11:48:40 +0100
parents 75ef314b5e64
children 30bffc455b4d
files core.js example_eval/radio_example.xml
diffstat 2 files changed, 105 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/core.js	Tue Apr 12 10:50:52 2016 +0100
+++ b/core.js	Tue Apr 12 11:48:40 2016 +0100
@@ -496,6 +496,47 @@
     return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
 }
 
+function randomiseOrder(input)
+{
+	// This takes an array of information and randomises the order
+	var N = input.length;
+	
+	var inputSequence = []; // For safety purposes: keep track of randomisation
+	for (var counter = 0; counter < N; ++counter) 
+		inputSequence.push(counter) // Fill array
+	var inputSequenceClone = inputSequence.slice(0);
+	
+	var holdArr = [];
+	var outputSequence = [];
+	for (var n=0; n<N; n++)
+	{
+		// First pick a random number
+		var r = Math.random();
+		// Multiply and floor by the number of elements left
+		r = Math.floor(r*input.length);
+		// Pick out that element and delete from the array
+		holdArr.push(input.splice(r,1)[0]);
+		// Do the same with sequence
+		outputSequence.push(inputSequence.splice(r,1)[0]);
+	}
+	console.log(inputSequenceClone.toString()); // print original array to console
+	console.log(outputSequence.toString()); 	// print randomised array to console
+	return holdArr;
+}
+
+function randomSubArray(array,num) {
+    if (num > array.length) {
+        num = array.length;
+    }
+    var ret = [];
+    while (num > 0) {
+        var index = Math.floor(Math.random() * array.length);
+        ret.push( array.splice(index,1)[0] );
+        num--;
+    }
+    return ret;
+}
+
 function interfacePopup() {
 	// Creates an object to manage the popup
 	this.popup = null;
@@ -556,12 +597,14 @@
 	};
 	
 	this.hidePopup = function(){
-		this.popup.style.zIndex = -1;
-		this.popup.style.visibility = 'hidden';
-		var blank = document.getElementsByClassName('testHalt')[0];
-		blank.style.zIndex = -2;
-		blank.style.visibility = 'hidden';
-		this.buttonPrevious.style.visibility = 'inherit';
+        if (this.popup) {
+            this.popup.style.zIndex = -1;
+            this.popup.style.visibility = 'hidden';
+            var blank = document.getElementsByClassName('testHalt')[0];
+            blank.style.zIndex = -2;
+            blank.style.visibility = 'hidden';
+            this.buttonPrevious.style.visibility = 'inherit';
+        }
 	};
 	
 	this.postNode = function() {
@@ -850,29 +893,68 @@
 	this.currentStatePosition = null;
     this.currentStore = null;
 	this.initialise = function(){
-		
+        
 		// Get the data from Specification
-		var pageHolder = [];
+		var pagePool = [];
+        var pageInclude = [];
 		for (var page of specification.pages)
 		{
-            var repeat = page.repeatCount;
-            while(repeat >= 0)
-            {
-                pageHolder.push(page);
-                repeat--;
+            if (page.alwaysInclude) {
+                pageInclude.push(page);
+            } else {
+                pagePool.push(page);
             }
 		}
+        
+        // Find how many are left to get
+        var numPages = specification.poolSize;
+        if (numPages > pagePool.length) {
+            console.log("WARNING - You have specified more pages in <setup poolSize> than you have created!!");
+            numPages = specification.pages.length;
+        }
+        if (specification.poolSize == 0) {
+            numPages = specification.pages.length;
+        }
+        numPages -= pageInclude.length;
+        
+        if (numPages > 0) {
+            // Go find the rest of the pages from the pool
+            var subarr = null;
+            if (specification.randomiseOrder) {
+                // Append a random sub-array
+                subarr = randomSubArray(pagePool,numPages);
+            } else {
+                // Append the matching number
+                subarr = pagePool.slice(0,numPages);
+            }
+            pageInclude = pageInclude.concat(subarr);
+        }
+        
+        // We now have our selected pages in pageInclude array
 		if (specification.randomiseOrder)
 		{
-			pageHolder = randomiseOrder(pageHolder);
+			pageInclude = randomiseOrder(pageInclude);
 		}
-		for (var i=0; i<pageHolder.length; i++)
+		for (var i=0; i<pageInclude.length; i++)
 		{
-			if (specification.poolSize <= i && specification.poolSize != 0) {break;}
-            pageHolder[i].presentedId = i;
-			this.stateMap.push(pageHolder[i]);
-            storage.createTestPageStore(pageHolder[i]);
-            audioEngineContext.loadPageData(pageHolder[i]);
+            pageInclude[i].presentedId = i;
+			this.stateMap.push(pageInclude[i]);
+            // For each selected page, we must get the sub pool
+            if (pageInclude[i].poolSize != 0 && pageInclude[i].poolSize != pageInclude[i].audioElements.length) {
+                var elemInclude = [];
+                var elemPool = [];
+                for (var elem of pageInclude[i].audioElements) {
+                    if (elem.include || elem.type != "normal") {
+                        elemInclude.push(elem);
+                    } else {
+                        elemPool.push(elem);
+                    }
+                }
+                var numElems = pageInclude[i].poolSize - elemInclude.length;
+                pageInclude[i].audioElements = elemInclude.concat(randomSubArray(elemPool,numElems));
+            }
+            storage.createTestPageStore(pageInclude[i]);
+            audioEngineContext.loadPageData(pageInclude[i]);
 		}
         
 		if (specification.preTest != null) {this.preTestSurvey = specification.preTest;}
@@ -1768,34 +1850,6 @@
 		return storeDOM;
 	};
 }
-
-function randomiseOrder(input)
-{
-	// This takes an array of information and randomises the order
-	var N = input.length;
-	
-	var inputSequence = []; // For safety purposes: keep track of randomisation
-	for (var counter = 0; counter < N; ++counter) 
-		inputSequence.push(counter) // Fill array
-	var inputSequenceClone = inputSequence.slice(0);
-	
-	var holdArr = [];
-	var outputSequence = [];
-	for (var n=0; n<N; n++)
-	{
-		// First pick a random number
-		var r = Math.random();
-		// Multiply and floor by the number of elements left
-		r = Math.floor(r*input.length);
-		// Pick out that element and delete from the array
-		holdArr.push(input.splice(r,1)[0]);
-		// Do the same with sequence
-		outputSequence.push(inputSequence.splice(r,1)[0]);
-	}
-	console.log(inputSequenceClone.toString()); // print original array to console
-	console.log(outputSequence.toString()); 	// print randomised array to console
-	return holdArr;
-}
 			
 function Interface(specificationObject) {
 	// This handles the bindings between the interface and the audioEngineContext;
--- a/example_eval/radio_example.xml	Tue Apr 12 10:50:52 2016 +0100
+++ b/example_eval/radio_example.xml	Tue Apr 12 11:48:40 2016 +0100
@@ -18,7 +18,7 @@
 			<interfaceoption type="show" name="page-count"/>
 		</interface>
 	</setup>
-	<page id='test-0' hostURL="example_eval/" randomiseOrder='true' repeatCount='4' loop='true' showElementComments='true' loudness="-23">
+	<page id='test-0' hostURL="example_eval/" randomiseOrder='true' repeatCount='4' loop='true' showElementComments='true' loudness="-23" poolSize="3">
 		<interface>
 			<scales>
 				<scalelabel position="0">(1) Very Annoying</scalelabel>
@@ -28,8 +28,9 @@
 				<scalelabel position="100">(5) Inaudible</scalelabel>
 			</scales>
 		</interface>
-		<audioelement url="0.wav" id="track-1"/>
+		<audioelement url="0.wav" id="track-1" alwaysInclude="true"/>
 		<audioelement url="1.wav" id="track-2"/>
+        <audioelement url="3.wav" id="track-4"/>
         <audioelement url="2.wav" id="track-3" type="outside-reference"/>
 	</page>
 </waet>