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: Chris@60: import java.nio.file.Files; Chris@60: import java.nio.file.Path; Chris@60: import java.nio.file.StandardCopyOption; Chris@60: 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: Chris@60: private static Integer dirId = 0; Chris@60: 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: Chris@60: protected synchronized String getDirToken() { Chris@60: dirId = dirId + 1; Chris@60: return dirId.toString(); Chris@60: } Chris@60: 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")); Chris@60: Double azim = Double.parseDouble(request.getParameter("azim")); Chris@60: String orientation = request.getParameter("orient"); Chris@60: Double dirweight = Double.parseDouble(request.getParameter("dirweight")); luis@38: Chris@60: logger.info("Got parameters"); luis@38: luis@11: MWStructArray Input = null; luis@0: luis@28: String outputfname = new String(); luis@28: Chris@60: // this needs to be distinct for the parameters Chris@60: StringBuilder sb = new StringBuilder(); Chris@42: Chris@60: sb.append("e_d"); Chris@60: sb.append(dist); Chris@60: sb.append("_a"); Chris@60: sb.append(azim); luis@16: Chris@60: if (orientation.equals("horz")) { Chris@60: sb.append("_H"); Chris@60: } else if (orientation.equals("angled")) { Chris@60: sb.append("_A"); Chris@60: } luis@0: Chris@60: sb.append("_w"); Chris@60: sb.append(dirweight); Chris@47: Chris@60: outputfname = sb.toString(); Chris@46: Chris@62: String target = outdir + File.separator + outputfname + ".wav"; Chris@60: File targetFile = new File(target); Chris@60: targetFile.deleteOnExit(); luis@11: Chris@60: if (!targetFile.exists()) { luis@11: Chris@60: try { Chris@60: logger.info("Property catalina.base is " + System.getProperty("catalina.base")); Chris@60: Chris@62: String tempDir = outdir + File.separator + getDirToken(); Chris@60: File tempDirFilePath = new File(tempDir); Chris@60: tempDirFilePath.mkdirs(); Chris@60: Chris@60: // Matlab structure: Chris@60: // Chris@60: // Input = struct('dist', 0.9, 'azim', 0, 'orient', Chris@60: // 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', Chris@60: // 'outname', 'foo.wav', 'indir', '/path/to/hrir') Chris@60: Chris@60: String[] InputStructFields = { Chris@60: "dist", "azim", "orient", "dirweight", Chris@60: "outdir", "outname", "indir" Chris@60: }; Chris@60: Chris@60: Input = new MWStructArray(1, 1, InputStructFields); Chris@60: Input.set("dist", 1, Double.valueOf(dist)); Chris@60: Input.set("azim", 1, Double.valueOf(azim)); Chris@60: Input.set("orient", 1, orientation); Chris@60: Input.set("dirweight", 1, Double.valueOf(dirweight)); Chris@60: Input.set("outdir", 1, tempDir); Chris@60: Input.set("indir", 1, indir); Chris@60: Chris@60: Input.set("outname", 1, outputfname); Chris@60: Chris@60: Object[] result = echo.simulateBinauralSignals(Input); Chris@60: Chris@62: File createdFile = new File(tempDir + File.separator + outputfname + ".wav"); Chris@60: if (!createdFile.exists()) { Chris@60: String err = "The simulateBinauralSignals call failed to create expected output file \"" + createdFile + "\""; Chris@60: logger.error(err); Chris@60: throw new ServletException(err); Chris@60: } Chris@60: Chris@60: Files.move(createdFile.toPath(), Chris@60: targetFile.toPath(), Chris@60: StandardCopyOption.ATOMIC_MOVE); Chris@60: Chris@60: tempDirFilePath.delete(); Chris@60: Chris@60: } catch (MWException e) { Chris@60: logger.error("Failed to calculate simulateBinauralSignals: " + e.getMessage()); Chris@60: e.printStackTrace(); Chris@60: throw new ServletException(e.getMessage()); Chris@60: } Chris@60: } 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@60: logger.info("About to read WAV data from \"" + target + "\""); luis@28: luis@28: //set response headers luis@32: response.setContentType("audio/x-wav"); Chris@67: response.addHeader("Content-Disposition","attachment; filename=" + outputfname + ".wav"); Chris@60: response.setContentLength( (int) targetFile.length( ) ); luis@28: Chris@60: FileInputStream input = new FileInputStream(targetFile); 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: }