Skip to content

Commit

Permalink
Fixing a bug with the key configuration process. Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarehart committed Oct 5, 2013
1 parent 5be06ac commit c42f470
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
82 changes: 42 additions & 40 deletions src/tarehart/alter/AlterForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class AlterForm {

private static final int MAX_VOLUME = 400;
private static final int GRACE_PERIOD = 1000; // 1000 milliseconds = 1 second
private static final int GRACE_PERIOD = 500; // 1000 milliseconds = 1 second
private static final int DEFAULT_THRESHOLD = 50;
private static final String CONFIG_THRESHOLD = "threshold";
private static final String CONFIG_KEY_CODE = "keyCode";
Expand All @@ -27,20 +27,21 @@ public class AlterForm {

public AlterForm() throws AWTException {

microphoneAnalyzer = new MicrophoneAnalyzer();
setupMicrophones();
populateMicrophoneCombo();

microphoneAnalyzer = new MicrophoneAnalyzer();
presser = new KeyPresser();
judge = new TalkingJudge(presser, GRACE_PERIOD);
presser.setKey(KeyEvent.VK_ALT);
spinner1.setValue(KeyEvent.VK_ALT);

setupKeyBinder();

progressBar1.setMaximum(MAX_VOLUME);
slider1.setMaximum(MAX_VOLUME);

readFromPreferences();
setUIFromPreferences();

setMicrophoneFromUI();
setKeyFromUI();

addUIListeners();

microphoneAnalyzer.addListener(new AmplitudeUpdateListener() {
@Override
Expand All @@ -60,17 +61,46 @@ public void amplitudeUpdated(float newAmplitude) {
}
});

}

private void addUIListeners() {
spinner1.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
presser.setKey((Integer) spinner1.getValue());
setKeyFromUI();
}
});

comboBox1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
setMicrophoneFromUI();
}
}
});

button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
KeyGrabber.grabNextKey(spinner1);
}
});
}

private void setKeyFromUI() {
presser.setKey((Integer) spinner1.getValue());
}

private void setMicrophoneFromUI() {
try {
microphoneAnalyzer.setMixer((Mixer.Info) comboBox1.getSelectedItem());
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}

private void readFromPreferences() {
private void setUIFromPreferences() {

slider1.setValue(preferences.getInt(CONFIG_THRESHOLD, DEFAULT_THRESHOLD));
spinner1.setValue(preferences.getInt(CONFIG_KEY_CODE, KeyEvent.VK_ALT));
Expand All @@ -84,16 +114,7 @@ private void savePreferences() {
preferences.putInt(CONFIG_MIC_CHOICE, comboBox1.getSelectedIndex());
}

private void setupKeyBinder() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
KeyGrabber.grabNextKey(spinner1);
}
});
}

private void setupMicrophones() {
private void populateMicrophoneCombo() {

comboBox1.setRenderer(new ListCellRenderer() {
@Override
Expand All @@ -109,25 +130,6 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
comboBox1.addItem(mixer);
}

try {
microphoneAnalyzer.setMixer(comboBox1.getItemAt(0));
} catch (LineUnavailableException e) {
e.printStackTrace();
}

comboBox1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Mixer.Info mixer = (Mixer.Info) e.getItem();
try {
microphoneAnalyzer.setMixer(mixer);
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
}
}
});
}


Expand Down Expand Up @@ -184,7 +186,7 @@ public static void main(String[] args) {
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (AWTException e) {
} catch (Throwable e) {
e.printStackTrace();
}

Expand Down
7 changes: 4 additions & 3 deletions src/tarehart/alter/MicrophoneAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void stop() {
class CaptureThread extends Thread{

//An arbitrary-size temporary holding buffer
byte tempBuffer[] = new byte[100];
byte tempBuffer[] = new byte[250];
public void run(){

threadEnded = false;
Expand All @@ -94,13 +94,14 @@ public void run(){
aul.amplitudeUpdated(currentLevel);
}
}

}

microphone.close();
threadEnded = true;
} catch (Exception e) {
} catch (Throwable e) {
System.out.println(e);
System.exit(0);
System.exit(-1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tarehart/alter/TalkingJudge.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TalkingJudge {
private KeyPresser presser;
private Timer timer;

public TalkingJudge(KeyPresser presser, int gracePeriod) throws AWTException {
public TalkingJudge(KeyPresser presser, int gracePeriod) {
this.gracePeriod = gracePeriod;
this.presser = presser;

Expand Down

0 comments on commit c42f470

Please sign in to comment.