Как записать сгенерированное аудио в формат .wav в java

Я очень новичок в Java Sound, и поэтому мои представления о нем не так сильны. Я хочу понять, как записать звук, сгенерированный следующим кодом, в файл формата .wav. Я прочитал несколько сообщений и несколько вопросов о stackoverflow, но я не могу понять из них решение. Поэтому, пожалуйста, помогите мне понять это. Спасибо.

import javax.sound.sampled.*;
import java.nio.ByteBuffer;
import java.util.Scanner;

public class Audio {           //Alternating Tones

public static void main(final String[] args) throws LineUnavailableException, InterruptedException {

    Scanner in = new Scanner(System.in);

    int alterTime = in.nextInt();       //For the time between frequencies
    int time = in.nextInt();            //Time specified by the user
    int freq1 = in.nextInt();           //Value of 1st Frequency specified by user
    int freq2 = in.nextInt();           //Value of 2nd Frequency specified by user


    final Clip clip1 = alternatingClip(freq1);          //For example 440
    final Clip clip2 = alternatingClip(freq2);          //For example 880


    //Adds a listener to this line. Whenever the line's status changes, the listener's update() method is called 
    //with a LineEvent object that describes the change.
    clip1.addLineListener(event -> {

        //getType() Obtains the event's type, which in this case should be equal to LineEvent.Type.STOP
        if (event.getType() == LineEvent.Type.STOP) {
            clip2.setFramePosition(0);      //Sets the media position
            clip2.loop(alterTime);
        }
    });

    //Adds a listener to this line. Whenever the line's status changes, the listener's update() method is called 
    //with a LineEvent object that describes the change.
    clip2.addLineListener(event -> {

        //getType() Obtains the event's type, which in this case should be equal to LineEvent.Type.STOP
        if (event.getType() == LineEvent.Type.STOP) {
            clip1.setFramePosition(0);      //Sets the media position
            clip1.loop(alterTime);
        }
    });


    clip1.loop(alterTime);

    Thread.sleep(1000*time);     // prevent JVM from exiting
}

public static Clip alternatingClip(final float frequency) throws LineUnavailableException {
    Clip clip = AudioSystem.getClip();
    final int SAMPLING_RATE = 44100;
    AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLING_RATE, 16, 1, 2, SAMPLING_RATE, true);

    final ByteBuffer buffer = ByteBuffer.allocate(SAMPLING_RATE * format.getFrameSize());
    final float cycleFraction = frequency / format.getFrameRate();
    double cyclePosition = 0;
    while (buffer.hasRemaining()) {         //Returns true only if there is at least one element remaining in the buffer
        buffer.putShort((short) (Short.MAX_VALUE * Math.sin(2 * Math.PI * cyclePosition)));
        cyclePosition += cycleFraction;
        if (cyclePosition > 1) {
            cyclePosition -= 1;
        }
    }
    // (AudioFormat, byte[] data, int offset, int bufferSize)
    clip.open(format, buffer.array(), 0, buffer.capacity());    
    return clip;
}
}

person Vibhav Chaddha    schedule 21.11.2016    source источник
comment
Я прочитал несколько сообщений и несколько вопросов по stackoverflow Предоставьте ссылки на них. ..но я не могу найти решение из них. Почему бы и нет? Нет большой мотивации помогать кому-то, если мы ожидаем, что вы вернетесь с Я не могу найти решение из этого - если вы не можете понять эти ответы, есть веская причина ожидать, что вы не поймете наши.   -  person Andrew Thompson    schedule 22.11.2016
comment
@AndrewThompson На самом деле, сообщения, которые я читал, несколько отличались от того, что я искал. Например, некоторые использовали «swing», «JFrame», «JButton» и т. д., и в настоящее время я не имею о них большого представления. Хотя с каждым постом я узнавал что-то новое, но я не мог решить свою проблему, и моя неопытность, возможно, была одной из причин этого. Я прикрепляю некоторые ссылки, которые я видел в моем следующем комментарии. И я никогда не хотел никого обидеть, я написал это просто от чистого невежества. Спасибо.   -  person Vibhav Chaddha    schedule 23.11.2016
comment
@AndrewThompson ссылка ссылка ссылка Спасибо.   -  person Vibhav Chaddha    schedule 23.11.2016