Is there any example Java program, that creates sound, like using Math.sin or something to create sound if I pass f1 and f2 (frequency) parameters of formant?
Asked
Active
Viewed 241 times
-2
-
yes, i googled , but still cant understand how to ... – Anab Khan Nov 25 '16 at 15:59
-
take a look here: http://stackoverflow.com/questions/1932490/java-generating-sound – aleb2000 Nov 25 '16 at 15:59
-
i want to know if if i pass two frequencies f1 and f2 of formant, then how the sound will be generated – Anab Khan Nov 25 '16 at 16:04
-
In the link of the answer I passed you there is an example of generating a sound. – aleb2000 Nov 25 '16 at 16:08
1 Answers
-1
Taken from here.
/** Generates a tone.
@param hz Base frequency (neglecting harmonic) of the tone in cycles per second
@param msecs The number of milliseconds to play the tone.
@param volume Volume, form 0 (mute) to 100 (max).
@param addHarmonic Whether to add an harmonic, one octave up. */
public static void generateTone(int hz,int msecs, int volume, boolean addHarmonic) throws LineUnavailableException {
float frequency = 44100;
byte[] buf;
AudioFormat af;
if (addHarmonic) {
buf = new byte[2];
af = new AudioFormat(frequency,8,2,true,false);
} else {
buf = new byte[1];
af = new AudioFormat(frequency,8,1,true,false);
}
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for(int i=0; i<msecs*frequency/1000; i++){
double angle = i/(frequency/hz)*2.0*Math.PI;
buf[0]=(byte)(Math.sin(angle)*volume);
if(addHarmonic) {
double angle2 = (i)/(frequency/hz)*2.0*Math.PI;
buf[1]=(byte)(Math.sin(2*angle2)*volume*0.6);
sdl.write(buf,0,2);
} else {
sdl.write(buf,0,1);
}
}
sdl.drain();
sdl.stop();
sdl.close();
}

aleb2000
- 452
- 4
- 10
-
may be this would help ,if i would be able understand this line `Math.sin(2*angle2)*volume*0.6` ? – Anab Khan Nov 25 '16 at 18:17