/** * 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 Lect48d { 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); Sound s3; s3 = reverseSound (s); //increaseVolume (s3); s3.explore(); } public static Sound reverseSound (Sound s1) { int lengthOfS3; lengthOfS3 = s1.getLength(); Sound s3 = new Sound (lengthOfS3); System.out.println ("Number of samples in the sound: " + s3.getLength()); SoundSample sampArr1[] = s1.getSamples(); SoundSample sampArr3[] = s3.getSamples(); int index; int index3; int sampVal; System.out.println ("Number of samples in the sound: " + sampArr3.length); for ( index = 0 ; index < lengthOfS3 ; index++ ) { sampVal = sampArr1[index].getValue(); if (sampVal > 32767) sampVal = 32767; if (sampVal < -32768) sampVal = -32768; index3 = lengthOfS3 - 1 - index; sampArr3[index3].setValue(sampVal); } return s3; } // 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