annotate Experiment.java @ 35:c860b5b4428d

Remove Block.class
author Jeremy Gow <jeremy.gow@gmail.com>
date Wed, 14 Nov 2012 12:15:51 +0000
parents b083ddc5c546
children 69dafbbfc422
rev   line source
m@0 1 /*=============================================================================
m@0 2 * File: Experiment.java
m@0 3 * Author: Marcus Pearce <m.pearce@gold.ac.uk>
m@0 4 * Created: <2007-02-14 11:28:27 marcusp>
marcus@14 5 * Time-stamp: <2012-01-16 17:10:48 marcusp>
m@0 6 *=============================================================================
m@0 7 */
m@0 8
m@0 9 import java.io.*;
m@0 10 import javax.swing.UIManager;
m@0 11 import java.awt.*;
m@0 12 import java.awt.Color;
jeremy@27 13 import javax.sound.midi.*;
m@0 14
m@0 15 public class Experiment {
m@0 16
m@0 17 /* pathnames */
m@0 18 private final String BASE_DIRECTORY =
m@0 19 new File("").getAbsolutePath() + File.separator;
marcus@14 20 private final String DATA_DIRECTORY =
marcus@14 21 BASE_DIRECTORY + "Data" + File.separator;
m@4 22
marcus@14 23 public String RESULTS_DIRECTORY =
m@0 24 BASE_DIRECTORY + "Results" + File.separator;
marcus@14 25 public String RESULTS_EXTENSION = ".dat";
marcus@14 26 public String SUBJECT_RESULTS_FILE =
m@0 27 RESULTS_DIRECTORY + "subjects" + RESULTS_EXTENSION;
m@0 28
m@7 29 public String INSTRUCTIONS_FILE =
m@0 30 DATA_DIRECTORY + "instructions.html";
m@4 31
m@4 32 public String MIDI_DIRECTORY =
m@4 33 DATA_DIRECTORY + "Midi" + File.separator;
m@4 34 public String MIDIFILELIST_FILE =
m@4 35 MIDI_DIRECTORY + "filelist.txt";
m@4 36 public String PRACTICE_MIDIFILELIST_FILE =
m@4 37 MIDI_DIRECTORY + "pfilelist.txt";
m@4 38
m@0 39
m@0 40 /* The GUI */
m@0 41 private ExperimentGui gui;
m@0 42
m@3 43 /* whether to show the clock */
m@3 44 private boolean showClock;
m@0 45 /* the units of the clock as multiples of the tatum */
m@0 46 private int clockUnits;
m@0 47 /* number of units that clock runs for before a probe event */
m@0 48 private int numUnits;
m@0 49
m@8 50 /* whether to ask about familiarity of each song */
m@8 51 private boolean askFamiliarity;
m@8 52 /* whether to ask about liking of each song */
m@8 53 private boolean askLiking;
m@8 54 /* whether to include final questionnaire */
m@8 55 private boolean finalQuestionnaire;
m@8 56
m@0 57 /* the blocks of the experiment */
m@0 58 Block[] blocks;
m@0 59 int currentBlockID;
m@0 60
m@0 61 /* Subject ID */
m@0 62 private int subjectID;
m@0 63
m@0 64 /* Results */
m@0 65 private SubjectResults results;
m@0 66
m@0 67 /* the details of the rating scale */
m@0 68 private int scaleLength;
m@0 69 private String lowAnchor;
m@0 70 private String highAnchor;
m@0 71
m@1 72 /* the midi device */
m@1 73 private int midiDevice;
m@1 74
m@10 75 /* debugging */
jeremy@32 76 private boolean debug;
m@10 77
m@0 78 /* accessors */
m@10 79 public boolean getDebug() { return debug; }
m@8 80 public boolean getAskLiking() { return askLiking; }
m@8 81 public boolean getAskFamiliarity() { return askFamiliarity; }
m@8 82 public boolean getFinalQuestionnaire() { return finalQuestionnaire; }
m@8 83
m@1 84 public int getMidiDeviceNumber() { return midiDevice; }
m@0 85 public int getScaleLength() { return scaleLength; }
m@0 86 public String getLowAnchor() { return lowAnchor; }
m@0 87 public String getHighAnchor() { return highAnchor; }
m@0 88
m@0 89 public String getMidiDirectory() { return MIDI_DIRECTORY; }
m@0 90 public SubjectResults getSubjectResults() { return results; }
m@0 91 public Block getCurrentBlock() { return blocks[currentBlockID]; }
m@0 92 public int getCurrentBlockID() { return currentBlockID + 1; }
m@3 93 public boolean showClock() { return showClock; }
m@0 94 public int getClockUnits() { return clockUnits; }
m@0 95 public int getNumUnits() { return numUnits; }
m@0 96 public String getInstructionsFile() { return INSTRUCTIONS_FILE; }
m@0 97 public int getSubjectID() { return subjectID; }
m@0 98 public void setSubjectID(int id) {
m@0 99 subjectID = id;
m@0 100 results.setSubjectID(id);
m@0 101 results.setOutputFile(id);
m@0 102 getCurrentBlock().getMelodyResults().setSubjectID(id);
jeremy@27 103 System.out.println("Subject ID = " + subjectID);
m@0 104 }
m@0 105
m@0 106 /* Constructor */
jeremy@32 107 public Experiment (int sc, int cu, int nu, int sl, int md, String la, String ha, String mfd, String inf, String rdr, int fam, int lik, int quest, int de) {
m@0 108
m@0 109 // Setup variables
jeremy@32 110 debug = (de != 0);
m@0 111 results = new SubjectResults(this);
m@3 112 if (sc == 0)
m@3 113 showClock = false;
m@3 114 else
m@3 115 showClock = true;
jeremy@33 116 clockUnits = positiveInteger(cu, 1);
jeremy@33 117 numUnits = positiveInteger(nu, 4);
jeremy@33 118 scaleLength = positiveInteger(sl, 7);
m@1 119 midiDevice = md;
m@0 120 lowAnchor = la;
m@0 121 highAnchor = ha;
m@4 122 MIDI_DIRECTORY = mfd + File.separator;
m@4 123 MIDIFILELIST_FILE = MIDI_DIRECTORY + "filelist.txt";
m@4 124 PRACTICE_MIDIFILELIST_FILE = MIDI_DIRECTORY + "pfilelist.txt";
m@7 125 INSTRUCTIONS_FILE = inf;
marcus@14 126 RESULTS_DIRECTORY = rdr + File.separator;
marcus@14 127 SUBJECT_RESULTS_FILE =
marcus@14 128 RESULTS_DIRECTORY + "subjects" + RESULTS_EXTENSION;
m@8 129
m@8 130 if (fam == 0)
m@8 131 askFamiliarity = false;
m@8 132 else
m@8 133 askFamiliarity = true;
m@8 134 if (lik == 0)
m@8 135 askLiking = false;
m@8 136 else
m@8 137 askLiking = true;
m@8 138 if (quest == 0)
m@8 139 finalQuestionnaire = false;
m@8 140 else
m@8 141 finalQuestionnaire = true;
m@0 142
jeremy@32 143 if (debug)
jeremy@32 144 displayMIDIDevices();
jeremy@32 145
m@0 146 // Initialise the experiment
m@0 147 Block practice = new Block(this, gui, PRACTICE_MIDIFILELIST_FILE,
m@0 148 "Practice", false);
m@0 149 Block main = new Block(this, gui, MIDIFILELIST_FILE,
m@0 150 "Main", true);
m@0 151 blocks = new Block[2];
m@0 152 blocks[0] = practice;
m@0 153 blocks[1] = main;
m@0 154 currentBlockID = 0;
m@0 155
m@0 156 // Create the GUI
m@0 157 gui = new ExperimentGui(this);
m@0 158 }
m@0 159
m@0 160 public Boolean nextBlock() {
m@0 161 boolean lastBlock = true;
m@0 162 if (currentBlockID + 1 < blocks.length)
m@0 163 currentBlockID = currentBlockID + 1;
m@0 164 else
m@0 165 lastBlock = false;
m@0 166 return lastBlock;
m@0 167 }
m@0 168
m@0 169 public void addToSubjectResults(MelodyResults mr) { results.addResult(mr); }
m@0 170
m@0 171 public void runExperiment () {
m@0 172 (new Thread(gui)).start();
m@0 173 getCurrentBlock().presentStimulus();
m@0 174 }
jeremy@27 175 public boolean isRunning() {
jeremy@27 176 Block cur = getCurrentBlock();;
jeremy@27 177 return cur != null && cur.isRunning();
jeremy@27 178 }
jeremy@27 179
jeremy@27 180 public boolean hasRun() {
jeremy@27 181 Block cur = getCurrentBlock();
jeremy@29 182 return cur == null || cur.hasRun();
jeremy@27 183 }
m@0 184
m@0 185 /* Show the GUI */
m@0 186 public void showGUI(int width, int height) {
m@0 187 gui.pack();
m@0 188 gui.setSize(width, height);
m@0 189 // UIManager.put("Button.focus", UIManager.get("Button.select"));
m@0 190 gui.setVisible(true);
m@0 191 }
m@0 192
m@0 193 /* Main method */
m@0 194 public static void main(String[] args) {
m@0 195 if (args.length == 0) {
m@0 196 System.out.println("Usage: " + "\t" + "java Experiment " +
m@3 197 "<show clock?> <clock units> <number of units> " +
m@3 198 "<midi device> " +
m@9 199 "<scale length> <low anchor> <high anchor> " +
marcus@14 200 "<midi file directory> <instructions file> <results directory>" +
jeremy@32 201 "<familiarity>" + "<pleasantness>" + "<questionnaire>" + "<debug>");
m@0 202 System.exit(1);
m@0 203 }
m@0 204
m@0 205 // Parse Arguments
m@3 206 int n = 0;
m@3 207 int sc = Integer.parseInt(args[n++]);
m@0 208 int cu = Integer.parseInt(args[n++]);
m@0 209 int nu = Integer.parseInt(args[n++]);
m@1 210 int md = Integer.parseInt(args[n++]);
m@0 211 int sl = Integer.parseInt(args[n++]);
m@0 212 String la = args[n++];
m@0 213 String ha = args[n++];
m@4 214 String mfd = args[n++];
m@7 215 String inf = args[n++];
marcus@14 216 String rdr = args[n++];
m@8 217 int fam = Integer.parseInt(args[n++]);
m@8 218 int lik = Integer.parseInt(args[n++]);
m@8 219 int quest = Integer.parseInt(args[n++]);
jeremy@32 220 int de = Integer.parseInt(args[n++]);
m@0 221
m@0 222 // Create experiment
jeremy@32 223 Experiment exp = new Experiment(sc, cu, nu, sl, md, la, ha, mfd, inf, rdr, fam, lik, quest, de);
m@0 224
m@0 225 // Show the GUI
m@0 226 int width=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
m@0 227 int height=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
m@0 228 exp.showGUI(width, height);
m@0 229 }
jeremy@27 230
jeremy@27 231 /*
jeremy@27 232 * Print a list of accessible MIDI devices
jeremy@27 233 */
jeremy@27 234 protected void displayMIDIDevices() {
jeremy@27 235 System.out.println("Searching for MIDI devices...");
jeremy@27 236
jeremy@27 237 MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
jeremy@27 238 if (devices.length == 0) {
jeremy@27 239 System.out.println("No MIDI devices found");
jeremy@27 240 } else {
jeremy@27 241 for (MidiDevice.Info dev : devices) {
jeremy@27 242 System.out.println("MIDI device: " + dev);
jeremy@27 243 }
jeremy@27 244 }
jeremy@27 245 }
jeremy@27 246
jeremy@33 247 private int positiveInteger(int x, int def) {
jeremy@33 248 if (x > 0)
jeremy@33 249 return x;
jeremy@33 250 else
jeremy@33 251 return def;
jeremy@33 252 }
jeremy@33 253
m@0 254 }