Mercurial > hg > human-echolocation-java-webapp
diff 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 |
line wrap: on
line diff
--- a/src/HumanEchoServlet.java Mon Mar 03 13:17:16 2014 +0000 +++ b/src/HumanEchoServlet.java Mon Mar 03 15:31:14 2014 +0000 @@ -29,10 +29,12 @@ import uk.ac.soton.isvr.*; public class HumanEchoServlet extends HttpServlet { + private HumanEcho echo; private static Logger logger = Logger.getLogger(HumanEchoServlet.class); - private String wavdir = ""; + private String outdir = ""; + private String indir = ""; public void init(ServletConfig config) throws ServletException { super.init(config); @@ -43,15 +45,15 @@ System.getProperty("java.library.path")); try { - // Read directory properties Properties properties = new Properties(); properties.load (Thread.currentThread().getContextClassLoader(). getResourceAsStream("HumanEcho.properties")); - wavdir = properties.getProperty("outdir"); + outdir = properties.getProperty("outdir"); + indir = properties.getProperty("indir"); - logger.info("WAV file directory is " + wavdir); + logger.info("WAV file directory is " + outdir); } catch (Exception e) { logger.error("Failed to load app properties: " + e.getMessage()); @@ -85,36 +87,41 @@ logger.info("In doGet"); + logger.info("Distance (as string) is: " + request.getParameter("dist")); + // todo: validate/normalise distance - int dist = Integer.parseInt(request.getParameter("dist")); + Double dist = Double.parseDouble(request.getParameter("dist")); - logger.info("String azim is: " + request.getParameter("azim")); - Float azim = Float.parseFloat(request.getParameter("azim")); + Double azim = Double.parseDouble(request.getParameter("azim")); + + String orientation = request.getParameter("orient"); + + Double dirweight = Double.parseDouble(request.getParameter("dirweight")); MWStructArray Input = null; - HumanEcho echo; String outputfname = new String(); - logger.info("Got dist and azim"); + logger.info("Got parameters"); try { - //!!! erch, shouldn't be creating a new one on each request as well as a global one - echo = new HumanEcho(); - logger.info("Property catalina.base is " + System.getProperty("catalina.base")); // Matlab structure: - // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav') + // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav', 'indir', '/path/to/hrir') - String[] InputStructFields = {"dist", "azim", "orient", "dirweight", "outdir", "outname"}; + String[] InputStructFields = { + "dist", "azim", "orient", "dirweight", + "outdir", "outname", "indir" + }; Input = new MWStructArray(1, 1, InputStructFields); Input.set("dist", 1, Double.valueOf(dist)); Input.set("azim", 1, Double.valueOf(azim)); - Input.set("orient", 1, "horz"); - Input.set("dirweight", 1, Double.valueOf(0.2)); - Input.set("outdir", 1, wavdir); + Input.set("orient", 1, orientation); + Input.set("dirweight", 1, Double.valueOf(dirweight)); + Input.set("outdir", 1, outdir); + Input.set("indir", 1, indir); // the ofname should depend on the parameters StringBuilder sb = new StringBuilder(); @@ -140,7 +147,11 @@ try{ stream = response.getOutputStream(); - File wavfile = new File(outputfname); + + String filename = outdir + "/" + outputfname + ".wav"; + logger.info("About to read WAV data from \"" + filename + "\""); + + File wavfile = new File(filename); //set response headers response.setContentType("audio/x-wav"); @@ -149,24 +160,24 @@ FileInputStream input = new FileInputStream(wavfile); buf = new BufferedInputStream(input); - int readBytes = 0; - //read from the file; write to the ServletOutputStream - while((readBytes = buf.read( )) != -1) - stream.write(readBytes); + int b = 0; + int total = 0; + + // read from the file; write to the ServletOutputStream + while ((b = buf.read()) != -1) { + stream.write(b); + total += 1; + } + + logger.info("Successfully wrote " + total + " byte(s) to client"); } catch (IOException ioe){ logger.error("Failed to read wav data: " + ioe.getMessage()); throw new ServletException(ioe.getMessage( )); } finally { - //close the input/output streams - if(stream != null) - stream.close( ); - - if(buf != null) - buf.close( ); - } - - logger.info("Wrote file: " + outputfname); + if (stream != null) stream.close(); + if (buf != null) buf.close(); + } } }