Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to generate a square wave signal, yet my program is generating a sine wave. What is wrong with the code below, and how

I'm trying to generate a square wave signal, yet my program is generating a sine wave. What is wrong with the code below, and how do I change it to simulate a square wave signal.?

code:

import java.util.Random; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.LineUnavailableException; import java.awt.*; import java.awt.geom.*; import javax.swing.*;

public class Noise extends JPanel{ static double[] sines; static int vol; public static void main(String[] args){ try { Noise.createTone(262, 100); } catch (LineUnavailableException lue) { System.out.println(lue); } //Frame object for drawing JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Noise()); frame.setSize(800,300); frame.setLocation(200,200); frame.setVisible(true); } public static void createTone(int Hertz, int volume) throws LineUnavailableException { float rate = 44100; byte[] buf; buf = new byte[1]; sines = new double[(int)rate]; vol=volume; AudioFormat audioF; audioF = new AudioFormat(rate,8,1,true,false); SourceDataLine sourceDL = AudioSystem.getSourceDataLine(audioF); sourceDL = AudioSystem.getSourceDataLine(audioF); sourceDL.open(audioF); sourceDL.start(); for(int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions