Compare commits

..

10 Commits

Author SHA1 Message Date
Nicolás A. Ortega
1e745b6d1c Forgot to change version. 2014-10-31 17:25:19 -05:00
Nicolás A. Ortega
e0720d56d0 Update README.md 2014-10-30 15:44:35 -05:00
Deathsbreed
eb392746ed Added comments to Generator code. 2014-10-22 11:49:38 -05:00
Deathsbreed
dc9840b6e5 Added license to About window. 2014-10-12 15:23:27 -05:00
Deathsbreed
69e9fa695c Fixed duration error. 2014-10-09 17:47:26 -05:00
Deathsbreed
7b23946e44 Added menu. 2014-10-09 15:28:46 -05:00
Deathsbreed
c0f48964ca Added a few things. 2014-10-08 21:55:13 -05:00
Deathsbreed
2ae030df92 Fixed warning. 2014-10-08 19:49:21 -05:00
Deathsbreed
dff0b993d3 Update build.xml 2014-10-08 19:45:40 -05:00
Deathsbreed
c56a664a2a Changed compilation instructions. 2014-10-08 19:43:35 -05:00
4 changed files with 133 additions and 41 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 jar $ ant
``` ```
### Contributing ### Contributing

View File

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

View File

@ -4,58 +4,138 @@ 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 {
public static final String version = "v0.2"; private static final String version = "v1.1.1";
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, 160); frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(); panel = new JPanel();
frame.add(panel); frame.add(panel);
panel.setLayout(null); setupMenu();
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 GPLv3 * @license GNU GPLv3
* @year 2014 * @year 2014
* *
*/ */
@ -21,6 +21,7 @@ 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);
@ -33,15 +34,21 @@ 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());
int ctSamplesTotal = (int)(SAMPLE_RATE * msecs / 1000); // Set the total samples to loop through
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)));
@ -56,6 +63,7 @@ public class Generator {
} }
} }
// Close the audioline
line.drain(); line.drain();
line.close(); line.close();
} }