2

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;
}

}
  • Is your audio stream data in a format that will tolerate the concatenation? If it contains length information the stream parser may stop after reading the number of bytes of data the stream 'says' it contains. – Jim Garrison Oct 02 '16 at 06:02
  • @JimGarrison I've check the length of both snareWet.getAuditoBytes and song. Song's length is exactly 2 times of snareWet.get... And I've randomly access both array if the content is the same, and the result is the same. I also try join the wav directly like [http://stackoverflow.com/questions/653861/join-two-wav-files-from-java] but the result stil the same one with before :( – Panida Wiriyachaiporn Oct 02 '16 at 06:13
  • It is possible the stream format contains markers that tell the audio stream parser either how long the stream is, or mark the end of the stream internally. If that is the case, then just concatenating the stream twice won't work, you'd have to modify the internal markers. I don't know enough about audio streams to tell you if that applies here, you'll need to research the stream format. – Jim Garrison Oct 02 '16 at 06:17

0 Answers0