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