comparison MelodyResults.java @ 0:4031cbb02f08

Initial import. Ignore-this: 87317e384f22bde48db996355191fa5f
author Marcus Pearce <m.pearce@gold.ac.uk>
date Tue, 18 May 2010 11:37:10 +0100
parents
children 1fe7ac28a3ca
comparison
equal deleted inserted replaced
-1:000000000000 0:4031cbb02f08
1 /*=============================================================================
2 * File: MelodyResults.java
3 * Author: Marcus Pearce <m.pearce@gold.ac.uk>
4 * Created: <2007-02-14 11:28:27 marcusp>
5 * Time-stamp: <2010-05-10 11:38:52 marcusp>
6 *=============================================================================
7 */
8
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.io.*;
12
13 public class MelodyResults {
14
15 /* Probe positions, subject responses and times, note onsets and IOIs */
16 private ArrayList probes, responses, responseTimes, questions, answers,
17 onsets, iois, durations, pitches;
18 private String composition;
19 private int subjectID;
20
21 /* accessors */
22 public int getSubjectID() { return subjectID; }
23 public void setSubjectID(int id) { subjectID = id; }
24
25 /* constructor */
26 public MelodyResults (String midifile, ArrayList pr, ArrayList o,
27 ArrayList i, ArrayList d, ArrayList pi) {
28 questions = new ArrayList();
29 answers = new ArrayList();
30 responses = new ArrayList();
31 responseTimes = new ArrayList();
32 composition = compositionName(midifile);
33 probes = pr;
34 onsets = o;
35 iois = i;
36 durations = d;
37 pitches = pi;
38
39 // Iterator oi = onsets.iterator();
40 // Iterator di = durations.iterator();
41 // Iterator ioii = iois.iterator();
42 // Iterator piti = pitches.iterator();
43 // while(oi.hasNext()) {
44 // long onset = ((Long)oi.next()).longValue();
45 // long duration = ((Long)di.next()).longValue();
46 // long ioi = ((Long)ioii.next()).longValue();
47 // int pitch = ((Integer)piti.next()).intValue();
48 // System.out.println(onset + " " + duration + " " + ioi + " " +
49 // pitch + " ");
50 // }
51 }
52
53 private String compositionName(String midifile) {
54 // assuming extension is '.mid'
55 return midifile.substring(0, midifile.length() - 4);
56 }
57
58 /* add a reponse i and time t (nanoseconds) */
59 public void addResponse(int i, long t) {
60 //System.out.println("addResponse(" + i + ", " + t + ")");
61 responses.add(i);
62 responseTimes.add(t);
63 }
64
65 /* add question */
66 public void addQuestion(String question) {
67 questions.add(question);
68 }
69
70 /* add response to question */
71 public void addAnswer(String answer) {
72 answers.add(answer);
73 }
74
75 public void writeResults(File outputFile, boolean header) {
76 Writer writer = null;
77 try {
78 writer = new FileWriter (outputFile, true);
79 } catch (IOException e) {
80 System.out.println("Could not write file: " + outputFile.getPath());
81 return;
82 }
83
84 try {
85 writeResults(writer, header);
86 writer.close();
87 } catch (IOException e) {
88 System.out.println (e.getMessage());
89 return;
90 }
91 }
92
93 private void writeResults(Writer w, boolean header) throws IOException {
94 Iterator oi = onsets.iterator();
95 Iterator di = durations.iterator();
96 Iterator ioii = iois.iterator();
97 Iterator piti = pitches.iterator();
98
99 Iterator pi = probes.iterator();
100 Iterator ri = responses.iterator();
101 Iterator rti = responseTimes.iterator();
102 int eventIndex = 1;
103
104 if (header)
105 writeHeader(w);
106
107 String answerString = new String("");
108 Iterator ai = answers.iterator();
109 while(ai.hasNext())
110 answerString = answerString + (String)ai.next() + " ";
111
112 while(oi.hasNext()) {
113 long onset = ((Long)oi.next()).longValue();
114 long duration = ((Long)di.next()).longValue();
115 long ioi = ((Long)ioii.next()).longValue();
116 int pitch = ((Integer)piti.next()).intValue();
117
118 ProbeID p = (ProbeID)pi.next();
119
120 int probe = 0;
121 int response = 0;
122 long responseTime = 0;
123
124 switch(p) {
125 case NOT_PROBE:
126 case START_CLOCK:
127 case BEFORE_PROBE:
128 case AFTER_PROBE:
129 break;
130 case PROBE:
131 case PROBE_EX:
132 case PROBE_UNEX:
133 if (p == ProbeID.PROBE_UNEX)
134 probe = 1;
135 else if (p == ProbeID.PROBE_EX)
136 probe = 2;
137 else if (p == ProbeID.PROBE)
138 probe = 1;
139
140 if (ri.hasNext())
141 response = ((Integer)ri.next()).intValue();
142 if (rti.hasNext()) {
143 responseTime = ((Long)rti.next()).longValue();
144 }
145 break;
146 default:
147 System.out.println("Unexpected probe id: " + p);
148 break;
149 }
150 w.write(subjectID + " " + composition + " " +
151 eventIndex + " " +
152 onset + " " + duration + " " + ioi + " " +
153 pitch + " " +
154 probe + " " + response + " " + responseTime + " " +
155 answerString + "\n");
156 eventIndex++;
157 }
158 }
159
160 private void writeHeader(Writer w) throws IOException {
161 w.write("subject melody note " +
162 "onset duration ioi pitch " +
163 "probe response time");
164 Iterator qi = questions.iterator();
165 while(qi.hasNext())
166 w.write(" " + (String)qi.next());
167 w.write("\n");
168 }
169 }