1 |
|
---|
2 |
|
---|
3 | var ChromagramMeandreWorkflow = function() {
|
---|
4 |
|
---|
5 | var domain_prefix = window.location.protocol + "//" + window.location.hostname;
|
---|
6 |
|
---|
7 | this.meandreController = new MeandreController(domain_prefix,"1714","1712");
|
---|
8 |
|
---|
9 | this.baseURI = "http://greenstone.org/meandre/flows/chromagram-v1/";
|
---|
10 | this.serviceHead = "http://greenstone.org/meandre/flows/chromagram-v1/instance/greenstone-audio-web-service-head/0";
|
---|
11 | this.serviceTail = "http://greenstone.org/meandre/flows/chromagram-v1/instance/float32-frame-web-service-buffer/5";
|
---|
12 |
|
---|
13 | this.flowPort = null;
|
---|
14 |
|
---|
15 | this.meandreFrameSize = null;
|
---|
16 | this.meandreDataOutput = new Array();
|
---|
17 |
|
---|
18 | this.meandreCount = 0;
|
---|
19 | }
|
---|
20 |
|
---|
21 |
|
---|
22 | ChromagramMeandreWorkflow.prototype.thisMeandreFlowIsRunning = function() {
|
---|
23 | return this.flowPort != null;
|
---|
24 | }
|
---|
25 |
|
---|
26 | ChromagramMeandreWorkflow.prototype.processThisMeandreFlowOutputDataCallback = function(oEvent,oReq) {
|
---|
27 |
|
---|
28 |
|
---|
29 | var arrayBuffer = oReq.response; // Note: not oReq.responseText
|
---|
30 |
|
---|
31 | if (arrayBuffer.byteLength) {
|
---|
32 | console.log("*** get-data callback: Recieved binary data: raw byte array buffer len = " + arrayBuffer.byteLength);
|
---|
33 |
|
---|
34 | console.log("*** type of arrayBuffer: " + toType(arrayBuffer));
|
---|
35 |
|
---|
36 | var float32Array = new Float32Array(arrayBuffer);
|
---|
37 |
|
---|
38 | console.info("*** get-data callback: number of frames received = " + float32Array.length/this.meandreFrameSize);
|
---|
39 |
|
---|
40 |
|
---|
41 | // Break it up into frames
|
---|
42 |
|
---|
43 | //payload.data = float32Array;
|
---|
44 | this.meandreDataOutput.push(float32Array);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | ChromagramMeandreWorkflow.prototype.processThisMeandreFlowOutputData = function(flowPort) {
|
---|
51 |
|
---|
52 | // does flowPort need to be passed in as a parameter, now this is method not a function?
|
---|
53 |
|
---|
54 | var getinfo_options = {cmd: "get-info" };
|
---|
55 |
|
---|
56 | var info_props = this.meandreController.webComponentProperties(flowPort,this.serviceTail,getinfo_options)
|
---|
57 |
|
---|
58 | // values stored in 'info_props' are strings => need to make sure we treat get numeric fields such
|
---|
59 | // as 'numFrames' as an integer
|
---|
60 |
|
---|
61 | var numFramesAvailable = parseInt(info_props.numFrames);
|
---|
62 | if (isDefined(numFramesAvailable)) {
|
---|
63 | this.meandreFrameSize = parseInt(info_props.frameSize);
|
---|
64 | }
|
---|
65 | else {
|
---|
66 | numFramesAvailable = 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // console.log("*** Meandre count = " + meandreCount);
|
---|
70 |
|
---|
71 | if (numFramesAvailable>0) {
|
---|
72 |
|
---|
73 | var getNumFrames = Math.min(1000,numFramesAvailable);
|
---|
74 |
|
---|
75 | console.info("get-data request: Number of frames available = " + numFramesAvailable, ", number of frames requested = " + getNumFrames);
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | // console.log("***** doing a get-data");
|
---|
80 | var getdata_options = {cmd: "get-data", numFrames: getNumFrames };
|
---|
81 | var payload = this.meandreController.binaryWebComponent(flowPort,this.serviceTail,getdata_options,
|
---|
82 | this.processThisMeandreFlowOutputDataCallback,this);
|
---|
83 |
|
---|
84 | // delay the next call to 'get-info' to give the 'get-data' a chance to reach the server
|
---|
85 | var that = this;
|
---|
86 | setTimeout(function() {
|
---|
87 | that.processThisMeandreFlowOutputData(flowPort);
|
---|
88 | }, 400);
|
---|
89 |
|
---|
90 | }
|
---|
91 | else {
|
---|
92 | console.log("*** Sending 'done' command");
|
---|
93 | var done_options = {cmd: "done" };
|
---|
94 | this.meandreController.webComponent(flowPort,this.serviceHead,done_options);
|
---|
95 |
|
---|
96 | // disable 'stop' and make 'run' active again
|
---|
97 | $('#meandre-stop-button').attr("disabled", "disabled");
|
---|
98 | $('#meandre-run-button').removeAttr("disabled");
|
---|
99 | }
|
---|
100 |
|
---|
101 | this.meandreCount++;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | ChromagramMeandreWorkflow.prototype.runThisMeandreFlow = function(elem,playCallback) {
|
---|
106 |
|
---|
107 | if (this.thisMeandreFlowIsRunning()) {
|
---|
108 | console.error("runThisMeandreFlow(): The flow is already running on port " + this.flowPort);
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | var $progressElem = $('#processing');
|
---|
113 | var $progressTitleElem = $('#processingTitle');
|
---|
114 | var $progressTextElem = $('#processingText');
|
---|
115 |
|
---|
116 | var displayProgress = ($progressElem.length>0) ? true : false;
|
---|
117 | if (displayProgress) {
|
---|
118 | $progressTitleElem.html("Starting Meandre Workflow ...");
|
---|
119 | $progressTextElem.css('display','none');
|
---|
120 | $progressElem.css('display','block');
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | var flow_token = this.meandreController.runFlow(this.baseURI);
|
---|
125 | //console.log("**** got flow token : " + flow_token);
|
---|
126 |
|
---|
127 | // Give the flow time to start up ...
|
---|
128 | var that = this;
|
---|
129 |
|
---|
130 | setTimeout(function() {
|
---|
131 | // Now find out which port it is running on ...
|
---|
132 | var flow_props = that.meandreController.runningFlowProperties(flow_token);
|
---|
133 | var flow_port = flow_props.port;
|
---|
134 |
|
---|
135 | var attempts = 0;
|
---|
136 | while ((flow_port==null) || (flow_port <= 0)) {
|
---|
137 | console.info("port returned as " + flow_port + ", trying again");
|
---|
138 | flow_props = that.meandreController.runningFlowProperties(flow_token);
|
---|
139 | flow_port = flow_props.port;
|
---|
140 | attempts++;
|
---|
141 |
|
---|
142 | if (attempts==10) {
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | console.info("Running Meandre flow on port : " + flow_port);
|
---|
148 | that.flowPort = flow_port;
|
---|
149 |
|
---|
150 | $('#meandre-run-button').attr("disabled", "disabled");
|
---|
151 | $('#meandre-stop-button').removeAttr("disabled");
|
---|
152 |
|
---|
153 | //$('#meandre-run-button').button({ disabled: true });
|
---|
154 | //$('#meandre-stop-button').button({disabled: false});
|
---|
155 |
|
---|
156 | var start_options = {
|
---|
157 | cmd: "generate-audio-signal",
|
---|
158 | docOID: "wibble",
|
---|
159 | site: gs.xsltParams["site_name"],
|
---|
160 | collect: collect,
|
---|
161 | assocPath: gs.documentMetadata["assocfilepath"],
|
---|
162 | assocFile: "doc.wav"
|
---|
163 | };
|
---|
164 |
|
---|
165 | that.meandreController.webComponent(that.flowPort,that.serviceHead,start_options);
|
---|
166 |
|
---|
167 | setTimeout(function() {
|
---|
168 | that.processThisMeandreFlowOutputData(that.flowPort);
|
---|
169 |
|
---|
170 | if (displayProgress) {
|
---|
171 | $progressElem.css('display','none');
|
---|
172 | $progressTextElem.css('display','block');
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (elem!=null) {
|
---|
176 | // runFlow has been triggered by clicking on the 'run' button
|
---|
177 | togglePlay(null,document.getElementById('mainPlayButton'));
|
---|
178 | }
|
---|
179 | else if (playCallback!=null) {
|
---|
180 | playCallback();
|
---|
181 | }
|
---|
182 |
|
---|
183 | }, 700);
|
---|
184 |
|
---|
185 | }, 800);
|
---|
186 | }
|
---|
187 |
|
---|
188 | ChromagramMeandreWorkflow.prototype.stopThisMeandreFlow = function(elem) {
|
---|
189 |
|
---|
190 | this.meandreController.abortFlow(this.flowPort);
|
---|
191 |
|
---|
192 | $('#meandre-stop-button').attr("disabled", "disabled");
|
---|
193 | $('#meandre-run-button').removeAttr("disabled");
|
---|
194 |
|
---|
195 | //$('#meandre-run-button').button({ disabled: false });
|
---|
196 | //$('#meandre-stop-button').button({disabled: true});
|
---|
197 | }
|
---|
198 |
|
---|