Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 15th, 2010 | Syntax: None | Size: 4.82 KB | Hits: 25 | Expires: Never
Copy text to clipboard
  1. package AudioManipulation;
  2.  
  3. /**
  4.  * AudioManipulation.java
  5.  *
  6.  *
  7.  * Created: Sat Jan 21 12:56:16 2006
  8.  *
  9.  * @author <a href="mailto:">Dilbag S</a>
  10.  * @version 1.0
  11.  */
  12. import javax.sound.sampled.*;
  13. import java.io.*;
  14. public class AudioManipulation {
  15.  
  16.         public static AudioInputStream echo(AudioInputStream ais, int delay, double fading) {
  17.  
  18.                 byte[] a=null;
  19.                 int[] data;
  20.                 int max;
  21.  
  22.                 try{
  23.                         delay=(int) (delay*ais.getFormat().getFrameSize()*
  24.                                         ais.getFormat().getFrameRate()/1000);
  25.  
  26.                         // reset the AudioInputStream
  27.                         ais.reset();
  28.  
  29.                         // create a byte array of the right size
  30.                         a=new byte[(int) ais.getFrameLength() *
  31.                                    ais.getFormat().getFrameSize()];
  32.  
  33.                         // fill the byte array with the data of the AudioInputStream
  34.                         ais.read(a);       
  35.  
  36.                         // Create an integer array of the right size
  37.                         data=new int[a.length/2];
  38.  
  39.                         // fill the integer array by combining two bytes of the
  40.                         // byteArray to one integer
  41.                         for (int i=0;i<data.length;++i) {
  42.                                 /* First byte is MSB (high order) */
  43.                                 int MSB = (int) a[2*i];
  44.                                 /* Second byte is LSB (low order) */
  45.                                 int LSB = (int) a[2*i+1];
  46.                                 data[i] = MSB << 8 | (255 & LSB);
  47.                         }
  48.  
  49.  
  50.                         // Adding a faded copy of the signal to the later measurements
  51.                         for (int i=0; i<data.length-delay;++i) {
  52.                                 data[i+delay] += (int) (data[i]*fading);
  53.                                 //              data[i] = (int) (data[i]*0.666);
  54.                         }
  55.  
  56.  
  57.                         // get the maxium amplitute
  58.                         max=0;
  59.                         for (int i=0;i<data.length;++i) {
  60.                                 max=Math.max(max,Math.abs(data[i]));
  61.                         }
  62.  
  63.                         // scale the maximum amplitude down to 256*128
  64.                         if (max > 256*128)
  65.                                 System.out.println("help");
  66.                         for (int i=0; i<data.length;++i) {
  67.                                 data[i] = (int) (data[i]*(256.0*256.0/2-1)/max);
  68.                         }
  69.  
  70.                         // convert the integer array to a byte array
  71.                         for (int i=0;i<data.length;++i) {
  72.                                 a[2*i] = (byte) ((int) ((data[i] >> 8) & 255));
  73.                                 a[2*i+1] = (byte) ( (int) (data[i] & 255));
  74.  
  75.  
  76.                         }
  77.  
  78.                 } catch(Exception e){
  79.                         System.out.println("Something went wrong");
  80.                         e.printStackTrace();
  81.  
  82.                 }
  83.                 //new byteArray for the AudioInputStream including the return method.
  84.                 return new AudioInputStream(new ByteArrayInputStream(a),
  85.                                 ais.getFormat(),ais.getFrameLength());
  86.         }
  87.  
  88.         public static AudioInputStream effect1(AudioInputStream ais){
  89.  
  90.                 byte[] r = null;
  91.                 //echo effect code is applied to one channel only
  92.                 try
  93.                 {
  94.                         ais.reset();
  95.  
  96.                         //creating a byteArray for the InputStream
  97.                         byte[] a = new byte[(int)ais.getFrameLength()*ais.getFormat().getFrameSize()];
  98.  
  99.                         ais.read(a);
  100.  
  101.                         r = new byte[a.length/2];
  102.  
  103.                         //adding only the every first bytes of to the array
  104.  
  105.                         for (int i = 0; i < a.length; i++)
  106.                                 if ((i%2)==0){
  107.                                         r[i/2]=a[i];
  108.                                 }
  109.                 }
  110.                 catch (IOException e)
  111.                 {
  112.                         e.printStackTrace();
  113.                 }
  114.  
  115.                 //create a new input stream from the byte array
  116.                 return new AudioInputStream(new ByteArrayInputStream(r), ais.getFormat(), ais.getFrameLength() / 2);//return new stream
  117.         }
  118.  
  119.         public static AudioInputStream effect2(AudioInputStream ais,
  120.                         int delay, double fading) {
  121.                 byte[] a=null;
  122.                 int[] data;
  123.                 int max;
  124.  
  125.                 try{
  126.                         delay=(int) (delay*ais.getFormat().getFrameSize()*
  127.                                         ais.getFormat().getFrameRate()/1000);
  128.  
  129.                         // reset the AudioInputStream
  130.                         ais.reset();
  131.  
  132.                         // create a byte array of the right size
  133.                         a=new byte[(int) ais.getFrameLength() *
  134.                                    ais.getFormat().getFrameSize()];
  135.  
  136.                         // fill the byte array with the data of the AudioInputStream
  137.                         ais.read(a);       
  138.  
  139.                         // Create an integer array of the right size
  140.                         data=new int[a.length/2];
  141.  
  142.                         // fill the integer array by combining two bytes of the
  143.                         // byteArray to one integer
  144.                         for (int i=0;i<data.length;++i) {
  145.                                 /* First byte is MSB (high order) */
  146.                                 int MSB = (int) a[2*i];
  147.                                 /* Second byte is LSB (low order) */
  148.                                 int LSB = (int) a[2*i+1];
  149.                                 data[i] = MSB << 8 | (255 & LSB);
  150.                         }
  151.  
  152.  
  153.                         // Adding a faded copy of the signal to the later measurements
  154.                         for (int i=0; i<data.length-delay;++i) {
  155.                                 data[i+delay] += (int) (data[i]*fading);
  156.                                 //effect 2 code
  157.                                 i++;
  158.                         }
  159.  
  160.  
  161.                         // get the maxium amplitute
  162.                         max=0;
  163.                         for (int i=0;i<data.length;++i) {
  164.                                 max=Math.max(max,Math.abs(data[i]));
  165.                         }
  166.  
  167.                         // scale the maximum amplitude down to 256*128
  168.                         if (max > 256*128)
  169.                                 System.out.println("help");
  170.                         for (int i=0; i<data.length;++i) {
  171.                                 data[i] = (int) (data[i]*(256.0*256.0/2-1)/max);
  172.                         }
  173.  
  174.                         // convert the integer array to a byte array
  175.                         for (int i=0;i<data.length;++i) {
  176.                                 a[2*i] = (byte) ((int) ((data[i] >> 8) & 255));                    
  177.  
  178.                         }
  179.  
  180.                 } catch(Exception e){
  181.                         System.out.println("Something went wrong");
  182.                         e.printStackTrace();
  183.  
  184.                 }
  185.  
  186.                 // create a new AudioInputStream out of the the byteArray
  187.                 // and return it.
  188.                 return new AudioInputStream(new ByteArrayInputStream(a),
  189.                                 ais.getFormat(),ais.getFrameLength());
  190.         }
  191.  
  192.  
  193.  
  194. } // AudioManipulation