How would i use the following java program so the oscillator will accommodate re
ID: 3768221 • Letter: H
Question
How would i use the following java program so the oscillator will accommodate request for a sine, square, sawtooth, or triangle. So when you click one of these, it will tell the oscillator which kind of wave to send.
Oscillator:
// Eric Diaz
// oscillate
// 27 Dec 2015
// ICT 362
// Java Programming
import javax.sound.sampled.*;
public class oscillate {
public static void main(String[] args){
oscillate e = new oscillate();
try { e.generate( 440,1,100); } catch (LineUnavailableException lue) { }
}
public oscillate() {
/*
try { generate( 440,1,100); } catch (LineUnavailableException lue) { }
try { generate( 880,2,100); } catch (LineUnavailableException lue) { }
try { generate( 440,3,100); } catch (LineUnavailableException lue) { }
try { generate(1200,1,10 ); } catch (LineUnavailableException lue) { }
*/
}
public void generate(int frequency, int duration, int amplitude ) throws LineUnavailableException {
int k = 0;
double rate = 44100.0;
int nSamples = 44100;
AudioFormat audioF;
double volume;
volume = 128 * loudness / 100.0;
audioF = new AudioFormat((float)rate,8,1,true,false);
byte[] buff = new byte[44190*duration];
nSamples = 44100 * duration;
int seconds = nSamples * duration;
double fudge = Math.PI * 2.0F;
double time = 0.0;
double Previous = 0.0;
int count=0;
double widthOfCycle = (double)1.0/(double)(1.0 * Hertz);
double widthOfSampling = (double)1.0 / rate;
double angle=0.0;
double x = 0.0;
// widthOfCycle is the wave time in seconds
// this is set up for 1 second of sound
for (int i=0; i<= nSamples ; i++) {
time = widthOfSampling * i;
// where in the sound cycle are we?
angle = fudge * time/widthOfCycle;
double q = Math.sin(angle);
double amp = q * volume;
int a = (int) amp;
// if (k++ > 440) {
// System.out.printf("%8.7f %8.4f %8.1f ",angle,q,amp);
// k=0;
// }
buff[i] = (byte)a;
}
SourceDataLine sourceDL = null;
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.printf("There are %d mixers ",mixerInfo.length);
/*
for (int i=0; i<mixerInfo.length; i++) {
System.out.printf("%d. %s ",i,mixerInfo[i].toString());
System.out.printf(" %s ",mixerInfo[i].getDescription());
System.out.printf(" %s ",mixerInfo[i].getName());
System.out.printf(" %s ",mixerInfo[i].getVendor());
System.out.printf(" %s ",mixerInfo[i].getVersion());
System.out.printf(" %04x ",mixerInfo[i].hashCode());
}
*/
sourceDL = AudioSystem.getSourceDataLine(audioF); // good
// sourceDL = AudioSystem.getSourceDataLine(); // good
System.out.printf("sourceDataLine = %s ",sourceDL);
sourceDL.open(audioF);
sourceDL.start();
sourceDL.write(buff,0,nSamples);
sourceDL.drain();
sourceDL.stop();
sourceDL.close();
}
}
Main Program:
// Eric Diaz
// SIGGEN
// 04 DEC 2015
// ICT 362
// Java Programming
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class box extends JFrame implements ActionListener {
// GLOBAL VARIABLES HERE OUTSIDE OF METHODS
JTextArea theOutput = null; // this is theOutput JTextArea
JTextField TF_Freq = 0.0; // input for Frequency
JTextField TF_Amp = 0.0; // input for Amplitude
JTextField TF_Dur = 0.0; // input for Duration
public void generate( int Frequency, int Duration, int Amplitude, String type) {
oscillate o = new oscillate ();
try { o.generate(Frequency, Amplitude, Duration, "Sine");}
catch(Exception LineNotAvailable) { }
}
public void actionPerformed(ActionEvent z) {
String theCommand = z.getActionCommand();
if (theCommand.equals("Quit")) { System.exit(0); }
if (theCommand.equals("Sine")) { theOutput.append("Sine was clicked "); }
if (theCommand.equals("Square")) { theOutput.append("Square was clicked "); }
if (theCommand.equals("Sawtooth")) { theOutput.append("Sawtooth was clicked "); }
if (theCommand.equals("Triangle")) { theOutput.append("Trianglewas clicked "); }
if (theCommand.equals("Generate")) { theOutput.append("Generate was clicked "); }
}
public static void main(String[] a) {
new box();
}
public box() {
JPanel JP1, JP2, JP3, JPTEMP;
JP1 = new JPanel();
JP2 = new JPanel();
JP3 = new JPanel();
JPTEMP = new JPanel();
JP1.setPreferredSize(new Dimension(900,60));
JP1.setBackground(new Color(255,0,0));
setupIO(JP2,JPTEMP);
JP2.setPreferredSize(new Dimension(900,200));
JP2.setBackground(new Color(0,255,0));
setupButtons(JP3);
JP3.setPreferredSize(new Dimension(900,50));
JP3.setBackground(new Color(255,250,255));
add(JP1,BorderLayout.NORTH);
add(JP2,BorderLayout.CENTER);
add(JP3,BorderLayout.SOUTH);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setTitle("DrMGMorrell Signal Generator");
setSize(1000,800);
setLocationRelativeTo(null);
setVisible(true);
}
public void setupButtons(JPanel a) {
JButton x=null;
x = new JButton("Sine");
x.addActionListener(this);
a.add(x);
x = new JButton("Square");
x.addActionListener(this);
a.add(x);
x = new JButton("Sawtooth");
x.addActionListener(this);
a.add(x);
x = new JButton("Triangle");
x.addActionListener(this);
a.add(x);
x = new JButton("Generate");
x.addActionListener(this);
a.add(x);
x = new JButton("Quit");
x.addActionListener(this);
a.add(x);
return;
}
public void setupIO(JPanel a,JPanel b) {
JLabel mm = null;
// set up 3 JTextFields, with JLabels and a JTextArea
// they are TFFreq, TFAmp, TFDur and theOutput
mm = new JLabel("Frequency (40-4000) Hz");
TF_Freq = new JTextField("1000");
TF_Freq.setPreferredSize(new Dimension(150,18));
b.add(mm,BorderLayout.WEST);
b.add(TF_Freq,BorderLayout.EAST);
a.add(b);
mm = new JLabel(" | Duration (.03 - 5) Seconds");
TF_Dur = new JTextField("1.0");
TF_Dur.setPreferredSize(new Dimension(150,18));
b.add(mm,BorderLayout.WEST);
b.add(TF_Dur,BorderLayout.EAST);
a.add(b);
mm = new JLabel(" | Amplitude (1 - 100) Level");
TF_Amp = new JTextField("99");
TF_Amp.setPreferredSize(new Dimension(150,18));
b.add(mm,BorderLayout.WEST);
b.add(TF_Amp,BorderLayout.EAST);
a.add(b);
theOutput = new JTextArea();
theOutput.setPreferredSize(new Dimension(600,600));
a.add(theOutput);
}
}
Explanation / Answer
Below program can generate Sin wave
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.