Compare commits

..

No commits in common. "1e745b6d1c832124a929d7633ea569d1795a4d84" and "544c15be1d1a4b206e56b9a73b4b51b2681ff431" have entirely different histories.

4 changed files with 41 additions and 133 deletions

View File

@ -5,8 +5,8 @@ FreqSample is a tool made in Java that will play a frequency.
### Compiling ### Compiling
Make sure you have the JDK installed along with Apache Ant. Then go to the root directory of this project and run: Make sure you have the JDK installed along with Apache Ant. Then go to the root directory of this project and run:
```bash ```
$ ant $ ant jar
``` ```
### Contributing ### Contributing

View File

@ -11,7 +11,7 @@
<target name="compile" > <target name="compile" >
<mkdir dir="${classes.dir}" /> <mkdir dir="${classes.dir}" />
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" /> <javac srcdir="${src.dir}" destdir="${classes.dir}" />
</target> </target>
<target name="jar" depends="compile" > <target name="jar" depends="compile" >
@ -23,9 +23,5 @@
</jar> </jar>
</target> </target>
<target name="main" depends="jar" /> <target name="main" depends="jar" ></target>
<target name="run" depends="jar" >
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true" />
</target>
</project> </project>

View File

@ -4,138 +4,58 @@ import java.awt.event.*;
import javax.swing.*; import javax.swing.*;
import javax.sound.sampled.*; import javax.sound.sampled.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class FreqSample { public class FreqSample {
private static final String version = "v1.1.1"; public static final String version = "v0.2";
private JFrame frame; private JFrame frame;
private JPanel panel; private JPanel panel;
private JTextField hzField, msField; private JTextField hzField, msField;
private JLabel hzLabel, msLabel; private JLabel hzLabel, msLabel;
private JButton playButton; private JButton playButton;
private JMenuBar menuBar;
private JMenu fsMenu, helpMenu;
private JMenuItem quitItem, copyrightItem, aboutItem;
public FreqSample() { public FreqSample() {
frame = new JFrame("FreqSample"); frame = new JFrame("FreqSample");
frame.setSize(400, 200); frame.setSize(400, 160);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(); panel = new JPanel();
frame.add(panel); frame.add(panel);
setupMenu(); panel.setLayout(null);
setupUI();
hzLabel = new JLabel("Frequency (Hz):");
hzLabel.setBounds(10, 10, 150, 25);
panel.add(hzLabel);
hzField = new JTextField();
hzField.setBounds(170, 10, 220, 25);
panel.add(hzField);
msLabel = new JLabel("Duration (ms):");
msLabel.setBounds(10, 40, 150, 25);
panel.add(msLabel);
msField = new JTextField();
msField.setBounds(170, 40, 220, 25);
panel.add(msField);
playButton = new JButton("Play");
playButton.setBounds(160, 80, 80, 25);
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
double hz = Double.parseDouble(hzField.getText());
double ms = Double.parseDouble(msField.getText());
try {
Generator.generateTone(hz, ms);
} catch(InterruptedException ie) {
ie.printStackTrace();
} catch(LineUnavailableException lue) {
lue.printStackTrace();
}
}
});
panel.add(playButton);
frame.setVisible(true); frame.setVisible(true);
} }
private void setupMenu() {
menuBar = new JMenuBar();
fsMenu = new JMenu("FreqSample");
helpMenu = new JMenu("Help");
quitItem = new JMenuItem("Quit");
quitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
quitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
fsMenu.add(quitItem);
copyrightItem = new JMenuItem("Copyright Info");
copyrightItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("Copyright Information",
"FreqSample " + version + ", play frequencies.\n" +
"Copyright (C) 2014 Nicolás A. Ortega\n\n" +
"This program is free software: you can redistribute it and/or modify\n" +
"it under the terms of the GNU General Public License as published by\n" +
"the Free Software Foundation, either version 3 of the License, or\n" +
"(at your option) any later version.\n" +
"This program is distributed in the hope that it will be useful,\n" +
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
"GNU General Public License for more details.\n\n" +
"You should have received a copy of the GNU General Public License\n" +
"along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
}
});
helpMenu.add(copyrightItem);
aboutItem = new JMenuItem("About");
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("About",
"FreqSample " + version + "\n" +
"Copyright (C) 2014 Nicolás A. Ortega\n" +
"License: GNU GPLv3\n" +
"Contact: nicolas.ortega.froysa@gmail.com\n" +
"Source-Code: https://github.com/Deathsbreed/FreqSample\n" +
"Developers: Nicolás Ortega\n\n");
}
});
helpMenu.add(aboutItem);
menuBar.add(fsMenu);
menuBar.add(helpMenu);
frame.setJMenuBar(menuBar);
}
private void setupUI() {
panel.setLayout(null);
hzLabel = new JLabel("Frequency (Hz):");
hzLabel.setBounds(10, 10, 150, 25);
panel.add(hzLabel);
hzField = new JTextField();
hzField.setBounds(170, 10, 220, 25);
panel.add(hzField);
msLabel = new JLabel("Duration (ms):");
msLabel.setBounds(10, 40, 150, 25);
panel.add(msLabel);
msField = new JTextField();
msField.setBounds(170, 40, 220, 25);
panel.add(msField);
playButton = new JButton("Play");
playButton.setBounds(160, 80, 80, 25);
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
double hz = Double.parseDouble(hzField.getText());
double ms = Double.parseDouble(msField.getText());
try {
Generator.generateTone(hz, ms);
} catch(InterruptedException ie) {
ie.printStackTrace();
System.exit(1);
} catch(LineUnavailableException lue) {
lue.printStackTrace();
System.exit(1);
}
}
});
panel.add(playButton);
}
public class Window {
public Window(String title, String msg) {
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) { new FreqSample(); } public static void main(String[] args) { new FreqSample(); }
} }

View File

@ -7,7 +7,7 @@ import javax.swing.*;
/** /**
* @author Nicolás A. Ortega * @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega * @copyright Nicolás A. Ortega
* @license GNU GPLv3 * @license GPLv3
* @year 2014 * @year 2014
* *
*/ */
@ -21,7 +21,6 @@ public class Generator {
double fCyclePosition = 0; double fCyclePosition = 0;
// Open Audio Device
AudioFormat af = new AudioFormat(SAMPLE_RATE, 16, 1, true, true); AudioFormat af = new AudioFormat(SAMPLE_RATE, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, af); DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
@ -34,21 +33,15 @@ public class Generator {
line.open(af); line.open(af);
line.start(); line.start();
// The audio bytebuffer
ByteBuffer cBuf = ByteBuffer.allocate(line.getBufferSize()); ByteBuffer cBuf = ByteBuffer.allocate(line.getBufferSize());
// Set the total samples to loop through int ctSamplesTotal = (int)(SAMPLE_RATE * msecs / 1000);
if(msecs < 150) msecs = 150;
int ctSamplesTotal = (int)(SAMPLE_RATE * (msecs / 1000));
while(ctSamplesTotal > 0) { while(ctSamplesTotal > 0) {
// Define the frequency at the given sample rate
double fCycleInc = fFreq/SAMPLE_RATE; double fCycleInc = fFreq/SAMPLE_RATE;
// Clear the buffer
cBuf.clear(); cBuf.clear();
// Play the frequency
int ctSamplesThisPass = line.available() / SAMPLE_SIZE; int ctSamplesThisPass = line.available() / SAMPLE_SIZE;
for(int i = 0; i < ctSamplesThisPass; i++) { for(int i = 0; i < ctSamplesThisPass; i++) {
cBuf.putShort((short)(Short.MAX_VALUE * Math.sin(2 * Math.PI * fCyclePosition))); cBuf.putShort((short)(Short.MAX_VALUE * Math.sin(2 * Math.PI * fCyclePosition)));
@ -63,7 +56,6 @@ public class Generator {
} }
} }
// Close the audioline
line.drain(); line.drain();
line.close(); line.close();
} }