annotate src/HumanEchoServlet.java @ 54:b27106b380d8

Populate and pass location of input dir; handle all parameters from page; use only one echo instance (??)
author Chris Cannam
date Mon, 03 Mar 2014 15:31:14 +0000
parents ebfb3b91fb06
children b3f3b42b9933
rev   line source
luis@16 1 /*
luis@28 2 * HumanEchoServlet
luis@16 3 *
luis@16 4 * Version information
luis@16 5 *
luis@16 6 * 3 December 2013
luis@16 7 *
luis@16 8 * Copyright notice
luis@16 9 */
luis@15 10
luis@15 11 import java.util.Properties;
luis@15 12 import java.io.IOException;
luis@15 13
luis@28 14 import java.io.FileInputStream;
luis@28 15 import java.io.BufferedInputStream;
luis@28 16 import java.io.File;
luis@28 17
luis@21 18 import org.apache.log4j.Logger;
luis@16 19
luis@28 20 import javax.servlet.*;
luis@28 21 import javax.servlet.http.*;
luis@0 22
luis@0 23 import com.mathworks.toolbox.javabuilder.MWJavaObjectRef;
luis@0 24 import com.mathworks.toolbox.javabuilder.MWNumericArray;
luis@11 25 import com.mathworks.toolbox.javabuilder.MWStructArray;
luis@0 26 import com.mathworks.toolbox.javabuilder.MWException;
luis@15 27 import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration;
luis@15 28
luis@15 29 import uk.ac.soton.isvr.*;
luis@0 30
luis@0 31 public class HumanEchoServlet extends HttpServlet {
Chris@54 32
luis@0 33 private HumanEcho echo;
luis@28 34 private static Logger logger = Logger.getLogger(HumanEchoServlet.class);
luis@0 35
Chris@54 36 private String outdir = "";
Chris@54 37 private String indir = "";
luis@16 38
luis@0 39 public void init(ServletConfig config) throws ServletException {
luis@0 40 super.init(config);
luis@0 41
Chris@51 42 logger.info("In init");
Chris@42 43
Chris@51 44 logger.info("java.library.path is " +
Chris@42 45 System.getProperty("java.library.path"));
Chris@42 46
luis@15 47 try {
luis@15 48 Properties properties = new Properties();
Chris@42 49 properties.load
Chris@42 50 (Thread.currentThread().getContextClassLoader().
Chris@51 51 getResourceAsStream("HumanEcho.properties"));
luis@15 52
Chris@54 53 outdir = properties.getProperty("outdir");
Chris@54 54 indir = properties.getProperty("indir");
luis@15 55
Chris@54 56 logger.info("WAV file directory is " + outdir);
Chris@42 57
Chris@51 58 } catch (Exception e) {
Chris@51 59 logger.error("Failed to load app properties: " + e.getMessage());
luis@15 60 e.printStackTrace();
luis@15 61 }
luis@15 62
luis@0 63 try {
Chris@42 64 // Test that we can construct one of the MATLAB objects
Chris@42 65
Chris@42 66 long before = System.currentTimeMillis();
luis@0 67 echo = new HumanEcho();
Chris@42 68 long after = System.currentTimeMillis();
Chris@51 69 logger.info("Created a HumanEcho object: it took " + (after - before) + " ms");
Chris@42 70 } catch (MWException e) {
Chris@51 71 logger.error("Failed to construct HumanEcho object: " + e.getMessage());
luis@0 72 e.printStackTrace();
luis@0 73 }
Chris@51 74
Chris@51 75 logger.info("Init completed");
luis@0 76 }
luis@0 77
luis@0 78 public void destroy() {
luis@0 79 super.destroy();
luis@0 80
Chris@42 81 if (echo != null) {
luis@0 82 echo.dispose();
luis@0 83 }
luis@0 84 }
luis@0 85
luis@11 86 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
luis@0 87
Chris@51 88 logger.info("In doGet");
Chris@47 89
Chris@54 90 logger.info("Distance (as string) is: " + request.getParameter("dist"));
Chris@54 91
luis@11 92 // todo: validate/normalise distance
Chris@54 93 Double dist = Double.parseDouble(request.getParameter("dist"));
luis@38 94
Chris@54 95 Double azim = Double.parseDouble(request.getParameter("azim"));
Chris@54 96
Chris@54 97 String orientation = request.getParameter("orient");
Chris@54 98
Chris@54 99 Double dirweight = Double.parseDouble(request.getParameter("dirweight"));
luis@38 100
luis@11 101 MWStructArray Input = null;
luis@0 102
luis@28 103 String outputfname = new String();
luis@28 104
Chris@54 105 logger.info("Got parameters");
Chris@42 106
luis@11 107 try {
Chris@51 108 logger.info("Property catalina.base is " + System.getProperty("catalina.base"));
luis@16 109
luis@11 110 // Matlab structure:
Chris@54 111 // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav', 'indir', '/path/to/hrir')
luis@0 112
Chris@54 113 String[] InputStructFields = {
Chris@54 114 "dist", "azim", "orient", "dirweight",
Chris@54 115 "outdir", "outname", "indir"
Chris@54 116 };
Chris@47 117
luis@11 118 Input = new MWStructArray(1, 1, InputStructFields);
luis@11 119 Input.set("dist", 1, Double.valueOf(dist));
luis@38 120 Input.set("azim", 1, Double.valueOf(azim));
Chris@54 121 Input.set("orient", 1, orientation);
Chris@54 122 Input.set("dirweight", 1, Double.valueOf(dirweight));
Chris@54 123 Input.set("outdir", 1, outdir);
Chris@54 124 Input.set("indir", 1, indir);
Chris@46 125
luis@11 126 // the ofname should depend on the parameters
luis@11 127 StringBuilder sb = new StringBuilder();
luis@17 128 sb.append("e_d");
luis@11 129 sb.append(dist);
luis@38 130 sb.append("_a");
luis@38 131 sb.append(azim);
luis@28 132 outputfname = sb.toString();
luis@11 133
Chris@46 134 Input.set("outname", 1, outputfname);
luis@11 135
luis@11 136 // todo: before calling should test if wav already exists..
luis@11 137 Object[] result = echo.simulateBinauralSignals(Input);
luis@0 138 }
luis@0 139 catch(MWException e) {
Chris@51 140 logger.error("Failed to calculate simulateBinauralSignals: " + e.getMessage());
luis@0 141 e.printStackTrace();
luis@0 142 }
luis@0 143
luis@28 144 // Creating the stream
luis@28 145 ServletOutputStream stream = null;
luis@28 146 BufferedInputStream buf = null;
luis@28 147
luis@28 148 try{
luis@28 149 stream = response.getOutputStream();
Chris@54 150
Chris@54 151 String filename = outdir + "/" + outputfname + ".wav";
Chris@54 152 logger.info("About to read WAV data from \"" + filename + "\"");
Chris@54 153
Chris@54 154 File wavfile = new File(filename);
luis@28 155
luis@28 156 //set response headers
luis@32 157 response.setContentType("audio/x-wav");
luis@28 158 response.addHeader("Content-Disposition","attachment; filename=" + outputfname );
luis@28 159 response.setContentLength( (int) wavfile.length( ) );
luis@28 160
luis@28 161 FileInputStream input = new FileInputStream(wavfile);
luis@28 162 buf = new BufferedInputStream(input);
luis@28 163
Chris@54 164 int b = 0;
Chris@54 165 int total = 0;
Chris@54 166
Chris@54 167 // read from the file; write to the ServletOutputStream
Chris@54 168 while ((b = buf.read()) != -1) {
Chris@54 169 stream.write(b);
Chris@54 170 total += 1;
Chris@54 171 }
Chris@54 172
Chris@54 173 logger.info("Successfully wrote " + total + " byte(s) to client");
luis@28 174
luis@28 175 } catch (IOException ioe){
Chris@51 176 logger.error("Failed to read wav data: " + ioe.getMessage());
luis@28 177 throw new ServletException(ioe.getMessage( ));
luis@28 178 } finally {
Chris@54 179 if (stream != null) stream.close();
Chris@54 180 if (buf != null) buf.close();
Chris@54 181 }
luis@0 182 }
luis@0 183 }