Now I'm trying to put the sample wav file in to byte[] and then export the byte[] back to the wav file again.
I succeed doing that but when I try to concat the byte[] to make the wav file longer, the result is just the same one before the concat.
I've tried with 1 kick drum input (2 seconds) and I try to get 2 kick drum as a result but the result is still 1 kick drum ,2 seconds.
Here's main that mix & convert byte[] to wav
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.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import Samples.Percussion;
public class Main {
public static Percussion snareWet;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Instrument Type");
String type = sc.nextLine();
try {
snareWet = new Percussion(type);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("BPM");
String bpm = sc.nextLine();
System.out.println("Instrument : "+type+" BPM : " + bpm);
byte[] song;
song = concat(snareWet.getAudioBytes(),snareWet.getAudioBytes());
AudioInputStream oAIS = null;
ByteArrayInputStream oInstream = new ByteArrayInputStream(song);
try {
oAIS = AudioSystem.getAudioInputStream(oInstream);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File out = new File("result/testDouble.wav");
AudioSystem.write(oAIS,AudioFileFormat.Type.WAVE,out);
}
public static byte[] concat(byte[] a, byte[] b) {
int aLen = a.length;
int bLen = b.length;
byte[] c= new byte[aLen+bLen];
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);
return c;
}
}
And here's the class that convert wav to byte[]
package Samples;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Percussion {
private byte[] audioBytes = null;
public Percussion(String type) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in;
try {
in = new BufferedInputStream(new FileInputStream("samples/"+type+".wav"));
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("ERROR :: NO INSTRUMENT FOUND");
e.printStackTrace();
return;
}
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
out.flush();
audioBytes = out.toByteArray();
System.out.println(audioBytes.length);
/*for(int i = 0 ; i< audioBytes.length ; i++){
System.out.println(audioBytes[i]);
}*/
}
public byte[] getAudioBytes() {
return audioBytes;
}
public void setAudioBytes(byte[] audioBytes) {
this.audioBytes = audioBytes;
}
}