- package AudioManipulation;
- /**
- * AudioManipulation.java
- *
- *
- * Created: Sat Jan 21 12:56:16 2006
- *
- * @author <a href="mailto:">Dilbag S</a>
- * @version 1.0
- */
- import javax.sound.sampled.*;
- import java.io.*;
- public class AudioManipulation {
- public static AudioInputStream echo(AudioInputStream ais, int delay, double fading) {
- byte[] a=null;
- int[] data;
- int max;
- try{
- delay=(int) (delay*ais.getFormat().getFrameSize()*
- ais.getFormat().getFrameRate()/1000);
- // reset the AudioInputStream
- ais.reset();
- // create a byte array of the right size
- a=new byte[(int) ais.getFrameLength() *
- ais.getFormat().getFrameSize()];
- // fill the byte array with the data of the AudioInputStream
- ais.read(a);
- // Create an integer array of the right size
- data=new int[a.length/2];
- // fill the integer array by combining two bytes of the
- // byteArray to one integer
- for (int i=0;i<data.length;++i) {
- /* First byte is MSB (high order) */
- int MSB = (int) a[2*i];
- /* Second byte is LSB (low order) */
- int LSB = (int) a[2*i+1];
- data[i] = MSB << 8 | (255 & LSB);
- }
- // Adding a faded copy of the signal to the later measurements
- for (int i=0; i<data.length-delay;++i) {
- data[i+delay] += (int) (data[i]*fading);
- // data[i] = (int) (data[i]*0.666);
- }
- // get the maxium amplitute
- max=0;
- for (int i=0;i<data.length;++i) {
- max=Math.max(max,Math.abs(data[i]));
- }
- // scale the maximum amplitude down to 256*128
- if (max > 256*128)
- System.out.println("help");
- for (int i=0; i<data.length;++i) {
- data[i] = (int) (data[i]*(256.0*256.0/2-1)/max);
- }
- // convert the integer array to a byte array
- for (int i=0;i<data.length;++i) {
- a[2*i] = (byte) ((int) ((data[i] >> 8) & 255));
- a[2*i+1] = (byte) ( (int) (data[i] & 255));
- }
- } catch(Exception e){
- System.out.println("Something went wrong");
- e.printStackTrace();
- }
- //new byteArray for the AudioInputStream including the return method.
- return new AudioInputStream(new ByteArrayInputStream(a),
- ais.getFormat(),ais.getFrameLength());
- }
- public static AudioInputStream effect1(AudioInputStream ais){
- byte[] r = null;
- //echo effect code is applied to one channel only
- try
- {
- ais.reset();
- //creating a byteArray for the InputStream
- byte[] a = new byte[(int)ais.getFrameLength()*ais.getFormat().getFrameSize()];
- ais.read(a);
- r = new byte[a.length/2];
- //adding only the every first bytes of to the array
- for (int i = 0; i < a.length; i++)
- if ((i%2)==0){
- r[i/2]=a[i];
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- //create a new input stream from the byte array
- return new AudioInputStream(new ByteArrayInputStream(r), ais.getFormat(), ais.getFrameLength() / 2);//return new stream
- }
- public static AudioInputStream effect2(AudioInputStream ais,
- int delay, double fading) {
- byte[] a=null;
- int[] data;
- int max;
- try{
- delay=(int) (delay*ais.getFormat().getFrameSize()*
- ais.getFormat().getFrameRate()/1000);
- // reset the AudioInputStream
- ais.reset();
- // create a byte array of the right size
- a=new byte[(int) ais.getFrameLength() *
- ais.getFormat().getFrameSize()];
- // fill the byte array with the data of the AudioInputStream
- ais.read(a);
- // Create an integer array of the right size
- data=new int[a.length/2];
- // fill the integer array by combining two bytes of the
- // byteArray to one integer
- for (int i=0;i<data.length;++i) {
- /* First byte is MSB (high order) */
- int MSB = (int) a[2*i];
- /* Second byte is LSB (low order) */
- int LSB = (int) a[2*i+1];
- data[i] = MSB << 8 | (255 & LSB);
- }
- // Adding a faded copy of the signal to the later measurements
- for (int i=0; i<data.length-delay;++i) {
- data[i+delay] += (int) (data[i]*fading);
- //effect 2 code
- i++;
- }
- // get the maxium amplitute
- max=0;
- for (int i=0;i<data.length;++i) {
- max=Math.max(max,Math.abs(data[i]));
- }
- // scale the maximum amplitude down to 256*128
- if (max > 256*128)
- System.out.println("help");
- for (int i=0; i<data.length;++i) {
- data[i] = (int) (data[i]*(256.0*256.0/2-1)/max);
- }
- // convert the integer array to a byte array
- for (int i=0;i<data.length;++i) {
- a[2*i] = (byte) ((int) ((data[i] >> 8) & 255));
- }
- } catch(Exception e){
- System.out.println("Something went wrong");
- e.printStackTrace();
- }
- // create a new AudioInputStream out of the the byteArray
- // and return it.
- return new AudioInputStream(new ByteArrayInputStream(a),
- ais.getFormat(),ais.getFrameLength());
- }
- } // AudioManipulation
