/** * Class for creating a template for a simple Java program * * @author Pat Troy: troy AT uic DOT edu */ import java.awt.Color; // Note the name of the class in the following line MUST // match the name of the file. public class Lect48b { public static void main (String[] args) { String fname = FileChooser.pickAFile(); //System.out.println (fname); Sound s = new Sound (fname); System.out.println ("Number of samples in the sound: " + s.getLength()); System.out.println ("Number of samples per second: " + s.getSamplingRate()); double lengthInSeconds = s.getLength() / s.getSamplingRate(); System.out.println ("The length in seconds of the sound: " + lengthInSeconds); increaseVolume (s); fadeIn (s, 4); s.explore(); } // fade in over a 5 second period // fade in => gradually increase the volume of the sound from // 0 volume to regular volume. public static void fadeIn (Sound s) { SoundSample sampArr[] = s.getSamples(); int index; int sampVal; double changeAmount; int numOfSeconds = 5; int sampleRate; int totalSamples; double deltaAmount; // determine the number of samples needed to manipulate sampleRate = (int)s.getSamplingRate(); totalSamples = sampleRate * numOfSeconds; if (totalSamples > sampArr.length) totalSamples = sampArr.length; // determine the amount of volume change at each second deltaAmount = 1.0 / totalSamples; changeAmount = 10; System.out.println ("Number of samples in the sound: " + sampArr.length); for ( index = 0 ; index < totalSamples ; index++ ) { sampVal = sampArr[index].getValue(); changeAmount = deltaAmount * index; sampVal = (int) (sampVal * changeAmount); if (sampVal > 32767) sampVal = 32767; if (sampVal < -32768) sampVal = -32768; sampArr[index].setValue(sampVal); } } // fade in over the period of seconds indicated by the second parameter // fade in => gradually increase the volume of the sound from // 0 volume to regular volume. public static void fadeIn (Sound s, int numOfSeconds) { SoundSample sampArr[] = s.getSamples(); int index; int sampVal; double changeAmount; //int numOfSeconds = 5; int sampleRate; int totalSamples; double deltaAmount; // determine the number of samples needed to manipulate sampleRate = (int)s.getSamplingRate(); totalSamples = sampleRate * numOfSeconds; if (totalSamples > sampArr.length) totalSamples = sampArr.length; // determine the amount of volume change at each second deltaAmount = 1.0 / totalSamples; changeAmount = 10; System.out.println ("Number of samples in the sound: " + sampArr.length); for ( index = 0 ; index < totalSamples ; index++ ) { sampVal = sampArr[index].getValue(); changeAmount = deltaAmount * index; sampVal = (int) (sampVal * changeAmount); if (sampVal > 32767) sampVal = 32767; if (sampVal < -32768) sampVal = -32768; sampArr[index].setValue(sampVal); } } public static void increaseVolume (Sound s) { SoundSample sampArr[] = s.getSamples(); int index; int sampVal; System.out.println ("Number of samples in the sound: " + sampArr.length); for ( index = 0 ; index < sampArr.length ; index++ ) { sampVal = sampArr[index].getValue(); sampVal = sampVal * 10; if (sampVal > 32767) sampVal = 32767; if (sampVal < -32768) sampVal = -32768; sampArr[index].setValue(sampVal); } } } // end of class