I want to record the audio file that is first played and recorded simultaneously creating the new audio file with the properties same as that of the audio file.
I want so because there are two audio files that are getting played with different properties like pitch, rates etc. I am setting the AudioFormat properties of the two audio files that will be created according to the 2 audios respectively using r.setdata() and then starting the recording but when I called r.start1() (where r is my Record class object) object control remains there and does not return to convertToAudio() function and play the clip, the mistake which I realized later on.
I wrote the following code :
My audio.java (part where I am starting the recording and then trying to play)
void convertToAudio(){
final File file1 = new File("E:/Jeena_Jeena_wapking1.wav");
final File file2 = new File("E:/Jeena_Jeena_wapking.wav");
final Record r = new Record();
String name = "E:/clips/Jeena";
String name1;
try {
AudioInputStream in1 = AudioSystem.getAudioInputStream(file1);
AudioInputStream in2 = AudioSystem.getAudioInputStream(file2);
AudioFormat inForm1 = in1.getFormat();
AudioFormat inForm2 = in2.getFormat();
rate1 = inForm1.getSampleRate();
rate2 = inForm2.getSampleRate();
ch1 = inForm1.getChannels();
ch2 = inForm2.getChannels();
DataLine.Info info1 = new DataLine.Info(Clip.class,inForm1);
DataLine.Info info2 = new DataLine.Info(Clip.class,inForm2);
Clip clip1 = (Clip)AudioSystem.getLine(info1);
Clip clip2 = (Clip)AudioSystem.getLine(info2);
long d = 0,s = 0;
int i;
clip1.open(in1);
long dt = clip1.getMicrosecondLength();
clip1.close();
System.out.println("Duration : " + (float)((dt / 1000000)/60.0) + "min");
for(i=0;i< 1 /*duration.length*/;i++){
if(d >= dt){
System.out.println("overflowed ");
break;
}
System.out.println("i" + i);
in1 = AudioSystem.getAudioInputStream(file1);
inForm1 = in1.getFormat();
info1 = new DataLine.Info(Clip.class,inForm1);
clip1 = (Clip)AudioSystem.getLine(info1);
clip1.addLineListener(this);
clip1.open(in1);
clip1.setMicrosecondPosition(d);
name1 = name + i;
s = duration[i];
r.setData(rate1, 16, ch1, true,inForm1.isBigEndian(),name1,s);
r.start1();
long pos = 0;
clip1.start();
while(true){
pos = clip1.getMicrosecondPosition();
System.out.println("pos 1: " + pos);
if( pos >= (d + duration[i])){
System.out.println("posFinal : " + pos);
clip1.stop();
break;
}
}
while (!playCompleted1) {
// wait for the playback completes
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
clip1.close();
playCompleted1 = false;
System.out.println("PLayback1 completed");
try {
Thread.sleep(8000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
d = d+duration[i];
in2 = AudioSystem.getAudioInputStream(file2);
inForm2 = in2.getFormat();
info2 = new DataLine.Info(Clip.class,inForm2);
clip2 = (Clip)AudioSystem.getLine(info2);
clip2.addLineListener(this);
clip2.open(in2);
clip2.setMicrosecondPosition(d);
name1 = name + i + i;
s = duration[i] + 2000000;
r.setData(rate2, 16, ch2, true,inForm2.isBigEndian(),name1,s);
d = d+2000000;
//pos = 0;
clip2.start();
while(true){
pos = clip2.getMicrosecondPosition();
System.out.println("pos 2: " + pos);
if( pos >= d){
System.out.println("posFinal : " + pos);
clip2.stop();
break;
}
}
while (!playCompleted1) {
// wait for the playback completes
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
clip2.close();
System.out.println("PLayback2 completed");
try {
Thread.sleep(8000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//d = pos + 2000000;
playCompleted1 = false;
// playCompleted2 = false;
}
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(LineEvent event) {
LineEvent.Type type = event.getType();
if (type == LineEvent.Type.START) {
System.out.println("Playback started.");
} else if (type == LineEvent.Type.STOP) {
playCompleted1 = true;
System.out.println("Playback completed.");
/* if(playCompleted1 == true){
playCompleted2 = true;
System.out.println("Playback2 completed.");
}*/
}
}
My record .java
import javax.sound.sampled.*;
import java.io.*;
/**
* A sample program is to demonstrate how to record sound in Java
* author: www.codejava.net
*/
public class Record {
// record duration, in milliseconds
public static long RECORD_TIME; // 1 minute
public Record recorder;
// path of the wav file
public static File wavFile;
// format of audio file
public float sampleRate;
public int sampleSizeInBits;
public int channels;
public boolean signed;
public boolean bigEndian;
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
// the line from which audio data is captured
TargetDataLine line;
/**
* Defines an audio format
*/
public void setData(float sr, int ssb, int ch, boolean s, boolean bE, String n, long rt){
sampleRate = sr;
sampleSizeInBits = ssb;
channels = ch;
signed = s;
bigEndian = bE;
// recorder = this;
RECORD_TIME = rt;
wavFile = new File(n);
}
AudioFormat getAudioFormat() {
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
channels, signed, bigEndian);
return format;
}
/**
* Captures the sound and record into a WAV file
*/
void start() {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start(); // start capturing
System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
System.out.println("Start recording...");
// start recording
AudioSystem.write(ais, fileType, wavFile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Closes the target data line to finish capturing and recording
*/
void finish() {
line.stop();
line.close();
System.out.println("Finished");
}
/**
* Entry to run the program
*/
public void start1() {
recorder = this;
// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
// start recording
recorder.start();
}
}
How can I do that? Is there a way by which we can create the recorded file with the properties of the audio file that is played apart from the way I am doing? The way I'm doing it is not working.
Aucun commentaire:
Enregistrer un commentaire