diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MelodyResults.java	Tue May 18 11:37:10 2010 +0100
@@ -0,0 +1,169 @@
+/*=============================================================================
+ * File:       MelodyResults.java
+ * Author:     Marcus Pearce <m.pearce@gold.ac.uk>
+ * Created:    <2007-02-14 11:28:27 marcusp>
+ * Time-stamp: <2010-05-10 11:38:52 marcusp>
+ *=============================================================================
+ */
+
+import java.util.ArrayList; 
+import java.util.Iterator;
+import java.io.*; 
+
+public class MelodyResults { 
+    
+    /* Probe positions, subject responses and times, note onsets and IOIs */ 
+    private ArrayList probes, responses, responseTimes, questions, answers, 
+        onsets, iois, durations, pitches; 
+    private String composition; 
+    private int subjectID; 
+
+    /* accessors */ 
+    public int getSubjectID() { return subjectID; }
+    public void setSubjectID(int id) { subjectID = id; } 
+
+    /* constructor */ 
+    public MelodyResults (String midifile, ArrayList pr, ArrayList o, 
+                          ArrayList i, ArrayList d, ArrayList pi) {
+        questions = new ArrayList(); 
+        answers = new ArrayList(); 
+        responses = new ArrayList(); 
+        responseTimes = new ArrayList();  
+        composition = compositionName(midifile);
+        probes = pr; 
+        onsets = o; 
+        iois = i; 
+        durations = d; 
+        pitches = pi; 
+
+//         Iterator oi = onsets.iterator(); 
+//         Iterator di = durations.iterator(); 
+//         Iterator ioii = iois.iterator(); 
+//         Iterator piti = pitches.iterator(); 
+//         while(oi.hasNext()) { 
+//             long onset = ((Long)oi.next()).longValue();
+//             long duration = ((Long)di.next()).longValue(); 
+//             long ioi = ((Long)ioii.next()).longValue();
+//             int pitch = ((Integer)piti.next()).intValue(); 
+//             System.out.println(onset + " " + duration + " " + ioi + " " + 
+//                                pitch + " "); 
+//        }
+    }
+    
+    private String compositionName(String midifile) { 
+        // assuming extension is '.mid' 
+        return midifile.substring(0, midifile.length() - 4); 
+    }
+
+    /* add a reponse i and time t (nanoseconds) */ 
+    public void addResponse(int i, long t) { 
+        //System.out.println("addResponse(" + i + ", " + t + ")"); 
+        responses.add(i); 
+        responseTimes.add(t);
+    }
+
+    /* add question */ 
+    public void addQuestion(String question) { 
+        questions.add(question); 
+    }
+
+    /* add response to question */ 
+    public void addAnswer(String answer) { 
+        answers.add(answer); 
+    }
+
+    public void writeResults(File outputFile, boolean header) { 
+	Writer writer = null;
+        try {
+            writer = new FileWriter (outputFile, true);
+        } catch (IOException e) {
+            System.out.println("Could not write file: " + outputFile.getPath());
+            return;
+        }
+        
+	try {
+            writeResults(writer, header); 
+            writer.close();
+        } catch (IOException e) {
+            System.out.println (e.getMessage());
+            return;
+        }
+    }
+    
+    private void writeResults(Writer w, boolean header) throws IOException { 
+        Iterator oi = onsets.iterator(); 
+        Iterator di = durations.iterator(); 
+        Iterator ioii = iois.iterator(); 
+        Iterator piti = pitches.iterator(); 
+            
+        Iterator pi = probes.iterator(); 
+        Iterator ri = responses.iterator(); 
+        Iterator rti = responseTimes.iterator(); 
+        int eventIndex = 1; 
+        
+        if (header) 
+            writeHeader(w); 
+        
+        String answerString = new String(""); 
+        Iterator ai = answers.iterator(); 
+        while(ai.hasNext())
+            answerString = answerString + (String)ai.next() + " ";
+        
+        while(oi.hasNext()) { 
+            long onset = ((Long)oi.next()).longValue();
+            long duration = ((Long)di.next()).longValue(); 
+            long ioi = ((Long)ioii.next()).longValue();
+            int pitch = ((Integer)piti.next()).intValue(); 
+
+            ProbeID p = (ProbeID)pi.next(); 
+            
+            int probe = 0; 
+            int response = 0;  
+            long responseTime = 0; 
+            
+            switch(p) { 
+            case NOT_PROBE: 
+            case START_CLOCK:
+            case BEFORE_PROBE: 
+            case AFTER_PROBE: 
+                break; 
+            case PROBE:
+            case PROBE_EX: 
+            case PROBE_UNEX:
+                if (p == ProbeID.PROBE_UNEX) 
+                    probe = 1; 
+                else if (p == ProbeID.PROBE_EX)
+                    probe = 2; 
+                else if (p == ProbeID.PROBE)
+                    probe = 1;
+                
+                if (ri.hasNext())
+                    response = ((Integer)ri.next()).intValue();
+                if (rti.hasNext()) { 
+                    responseTime = ((Long)rti.next()).longValue();
+                }
+                break; 
+            default: 
+                System.out.println("Unexpected probe id: " + p); 
+                break; 
+            }
+            w.write(subjectID + " " + composition + " " + 
+                    eventIndex + " " + 
+                    onset + " " + duration + " " + ioi + " " + 
+                    pitch + " " + 
+                    probe + " " + response + " " + responseTime + " " + 
+                    answerString + "\n"); 
+            eventIndex++; 
+        }
+    }
+    
+    private void writeHeader(Writer w) throws IOException {
+        w.write("subject melody note " + 
+                "onset duration ioi pitch " + 
+                "probe response time"); 
+        Iterator qi = questions.iterator(); 
+        while(qi.hasNext())
+            w.write(" " + (String)qi.next());
+        w.write("\n"); 
+    }
+}