view Experiment.java @ 3:6108a8aa9d82

Add command line option to turn off clock. Ignore-this: 1b88592bed48f4491e1563cccfbccbeb
author Marcus Pearce <m.pearce@gold.ac.uk>
date Mon, 21 Jun 2010 17:37:24 +0100
parents 93ed757b9871
children 5080b65e6963
line wrap: on
line source
/*=============================================================================
 * File:       Experiment.java
 * Author:     Marcus Pearce <m.pearce@gold.ac.uk>
 * Created:    <2007-02-14 11:28:27 marcusp>
 * Time-stamp: <2010-06-21 16:51:26 marcusp>
 *=============================================================================
 */

import java.io.*;
import javax.swing.UIManager; 
import java.awt.*;
import java.awt.Color;

public class Experiment { 

    /* pathnames */ 
    private final String BASE_DIRECTORY = 
        new File("").getAbsolutePath() + File.separator; 
    private final String DATA_DIRECTORY = 
        BASE_DIRECTORY + "Data" + File.separator; 
    public final String RESULTS_DIRECTORY = 
        BASE_DIRECTORY + "Results" + File.separator; 

    private final String MIDI_DIRECTORY = 
        DATA_DIRECTORY + "Midi" + File.separator;

    public final String RESULTS_EXTENSION = ".dat"; 
    public final String SUBJECT_RESULTS_FILE = 
        RESULTS_DIRECTORY + "subjects" + RESULTS_EXTENSION; 

    public final String INSTRUCTIONS_FILE = 
        DATA_DIRECTORY + "instructions.html"; 
    public final String MIDIFILELIST_FILE = 
        DATA_DIRECTORY + "filelist.txt"; 
    public final String PRACTICE_MIDIFILELIST_FILE = 
        DATA_DIRECTORY + "pfilelist.txt"; 

    /* The GUI */ 
    private ExperimentGui gui; 

    /* whether to show the clock */
    private boolean showClock;
    /* the units of the clock as multiples of the tatum */
    private int clockUnits; 
    /* number of units that clock runs for before a probe event */ 
    private int numUnits; 

    /* the blocks of the experiment */ 
    Block[] blocks;
    int currentBlockID; 
    
    /* Subject ID */ 
    private int subjectID; 

    /* Results */ 
    private SubjectResults results; 

    /* the details of the rating scale */
    private int scaleLength;
    private String lowAnchor;
    private String highAnchor;

    /* the midi device */
    private int midiDevice;

    /* accessors */ 
    public int getMidiDeviceNumber() { return midiDevice; }
    public int getScaleLength() { return scaleLength; }
    public String getLowAnchor() { return lowAnchor; }
    public String getHighAnchor() { return highAnchor; }

    public String getMidiDirectory() { return MIDI_DIRECTORY; }
    public SubjectResults getSubjectResults() { return results; }
    public Block getCurrentBlock() { return blocks[currentBlockID]; }
    public int getCurrentBlockID() { return currentBlockID + 1; }
    public boolean showClock() { return showClock; }
    public int getClockUnits() { return clockUnits; } 
    public int getNumUnits() { return numUnits; } 
    public String getInstructionsFile() { return INSTRUCTIONS_FILE; }
    public int getSubjectID() { return subjectID; }
    public void setSubjectID(int id) { 
        subjectID = id; 
        results.setSubjectID(id); 
        results.setOutputFile(id); 
        getCurrentBlock().getMelodyResults().setSubjectID(id); 
    }

    /* Constructor */ 
    public Experiment (int sc, int cu, int nu, int sl, int md, String la, String ha) { 
        
        // Setup variables 
        results = new SubjectResults(this); 
        if (sc == 0) 
            showClock = false;
        else 
            showClock = true;
        clockUnits = cu; 
        numUnits = nu; 
        scaleLength = sl;
        midiDevice = md;
        lowAnchor = la;
        highAnchor = ha;

        // Initialise the experiment 
        Block practice = new Block(this, gui, PRACTICE_MIDIFILELIST_FILE, 
                                   "Practice", false); 
        Block main = new Block(this, gui, MIDIFILELIST_FILE, 
                               "Main", true); 
        blocks  = new Block[2]; 
        blocks[0] = practice; 
        blocks[1] = main; 
        currentBlockID = 0; 

        // Create the GUI 
        gui = new ExperimentGui(this); 
    }

    public Boolean nextBlock() { 
        boolean lastBlock = true; 
        if (currentBlockID + 1 < blocks.length) 
            currentBlockID = currentBlockID + 1; 
        else 
            lastBlock = false; 
        return lastBlock; 
    }

    public void addToSubjectResults(MelodyResults mr) { results.addResult(mr); }

    public void runExperiment () { 
        (new Thread(gui)).start();
        getCurrentBlock().presentStimulus(); 
    }
    public boolean isRunning() { return getCurrentBlock().isRunning(); }
    public boolean hasRun() { return getCurrentBlock().hasRun(); } 

    /* Show the GUI */ 
    public void showGUI(int width, int height) { 
        gui.pack();
        gui.setSize(width, height); 
        // UIManager.put("Button.focus", UIManager.get("Button.select"));
        gui.setVisible(true);
    } 

    /* Main method */ 
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Usage: " + "\t" + "java Experiment " + 
                               "<show clock?> <clock units> <number of units> " + 
                               "<midi device> " + 
                               "<scale length> <low anchor> <high anchor>"); 
            System.exit(1);
        }
        
        // Parse Arguments 
        int n = 0;
        int sc = Integer.parseInt(args[n++]); 
        int cu = Integer.parseInt(args[n++]); 
        int nu = Integer.parseInt(args[n++]); 
        int md = Integer.parseInt(args[n++]); 
        int sl = Integer.parseInt(args[n++]); 
        String la = args[n++];
        String ha = args[n++];

        // Create experiment 
        Experiment exp = new Experiment(sc, cu, nu, sl, md, la, ha);
        
        // Show the GUI 
        int width=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int height=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        exp.showGUI(width, height); 
    }
}