Skip to content

Commit

Permalink
Saving configuration between uses. Shutting down more cleanly so the …
Browse files Browse the repository at this point in the history
…button press doesn't linger.
  • Loading branch information
tarehart committed Oct 4, 2013
1 parent 8d587ff commit 5be06ac
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 21 deletions.
73 changes: 64 additions & 9 deletions src/tarehart/alter/AlterForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
import java.awt.event.*;
import java.net.URL;
import java.util.List;
import java.util.prefs.Preferences;


public class AlterForm {

public static final int MAX_VOLUME = 50;
public static final int GRACE_PERIOD = 1000; // 1000 milliseconds = 1 second
private static final int MAX_VOLUME = 400;
private static final int GRACE_PERIOD = 1000; // 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";
private static final String CONFIG_MIC_CHOICE = "microphoneChoice";
private TalkingJudge judge;
private KeyPresser presser;
private MicrophoneAnalyzer microphoneAnalyzer;
protected Preferences preferences = Preferences.userNodeForPackage(AlterForm.class);

public AlterForm() throws AWTException {

Expand All @@ -34,12 +40,12 @@ public AlterForm() throws AWTException {
progressBar1.setMaximum(MAX_VOLUME);
slider1.setMaximum(MAX_VOLUME);

slider1.setValue(3);
readFromPreferences();

microphoneAnalyzer.addListener(new AmplitudeUpdateListener() {
@Override
public void amplitudeUpdated(float newAmplitude) {
int level = (int) newAmplitude;
int level = (int) newAmplitude * 10; // multiply by 10 so we get finer config with an int
progressBar1.setValue(level);
if (level >= slider1.getValue()) {
judge.gainSound();
Expand All @@ -64,6 +70,20 @@ public void stateChanged(ChangeEvent e) {

}

private void readFromPreferences() {

slider1.setValue(preferences.getInt(CONFIG_THRESHOLD, DEFAULT_THRESHOLD));
spinner1.setValue(preferences.getInt(CONFIG_KEY_CODE, KeyEvent.VK_ALT));
comboBox1.setSelectedIndex(preferences.getInt(CONFIG_MIC_CHOICE, 0));

}

private void savePreferences() {
preferences.putInt(CONFIG_KEY_CODE, (Integer) spinner1.getValue());
preferences.putInt(CONFIG_THRESHOLD, slider1.getValue());
preferences.putInt(CONFIG_MIC_CHOICE, comboBox1.getSelectedIndex());
}

private void setupKeyBinder() {
button1.addActionListener(new ActionListener() {
@Override
Expand All @@ -90,9 +110,9 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
}

try {
microphoneAnalyzer.setMixer((Mixer.Info) comboBox1.getItemAt(0));
microphoneAnalyzer.setMixer(comboBox1.getItemAt(0));
} catch (LineUnavailableException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
}

comboBox1.addItemListener(new ItemListener() {
Expand All @@ -110,6 +130,40 @@ public void itemStateChanged(ItemEvent e) {
});
}


private WindowListener getWindowListener() {
return new WindowListener() {
@Override
public void windowOpened(WindowEvent e) { }

@Override
public void windowClosing(WindowEvent e) {

microphoneAnalyzer.stop();
presser.release();

savePreferences();
}

@Override
public void windowClosed(WindowEvent e) { }

@Override
public void windowIconified(WindowEvent e) { }

@Override
public void windowDeiconified(WindowEvent e) { }

@Override
public void windowActivated(WindowEvent e) { }

@Override
public void windowDeactivated(WindowEvent e) { }
};
}



public static void main(String[] args) {

try {
Expand All @@ -125,12 +179,13 @@ public static void main(String[] args) {
try {
AlterForm m = new AlterForm();
frame.setContentPane(m.rootPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(m.getWindowListener());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (AWTException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
}


Expand All @@ -142,6 +197,6 @@ public static void main(String[] args) {
private JButton button1;
private JSpinner spinner1;
private JPanel statusLight;
private JComboBox comboBox1;
private JComboBox<Mixer.Info> comboBox1;
private JTextPane thisAppWillTakeTextPane;
}
30 changes: 18 additions & 12 deletions src/tarehart/alter/MicrophoneAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,34 @@ private void killExistingThread() {
stopCapture = true;
while (!threadEnded) {
try {
Thread.sleep(50);
} catch (InterruptedException e) { }
Thread.sleep(10);
} catch (InterruptedException e) {
// no biggie
}
}
}


private float calculateRMSLevel(byte[] audioData) {
// audioData might be buffered data read from a data line
long lSum = 0;
for(int i = 0; i < audioData.length; i++) {
lSum = lSum + audioData[i];
long sum = 0;
for (byte aud : audioData) {
sum += aud;
}

double dAvg = lSum / audioData.length;
double average = sum / audioData.length;

double sumMeanSquare = 0d;
for(int j = 0; j < audioData.length; j++) {
sumMeanSquare = sumMeanSquare + Math.pow(audioData[j] - dAvg, 2d);
double sumMeanSquare = 0;
for (byte aud : audioData) {
sumMeanSquare = sumMeanSquare + Math.pow(aud - average, 2);
}

double averageMeanSquare = sumMeanSquare / audioData.length;
return (float)(Math.pow(averageMeanSquare, 0.5) + 0.5);
return (float)(Math.pow(averageMeanSquare, .5) + .5);
}

public void stop() {
killExistingThread();
}


Expand All @@ -81,8 +87,8 @@ public void run(){
stopCapture = false;
try{
while(!stopCapture) {
int cnt = microphone.read(tempBuffer, 0, tempBuffer.length);
if(cnt > 0){
int bytesRead = microphone.read(tempBuffer, 0, tempBuffer.length);
if(bytesRead > 0){
float currentLevel = calculateRMSLevel(tempBuffer);
for (AmplitudeUpdateListener aul: listeners) {
aul.amplitudeUpdated(currentLevel);
Expand Down

0 comments on commit 5be06ac

Please sign in to comment.