changeset 39:796b3e3e053f

Changed MidiPlayer.java to allow the use of external sequencer programs (e.g. Garageband). Changed Experiment.java to allow user to specify in commandline whether to use default MidiDevice (true), specified MidiDevice (0<variable<MidiDevs.length), or list the available devices on start up. Changed ExperimentGui.java and SubjectDataPanel.java to add familiarity question if familiarity question isn't included at the presentation of each stimuli.
author CBussey
date Fri, 31 May 2013 17:40:59 +0100
parents a36ca20cffd4
children af173212eb42
files Experiment.java ExperimentGui.java MidiPlayer.java SubjectDataPanel.java
diffstat 4 files changed, 153 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/Experiment.java	Fri Nov 16 10:39:57 2012 +0000
+++ b/Experiment.java	Fri May 31 17:40:59 2013 +0100
@@ -11,6 +11,7 @@
 import java.awt.*;
 import java.awt.Color;
 import javax.sound.midi.*;
+import java.util.Scanner;
 
 public class Experiment { 
 
@@ -104,7 +105,7 @@
     }
 
     /* Constructor */ 
-    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) { 
+    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) {
         
         // Setup variables 
 	debug = (de != 0);
@@ -200,7 +201,8 @@
         int sc = Integer.parseInt(args[n++]); 
         int cu = Integer.parseInt(args[n++]); 
         int nu = Integer.parseInt(args[n++]); 
-        int md = Integer.parseInt(args[n++]); 
+        String mdInput = args[n++];
+        int md = -1;
         int sl = Integer.parseInt(args[n++]); 
         String la = args[n++];
         String ha = args[n++];
@@ -212,6 +214,32 @@
         int quest = Integer.parseInt(args[n++]);
         int de = Integer.parseInt(args[n++]);
 
+    if(isBooleanFormat(mdInput)){
+        boolean defaultMD = Boolean.parseBoolean(mdInput);
+        
+        if(!defaultMD){
+            displayMIDIDevices();
+            System.out.print("Specify the desired MIDI Device: ");
+            Scanner scan = new Scanner(System.in);
+            String input = scan.next();
+            
+            while(!isMIDIDevice(input)){
+                System.out.print("Enter a valid input:");
+                input = scan.next();
+            }
+            md = Integer.parseInt(input);
+        }
+        else{
+            md = -1;
+        }
+    }
+    else if(isMIDIDevice(mdInput)){
+            md = Integer.parseInt(mdInput);
+    }
+    else{
+        showUsage();
+    }
+
         // Create experiment 
         Experiment exp = new Experiment(sc, cu, nu, sl, md, la, ha, mfd, inf, rdr, fam, lik, quest, de);
         
@@ -221,31 +249,61 @@
         exp.showGUI(width, height); 
 
 	} else {
-	     System.out.println("Usage: " + "\t" + "java Experiment " + 
-                               "<show clock?> <clock units> <number of units> " + 
-                               "<midi device> " + 
-                               "<scale length> <low anchor> <high anchor> " +
-                               "<midi file directory> <instructions file> <results directory>" + 
-                               "<familiarity>" + "<pleasantness>" + "<questionnaire>" + "<debug>");
-            System.exit(1);
+        showUsage();
 	}
     }
+    
+    public static void showUsage(){
+        System.out.println("Usage: " + "\t" + "java Experiment " +
+                           "<show clock?> <clock units> <number of units> " +
+                           "<default MIDI device?> " +
+                           "<scale length> <low anchor> <high anchor> " +
+                           "<midi file directory> <instructions file> <results directory>" +
+                           "<familiarity>" + "<pleasantness>" + "<questionnaire>" + "<debug>");
+        System.exit(1);
+    }
 
     /*
      * Print a list of accessible MIDI devices
      */
-    protected void displayMIDIDevices() {
+    public static void displayMIDIDevices() {
 	System.out.println("Searching for MIDI devices...");
 	    
 	MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
 	if (devices.length == 0) {
 	    System.out.println("No MIDI devices found");
 	} else {
-	    for (MidiDevice.Info dev : devices) {
-		System.out.println("MIDI device: " + dev);
+	    for (int i=0; i<devices.length; i++) {
+            MidiDevice.Info m = devices[i];
+            System.out.println(i+". MIDI device: " + m.getName());
 	    }
 	}
     }
+    
+    public static boolean isBooleanFormat(String s){
+        if(s.toLowerCase().equals("true") || s.toLowerCase().equals("false")){
+            return true;
+        }
+        else {
+            return false;
+        }
+    }
+
+	public static boolean isMIDIDevice(String dev){
+		boolean output;
+        MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
+        
+		try {
+			int device = Integer.parseInt(dev);
+            if(device>devices.length-1 || device<0){
+                output = false;
+            }
+            else { output = true; }
+		} catch(NumberFormatException e) {
+			output = false;
+		}
+		return output;
+	}
 
     private int positiveInteger(int x, int def) {
 	if (x > 0) 
--- a/ExperimentGui.java	Fri Nov 16 10:39:57 2012 +0000
+++ b/ExperimentGui.java	Fri May 31 17:40:59 2013 +0100
@@ -65,7 +65,7 @@
         instructionsPanel = new InstructionsPanel(this);
         stimulusPanel = new StimulusPanel(this, clock); 
         interBlockPanel = new InterBlockPanel(exp); 
-        subjectDataPanel = new SubjectDataPanel(this, exp.getSubjectResults()); 
+        subjectDataPanel = new SubjectDataPanel(this, exp.getSubjectResults(), !exp.getAskFamiliarity());
 
 	stimulusPanel.setResponseEnabled(false);
         
--- a/MidiPlayer.java	Fri Nov 16 10:39:57 2012 +0000
+++ b/MidiPlayer.java	Fri May 31 17:40:59 2013 +0100
@@ -45,20 +45,32 @@
     private Synthesizer synthesizer = null;
     private Sequence sequence = null;
     private ArrayList<Long> onsets, offsets = null;
-    private ArrayList<Integer> pitches, velocities = null; 
-    private int midiDevice = 0; /* 4 for usb midi device */ 
+    private ArrayList<Integer> pitches, velocities = null;
+    private File midiFile;
+    private boolean defaultMD = false;
+    private int midiDevice;
     private boolean debug;
 
     /* accessors */ 
-    public Sequencer getSequencer() { return sequencer; }  
+    public Sequencer getSequencer() { return sequencer; }
+    public boolean usingDefault() { return defaultMD; }
 
-    /* Constructors */ 
-    public MidiPlayer(String path, int deviceNumber, boolean d) { 
-        File midiFile = new File(path);
-        midiDevice = deviceNumber;
-	debug = d;
-
-        // Get sequence 
+    /* Constructors */
+    public MidiPlayer(String path, int deviceNumber, boolean d) {
+        if(deviceNumber == -1) {
+            defaultMD = true;
+            midiDevice = 0;
+        }
+        else{
+            midiDevice = deviceNumber;
+        }
+        midiFile = new File(path);
+        debug = d;
+        setup();
+    }
+    
+    private void setup(){
+        // Get sequence
         try { sequence = MidiSystem.getSequence(midiFile); }
         catch (InvalidMidiDataException e) {
             e.printStackTrace();
@@ -68,12 +80,6 @@
             e.printStackTrace();
             System.exit(1);
         }
-        // Get sequencer  
-        try { sequencer = MidiSystem.getSequencer(); }
-        catch (MidiUnavailableException e) {
-            e.printStackTrace();
-            System.exit(1);
-        }
         //sequencer.setTempoInBPM(bpm); 
         // Workaround bug in JDK 
 //         sequencer.addMetaEventListener(new MetaEventListener() {
@@ -86,36 +92,43 @@
 //                     }
 //                 }
 //             });
-        // Open sequencer 
-        try { sequencer.open(); }
-        catch (MidiUnavailableException e) {
-            e.printStackTrace();
-            System.exit(1);
-        }
-        // Assign sequence to sequencer 
-        try { sequencer.setSequence(sequence); }
-        catch (InvalidMidiDataException e) {
-            e.printStackTrace();
-            System.exit(1);
-        }
-        // Set up MIDI output for sequence 
+        // Set up MIDI output for sequence
         try {
             MidiDevice.Info msinfo[] = MidiSystem.getMidiDeviceInfo(); 
             for(int i = 0; i < msinfo.length; i++) { 
-                MidiDevice.Info m = msinfo[i]; 
-//                 System.out.println("Name: " + m.getName() + 
-//                                    "; Vendor: " + m.getVendor() + 
-//                                    "; Version: " + m.getVersion()); 
+		System.out.println("Carl: "+i);
+		MidiDevice.Info m = msinfo[i];
+                 System.out.println("Name: " + m.getName() + 
+                                    "; Vendor: " + m.getVendor() + 
+                                    "; Version: " + m.getVersion()); 
             }
+		MidiDevice synth = MidiSystem.getMidiDevice(msinfo[midiDevice]);
+		synth.open();
+		// Get sequencer  
+        	try { sequencer = MidiSystem.getSequencer(defaultMD); }
+        	catch (MidiUnavailableException e) {
+            		e.printStackTrace();
+           		System.exit(1);
+        	}
             // synthesizer = MidiSystem.getSynthesizer();
-            MidiDevice synth = MidiSystem.getMidiDevice(msinfo[midiDevice]); 
+
+		try { sequencer.open(); }////
+       		catch (MidiUnavailableException e) {
+            		e.printStackTrace();
+            		System.exit(1);
+        	}
+        	// Assign sequence to sequencer 
+        	try { sequencer.setSequence(sequence); }
+        	catch (InvalidMidiDataException e) {
+            		e.printStackTrace();
+            		System.exit(1);
+        	}////
 
             // Change the patch 
             //             MidiChannel channels[] = synthesizer.getChannels(); 
             //             for (int i = 0; i < channels.length; i++) 
             //                 channels[i].programChange(65);
-
-            synth.open();
+		
             Receiver synthReceiver = synth.getReceiver();
             Transmitter	seqTransmitter = sequencer.getTransmitter();
             seqTransmitter.setReceiver(synthReceiver);
--- a/SubjectDataPanel.java	Fri Nov 16 10:39:57 2012 +0000
+++ b/SubjectDataPanel.java	Fri May 31 17:40:59 2013 +0100
@@ -16,18 +16,25 @@
     /* variables */ 
     private JTextField ageField, listeningField, nationalityField; 
     private JComboBox sexBox, handBox, hearingBox, ethnicityBox, 
-        goldMSI3_1, goldMSI3_2, goldMSI3_3, goldMSI3_4, goldMSI3_5, goldMSI3_6, goldMSI3_7, goldMSI3_8, goldMSI3_9;
+        goldMSI3_1, goldMSI3_2, goldMSI3_3, goldMSI3_4, goldMSI3_5, goldMSI3_6, goldMSI3_7, goldMSI3_8, goldMSI3_9, goldMSI3_10;
 
     private JButton finishButton; 
 
-    private SubjectResults results; 
+    private SubjectResults results;
+    
+    private boolean askFamiliarity = false;
 
     /* accessors */ 
     public JButton getFinishButton() { return finishButton; }
 
-    /* constructor */ 
-    public SubjectDataPanel(ExperimentGui gui, SubjectResults sr) { 
-	
+    /* constructors */
+    public SubjectDataPanel(ExperimentGui gui, SubjectResults sr) {
+        this(gui, sr, false);
+    }
+    
+    public SubjectDataPanel(ExperimentGui gui, SubjectResults sr, boolean askFamiliarity) {
+    
+    this.askFamiliarity = askFamiliarity;
 	results = sr; 
 
 	JPanel questionsPanel = new JPanel(); 
@@ -177,6 +184,16 @@
         c.anchor = GridBagConstraints.WEST;
         questionsPanel.add(goldMSI3_9, c);
         
+        if(askFamiliarity){
+            c.gridy = 16;
+            String[] goldMSI3_10o = {"", "25%", "50%", "75%", "100%"};
+            goldMSI3_10 = new JComboBox(goldMSI3_10o);
+            c.anchor = GridBagConstraints.EAST;
+            questionsPanel.add(new JLabel("I was familiar with approximately ------- of the study songs."), c);
+            c.anchor = GridBagConstraints.WEST;
+            questionsPanel.add(goldMSI3_10, c);
+        }
+        
         // Put it all together 
         JPanel finishPanel = new JPanel(); 
 	finishButton = new JButton("Finish"); 
@@ -237,13 +254,17 @@
         String[] goldMSI3_8a = {"GoldMSI3.8", (String)goldMSI3_8.getSelectedItem()};
         subjectData.add(goldMSI3_8a);
         String[] goldMSI3_9a = {"GoldMSI3.9", (String)goldMSI3_9.getSelectedItem()};
-        subjectData.add(goldMSI3_9a);
+        if(askFamiliarity){
+            subjectData.add(goldMSI3_9a);
+            String[] goldMSI3_10a = {"GoldMSI3.10", (String)goldMSI3_10.getSelectedItem()};
+            subjectData.add(goldMSI3_10a);
+        }
 
-        results.setSubjectData(subjectData); 
+        results.setSubjectData(subjectData);
     } 
 
     public boolean allDataEntered () { 
-        if (ageField.getText().equals("") || 
+        if (ageField.getText().equals("") ||
             sexBox.getSelectedItem().equals("") ||
             handBox.getSelectedItem().equals("") ||
             ethnicityBox.getSelectedItem().equals("") || 
@@ -258,10 +279,11 @@
             goldMSI3_6.getSelectedItem().equals("") ||
             goldMSI3_7.getSelectedItem().equals("") ||
             goldMSI3_8.getSelectedItem().equals("") ||
-            goldMSI3_9.getSelectedItem().equals("")
+            goldMSI3_9.getSelectedItem().equals("") 
             )
-            return false; 
-        else return true; 
+            return false;
+        else if (askFamiliarity && goldMSI3_10.getSelectedItem().equals("")) return false;
+        else return true;
     }
 
     public void addFinishButtonListener(ActionListener al) {