1
首先,感謝大家幫助我解決以前的問題。如何在不使用java中的「剪輯」之前循環播放聲音
在下面的代碼中,我選擇兩個頻率並將它們寫入.wav格式,以在用戶給定的特定時間在Windows Media Player中運行它。 我想要的是瞭解如何循環這些頻率在指定時間內交替運行,如救護車的警笛聲,並且在我的程序中,兩個頻率都只播放一次。例如,如果我將時間指定爲10秒,那麼兩個頻率一次運行5秒。但我想要的是第一個頻率運行一兩秒鐘(如用戶指定),然後是第二個頻率運行相似的秒,然後再運行第一個頻率,它應該繼續運行直到指定的時間。
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
public class AudioWrite2New {
public static void main(String[] args) throws IOException, InterruptedException, LineUnavailableException {
Scanner in = new Scanner(System.in);
final double SAMPLING_RATE = 44100; // Audio sampling rate
int time = in.nextInt(); //Time specified by user in seconds
// int time2 = in.nextByte();
int frequency1 = in.nextInt(); //Frequency1 specified by the user in hz
int frequency2 = in.nextInt(); //Frequency2 specified by the user in hz
float buffer[] = new float[(int) (time/2 * SAMPLING_RATE)]; //Size of buffer[], which in case of 10 seconds is 441000
float buffer1[] = new float[(int) (time/2 * SAMPLING_RATE)]; //Size of buffer1[], which in case of 10 seconds is 441000
for (int sample = 0; sample < buffer.length; sample++) {
double cycle = sample/SAMPLING_RATE; //Fraction of cycle between samples
buffer[sample] = (float) (Math.sin(2 * Math.PI * frequency1 * cycle));
//buffer1[sample] = (float) (Math.sin(2 * Math.PI * frequency2 * cycle));
}
for (int sample = 0; sample < buffer1.length; sample++) {
double cycle = sample/SAMPLING_RATE; //Fraction of cycle between samples
//buffer[sample] = (float) (Math.sin(2 * Math.PI * frequency1 * cycle));
buffer1[sample] = (float) (Math.sin(2 * Math.PI * frequency2 * cycle));
}
//System.out.println(buffer[1]);
byte byteBuffer[] = new byte[buffer.length * 2]; //Size of byteBuffer, in this case 882000
byte byteBuffer1[] = new byte[buffer1.length * 2]; //Size of byteBuffer, in this case 882000
int count = 0;
for (int i = 0; i < byteBuffer.length; i++) {
final int x = (int) (buffer[count++] * Short.MAX_VALUE);
byteBuffer[i++] = (byte) x;
byteBuffer[i] = (byte) (x/256);
}
count = 0;
for (int i = 0; i < byteBuffer1.length; i++) {
final int x = (int) (buffer1[count++] * Short.MAX_VALUE);
byteBuffer1[i++] = (byte) x;
byteBuffer1[i] = (byte) (x/256);
}
//For merging the two frequencies
byte[] merge = new byte[byteBuffer.length + byteBuffer1.length];
System.arraycopy(byteBuffer, 0, merge, 0, byteBuffer.length);
System.arraycopy(byteBuffer1, 0, merge, byteBuffer.length, byteBuffer1.length);
File out = new File("E:/RecordAudio17.wav"); //The path where user want the file data to be written
//Construct an audio format, using 44100hz sampling rate, 16 bit samples, mono, and big
// endian byte ordering
AudioFormat format = new AudioFormat((float) SAMPLING_RATE, 16, 1, true, false);
// It uses bytebuffer as its buffer array that contains bytes that may be read from the stream.
ByteArrayInputStream bais = new ByteArrayInputStream(merge);
//Constructs an audio input stream that has the requested format and length in sample frames, using audio data
//from the specified input stream.
AudioInputStream audioInputStream = new AudioInputStream(bais, format, buffer1.length + buffer.length);
//Writes a stream of bytes representing an audio file of the specified file type to the external file provided.
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, out);
audioInputStream.close(); //Closes this audio input stream
}
}
正如我在Java和JavaSounds挺新的,所以有時我會問一些愚蠢的或不相關的問題。所以請耐心等待,因爲這是我學習的唯一途徑。 謝謝。
你知道所有這些複雜的東西,你不知道如何把一個聲明和一個共同聲明 - 這意味着你不知道編程 - 指的這些人,幫助你http://stackoverflow.com/questions/ 40586715/play-2-different-frequencies-alternatives-in-java – gpasch
@gpasch在發佈我的問題之前,我試圖解決它,但我沒有得到我想要的輸出。有時它不罷工。是的,我在編程方面很新穎,所以也許我不擅長它。但我不是一個枯燥的人,我最終解決了這個問題。 無論如何感謝。 – Learner