Click here for important information about

Sine Wave output file

November 6, 2010 at 19:29:26
Specs: Windows Vista
I am pretty new to programming in Java and really need help in a Sine Wave output file program. I am trying to make my program produce a .wav file from my program and it just gives me back console output. Here is my code:

import java.lang.*;
import java.lang.Math.*;
import javax.sound.sampled.*;

class oscillator {

public static void main(String[] args) {

int duration = 3000; // in milliseconds
int frequency = 442; // in Hertz
int samplerate = 8000;
int volumeLevel = 16000;

if (args.length != 1) {
System.err.println("usage: oscillator output.wav");

String filename = args[0];
// check that it has .wav extension
if (! (filename.toLowerCase().endsWith(".wav"))) {
System.err.println("must use a .wav file!");
File outfile = new File(filename);

int numSamples = (duration*samplerate)/1000;

short [] samples = new short[numSamples];

// now fill in samples[i] with values
int f;
float x = 0;
int i;
f = frequency / samplerate;
for (i=0; i< numSamples; i++) {
x = (int) Math.sin(x);
x = (float) ((float) x*(2*Math.PI*f*i));

x = (short) (x * volumeLevel);

WavReader.writeWav(samples, samplerate, outfile);


Can anyone help me with this, I could really use some help. I know the issue is somewhere in the last for loop but i am stumped. Any help would be greatly appreciated.

See More: Sine Wave output file

November 7, 2010 at 02:43:12
Thing e computer usually will not generate true sine waves. Depend upon the sound chip you will get an approximate.

Report •

November 7, 2010 at 08:04:03
I really do not understand your reply, again I am new to java so not sure how to proceed with your reply. Can you please clarify.

Report •

November 7, 2010 at 09:45:44
What's WavReader?

Report •

Related Solutions

November 8, 2010 at 14:29:19
WavReader is a separate class that produces a wav file. I got the code to produce a .wav file that has Asine sound on it but now I need to produce another .wav for a Square, Triangle, and Sawtooth Wave. I have this code:

int duration1 = 3000; // in milliseconds
double frequency1 = 442; // in Hertz
int samplerate1 = 8000;
int volumeLevel1 = 16000;

String filename1 = "Asquare.wav";
// check that it has .wav extension
if (! (filename1.toLowerCase().endsWith(".wav"))) {
System.err.println("must use a .wav file!");
File outfile1 = new File(filename1);

int numSamples1 = (duration1*samplerate1)/1000;

short [] samples1 = new short[numSamples1];

// now fill in samples[i] with values
double f1;
double x1;
int i1;
double t = 0;
f1 = frequency1 / samplerate1;
for (i1=0; i1< numSamples1; i1++) {

t = t - Math.floor(t);
if (t>0.5) {
t = t-1;
if (t>=0){
return +1; //Eclipse does not like this
else return -1; //Eclipse does not like this

samples1[i1] = (short) x1;


WavReader.writeWav(samples1, samplerate1, outfile1);

I am suppose to use this code to produce the file but it does not like the +1 and -1 after the return, can anyone help with this.

Report •

November 8, 2010 at 15:26:32
What's the method's return type? I assume it's void, since you've got a plain return near the top. So if it's void, how is it going to return an int?

Report •

Ask Question