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 {
|
luis@0
|
32 private HumanEcho echo;
|
luis@28
|
33 private static Logger logger = Logger.getLogger(HumanEchoServlet.class);
|
luis@0
|
34
|
luis@28
|
35 private String wavdir = "";
|
luis@16
|
36
|
luis@0
|
37 public void init(ServletConfig config) throws ServletException {
|
luis@0
|
38 super.init(config);
|
luis@0
|
39
|
Chris@51
|
40 logger.info("In init");
|
Chris@42
|
41
|
Chris@51
|
42 logger.info("java.library.path is " +
|
Chris@42
|
43 System.getProperty("java.library.path"));
|
Chris@42
|
44
|
luis@15
|
45 try {
|
Chris@42
|
46 // Read directory properties
|
luis@15
|
47 Properties properties = new Properties();
|
Chris@42
|
48 properties.load
|
Chris@42
|
49 (Thread.currentThread().getContextClassLoader().
|
Chris@51
|
50 getResourceAsStream("HumanEcho.properties"));
|
luis@15
|
51
|
luis@28
|
52 wavdir = properties.getProperty("outdir");
|
luis@15
|
53
|
Chris@51
|
54 logger.info("WAV file directory is " + wavdir);
|
Chris@42
|
55
|
Chris@51
|
56 } catch (Exception e) {
|
Chris@51
|
57 logger.error("Failed to load app properties: " + e.getMessage());
|
luis@15
|
58 e.printStackTrace();
|
luis@15
|
59 }
|
luis@15
|
60
|
luis@0
|
61 try {
|
Chris@42
|
62 // Test that we can construct one of the MATLAB objects
|
Chris@42
|
63
|
Chris@42
|
64 long before = System.currentTimeMillis();
|
luis@0
|
65 echo = new HumanEcho();
|
Chris@42
|
66 long after = System.currentTimeMillis();
|
Chris@51
|
67 logger.info("Created a HumanEcho object: it took " + (after - before) + " ms");
|
Chris@42
|
68 } catch (MWException e) {
|
Chris@51
|
69 logger.error("Failed to construct HumanEcho object: " + e.getMessage());
|
luis@0
|
70 e.printStackTrace();
|
luis@0
|
71 }
|
Chris@51
|
72
|
Chris@51
|
73 logger.info("Init completed");
|
luis@0
|
74 }
|
luis@0
|
75
|
luis@0
|
76 public void destroy() {
|
luis@0
|
77 super.destroy();
|
luis@0
|
78
|
Chris@42
|
79 if (echo != null) {
|
luis@0
|
80 echo.dispose();
|
luis@0
|
81 }
|
luis@0
|
82 }
|
luis@0
|
83
|
luis@11
|
84 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
luis@0
|
85
|
Chris@51
|
86 logger.info("In doGet");
|
Chris@47
|
87
|
luis@11
|
88 // todo: validate/normalise distance
|
luis@11
|
89 int dist = Integer.parseInt(request.getParameter("dist"));
|
luis@38
|
90
|
luis@38
|
91 logger.info("String azim is: " + request.getParameter("azim"));
|
luis@38
|
92 Float azim = Float.parseFloat(request.getParameter("azim"));
|
luis@38
|
93
|
luis@11
|
94 MWStructArray Input = null;
|
luis@11
|
95 HumanEcho echo;
|
luis@0
|
96
|
luis@28
|
97 String outputfname = new String();
|
luis@28
|
98
|
Chris@51
|
99 logger.info("Got dist and azim");
|
Chris@42
|
100
|
luis@11
|
101 try {
|
Chris@42
|
102 //!!! erch, shouldn't be creating a new one on each request as well as a global one
|
luis@11
|
103 echo = new HumanEcho();
|
luis@0
|
104
|
Chris@51
|
105 logger.info("Property catalina.base is " + System.getProperty("catalina.base"));
|
luis@16
|
106
|
luis@11
|
107 // Matlab structure:
|
Chris@47
|
108 // Input = struct('dist', 0.9, 'azim', 0, 'orient', 'horz', 'dirweight', 0.2, 'outdir', '/tmp/wav', 'outname', 'foo.wav')
|
luis@0
|
109
|
Chris@47
|
110 String[] InputStructFields = {"dist", "azim", "orient", "dirweight", "outdir", "outname"};
|
Chris@47
|
111
|
luis@11
|
112 Input = new MWStructArray(1, 1, InputStructFields);
|
luis@11
|
113 Input.set("dist", 1, Double.valueOf(dist));
|
luis@38
|
114 Input.set("azim", 1, Double.valueOf(azim));
|
luis@11
|
115 Input.set("orient", 1, "horz");
|
luis@11
|
116 Input.set("dirweight", 1, Double.valueOf(0.2));
|
Chris@46
|
117 Input.set("outdir", 1, wavdir);
|
Chris@46
|
118
|
luis@11
|
119 // the ofname should depend on the parameters
|
luis@11
|
120 StringBuilder sb = new StringBuilder();
|
luis@17
|
121 sb.append("e_d");
|
luis@11
|
122 sb.append(dist);
|
luis@38
|
123 sb.append("_a");
|
luis@38
|
124 sb.append(azim);
|
luis@28
|
125 outputfname = sb.toString();
|
luis@11
|
126
|
Chris@46
|
127 Input.set("outname", 1, outputfname);
|
luis@11
|
128
|
luis@11
|
129 // todo: before calling should test if wav already exists..
|
luis@11
|
130 Object[] result = echo.simulateBinauralSignals(Input);
|
luis@0
|
131 }
|
luis@0
|
132 catch(MWException e) {
|
Chris@51
|
133 logger.error("Failed to calculate simulateBinauralSignals: " + e.getMessage());
|
luis@0
|
134 e.printStackTrace();
|
luis@0
|
135 }
|
luis@0
|
136
|
luis@28
|
137 // Creating the stream
|
luis@28
|
138 ServletOutputStream stream = null;
|
luis@28
|
139 BufferedInputStream buf = null;
|
luis@28
|
140
|
luis@28
|
141 try{
|
luis@28
|
142 stream = response.getOutputStream();
|
luis@28
|
143 File wavfile = new File(outputfname);
|
luis@28
|
144
|
luis@28
|
145 //set response headers
|
luis@32
|
146 response.setContentType("audio/x-wav");
|
luis@28
|
147 response.addHeader("Content-Disposition","attachment; filename=" + outputfname );
|
luis@28
|
148 response.setContentLength( (int) wavfile.length( ) );
|
luis@28
|
149
|
luis@28
|
150 FileInputStream input = new FileInputStream(wavfile);
|
luis@28
|
151 buf = new BufferedInputStream(input);
|
luis@28
|
152 int readBytes = 0;
|
luis@28
|
153
|
luis@28
|
154 //read from the file; write to the ServletOutputStream
|
luis@28
|
155 while((readBytes = buf.read( )) != -1)
|
luis@28
|
156 stream.write(readBytes);
|
luis@28
|
157
|
luis@28
|
158 } catch (IOException ioe){
|
Chris@51
|
159 logger.error("Failed to read wav data: " + ioe.getMessage());
|
luis@28
|
160 throw new ServletException(ioe.getMessage( ));
|
luis@28
|
161 } finally {
|
luis@28
|
162 //close the input/output streams
|
luis@28
|
163 if(stream != null)
|
luis@28
|
164 stream.close( );
|
luis@28
|
165
|
luis@28
|
166 if(buf != null)
|
luis@28
|
167 buf.close( );
|
luis@28
|
168 }
|
luis@28
|
169
|
Chris@51
|
170 logger.info("Wrote file: " + outputfname);
|
luis@0
|
171 }
|
luis@0
|
172 }
|