luis@16: /* luis@28: * HumanEchoServlet luis@16: * luis@16: * Version information luis@16: * luis@16: * 3 December 2013 luis@16: * luis@16: * Copyright notice luis@16: */ luis@15: luis@15: import java.util.Properties; luis@15: import java.io.IOException; luis@15: luis@28: import java.io.FileInputStream; luis@28: import java.io.BufferedInputStream; luis@28: import java.io.File; luis@28: luis@21: import org.apache.log4j.Logger; luis@16: luis@28: import javax.servlet.*; luis@28: import javax.servlet.http.*; luis@0: luis@0: import com.mathworks.toolbox.javabuilder.MWJavaObjectRef; luis@0: import com.mathworks.toolbox.javabuilder.MWNumericArray; luis@11: import com.mathworks.toolbox.javabuilder.MWStructArray; luis@0: import com.mathworks.toolbox.javabuilder.MWException; luis@15: import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration; luis@15: luis@15: import uk.ac.soton.isvr.*; luis@0: luis@0: public class HumanEchoServlet extends HttpServlet { Chris@54: luis@0: private HumanEcho echo; luis@28: private static Logger logger = Logger.getLogger(HumanEchoServlet.class); luis@0: Chris@54: private String outdir = ""; Chris@54: private String indir = ""; luis@16: luis@0: public void init(ServletConfig config) throws ServletException { luis@0: super.init(config); luis@0: Chris@51: logger.info("In init"); Chris@42: Chris@51: logger.info("java.library.path is " + Chris@42: System.getProperty("java.library.path")); Chris@42: luis@15: try { luis@15: Properties properties = new Properties(); Chris@42: properties.load Chris@42: (Thread.currentThread().getContextClassLoader(). Chris@51: getResourceAsStream("HumanEcho.properties")); luis@15: Chris@54: outdir = properties.getProperty("outdir"); Chris@54: indir = properties.getProperty("indir"); luis@15: Chris@54: logger.info("WAV file directory is " + outdir); Chris@42: Chris@51: } catch (Exception e) { Chris@51: logger.error("Failed to load app properties: " + e.getMessage()); luis@15: e.printStackTrace(); luis@15: } luis@15: luis@0: try { Chris@42: // Test that we can construct one of the MATLAB objects Chris@42: Chris@42: long before = System.currentTimeMillis(); luis@0: echo = new HumanEcho(); Chris@42: long after = System.currentTimeMillis(); Chris@51: logger.info("Created a HumanEcho object: it took " + (after - before) + " ms"); Chris@42: } catch (MWException e) { Chris@51: logger.error("Failed to construct HumanEcho object: " + e.getMessage()); luis@0: e.printStackTrace(); luis@0: } Chris@51: Chris@51: logger.info("Init completed"); luis@0: } luis@0: luis@0: public void destroy() { luis@0: super.destroy(); luis@0: Chris@42: if (echo != null) { luis@0: echo.dispose(); luis@0: } luis@0: } luis@0: luis@11: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { luis@0: Chris@51: logger.info("In doGet"); Chris@47: Chris@54: logger.info("Distance (as string) is: " + request.getParameter("dist")); Chris@54: luis@11: // todo: validate/normalise distance Chris@54: Double dist = Double.parseDouble(request.getParameter("dist")); luis@38: Chris@54: Double azim = Double.parseDouble(request.getParameter("azim")); Chris@54: Chris@54: String orientation = request.getParameter("orient"); Chris@54: Chris@54: Double dirweight = Double.parseDouble(request.getParameter("dirweight")); luis@38: luis@11: MWStructArray Input = null; luis@0: luis@28: String outputfname = new String(); luis@28: Chris@54: logger.info("Got parameters"); Chris@42: luis@11: try { Chris@51: logger.info("Property catalina.base is " + System.getProperty("catalina.base")); luis@16: luis@11: // Matlab structure: Chris@54: // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav', 'indir', '/path/to/hrir') luis@0: Chris@54: String[] InputStructFields = { Chris@54: "dist", "azim", "orient", "dirweight", Chris@54: "outdir", "outname", "indir" Chris@54: }; Chris@47: luis@11: Input = new MWStructArray(1, 1, InputStructFields); luis@11: Input.set("dist", 1, Double.valueOf(dist)); luis@38: Input.set("azim", 1, Double.valueOf(azim)); Chris@54: Input.set("orient", 1, orientation); Chris@54: Input.set("dirweight", 1, Double.valueOf(dirweight)); Chris@54: Input.set("outdir", 1, outdir); Chris@54: Input.set("indir", 1, indir); Chris@46: luis@11: // the ofname should depend on the parameters luis@11: StringBuilder sb = new StringBuilder(); luis@17: sb.append("e_d"); luis@11: sb.append(dist); luis@38: sb.append("_a"); luis@38: sb.append(azim); luis@28: outputfname = sb.toString(); luis@11: Chris@46: Input.set("outname", 1, outputfname); luis@11: luis@11: // todo: before calling should test if wav already exists.. luis@11: Object[] result = echo.simulateBinauralSignals(Input); luis@0: } luis@0: catch(MWException e) { Chris@51: logger.error("Failed to calculate simulateBinauralSignals: " + e.getMessage()); luis@0: e.printStackTrace(); luis@0: } luis@0: luis@28: // Creating the stream luis@28: ServletOutputStream stream = null; luis@28: BufferedInputStream buf = null; luis@28: luis@28: try{ luis@28: stream = response.getOutputStream(); Chris@54: Chris@54: String filename = outdir + "/" + outputfname + ".wav"; Chris@54: logger.info("About to read WAV data from \"" + filename + "\""); Chris@54: Chris@54: File wavfile = new File(filename); luis@28: luis@28: //set response headers luis@32: response.setContentType("audio/x-wav"); luis@28: response.addHeader("Content-Disposition","attachment; filename=" + outputfname ); luis@28: response.setContentLength( (int) wavfile.length( ) ); luis@28: luis@28: FileInputStream input = new FileInputStream(wavfile); luis@28: buf = new BufferedInputStream(input); luis@28: Chris@54: int b = 0; Chris@54: int total = 0; Chris@54: Chris@54: // read from the file; write to the ServletOutputStream Chris@54: while ((b = buf.read()) != -1) { Chris@54: stream.write(b); Chris@54: total += 1; Chris@54: } Chris@54: Chris@54: logger.info("Successfully wrote " + total + " byte(s) to client"); luis@28: luis@28: } catch (IOException ioe){ Chris@51: logger.error("Failed to read wav data: " + ioe.getMessage()); luis@28: throw new ServletException(ioe.getMessage( )); luis@28: } finally { Chris@54: if (stream != null) stream.close(); Chris@54: if (buf != null) buf.close(); Chris@54: } luis@0: } luis@0: }