3 methods needs doing;
wave1: This static method takes an AudioInputStream and gradually lowers and then raises the volume so that the new AudioInputStream appears to be constantly alerting in volume.
wave2: This static method takes an AudioInputStream and alters it so that it seems to move from left to right. The resultant new audio stream is then returned.
down: This static method takes a 16 bit AudioInputStream and returns an AudiInputStream converted to an 8 bit AudioInputStream.
====================================
import javax.sound.sampled.*;
import java.io.*;
public class AudioManipulation {
/**
* Creating an echo.
* Out of a given AudioInputSteam a new one is created with an additional echo effect.
*
* @param ais the original sound
* @param delay the delay of the echo in mili seconds.
* @param fading the strength of the echo
*/
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) {
data[i]=a[2*i]*256+a[2*i+1];
}
// 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);
}
// 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)
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) (Math.round(data[i]/256.0));
a[2*i+1]=(byte) (data[i]-(a[2*i]*256));
}
} 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());
}
/**
* Creates a wave pattern out of a given sound.
* A new AudioInputStream is created with an additional wave effect.
* In the new sound the volume goes up and down in fixes intervals.
* If i is the length of the interval, then the amplitude of a sound
* at position x will be modified by a factor
* abs( 2 * ((x % i) / i -0.5))
*
* @param ais the original sound
* @param interval the length of the interval
*/
public static AudioInputStream wave1(AudioInputStream ais, int interval) {
// add your code here and change the return statement
return ais;
}
/**
* Creates a wave pattern out of a given sound.
* A new AudioInputStream is created with an additional wave effect.
* In the new sound the volume of the two channels goes up and down,
* which creates the effect that sound moves from the left to right
* and back.
* If i is the length of the interval, then the amplitude for the left channel
* at position x will be modified by a factor
* abs(2*((x % i) / i-0.5))
* and for the right channel
* abs(2*(((x+i/2) % i)/ i -0.5))
*
* @param ais the original sound
* @param interval the length of the interval
*/
public static AudioInputStream wave2(AudioInputStream ais,
int interval) {
// add your code here and change the return statement
return ais;
}
/**
* Convert a given sound from 16 bit to 8bit.
* A new AudioInputStream is created representing the originial sound in 8 bit.
*
* @param ais the original sound
*/
public static AudioInputStream down(AudioInputStream ais) {
// add your code here and change the return statement
return ais;
}
}