/** * Class that represents a sound. This class is used by the students * to extend the capabilities of SimpleSound. * * Copyright Georgia Institute of Technology 2004 * @author Barbara Ericson ericson@cc.gatech.edu */ public class Sound extends SimpleSound { /////////////// consructors //////////////////////////////////// /** * Constructor that takes a file name * @param fileName the name of the file to read the sound from */ public Sound(String fileName) { // let the parent class handle setting the file name super(fileName); } /** * Constructor that takes the number of seconds that this * sound will have * @param numSeconds the number of seconds desired */ public Sound (int numSeconds) { // let the parent class handle this super(numSeconds); } /** * Constructor that takes a sound to copy */ public Sound (Sound copySound) { // let the parent class handle this super(copySound); } ////////////////// methods //////////////////////////////////// /** * Method to return the string representation of this sound * @return a string with information about this sound */ public String toString() { String output = "Sound"; String fileName = getFileName(); // if there is a file name then add that to the output if (fileName != null) output = output + " file: " + fileName; // add the length in frames output = output + " number of samples: " + getLengthInFrames(); return output; } // ----- Methods for Session 14 ------------------------------ public void displaySamples( int count ) { SoundSample[] samples = this.getSamples(); for (int i = 0; i < count; i++ ) System.out.println( samples[i] ); } public void amplify( double increase ) { for (int i = 0; i < this.getLength(); i++ ) { int value = this.getSampleValueAt( i ); this.setSampleValueAt( i, (int) (value * (1 + increase)) ); } } // ----- Methods for Session 15 ------------------------------ public void amplify( int increase ) { for (int i = 0; i < this.getLength(); i++ ) { int value = this.getSampleValueAt( i ); this.setSampleValueAt( i, (int) (value + increase) ); } } public void amplifyNotShift( int increase ) { for (int i = 0; i < this.getLength(); i++ ) { int value = this.getSampleValueAt( i ); if (value > 0 ) this.setSampleValueAt( i, value + increase ); else if (value < 0 ) this.setSampleValueAt( i, value - increase ); } } public void invert() { for (int i = 0; i < this.getLength(); i++ ) { int value = this.getSampleValueAt( i ); this.setSampleValueAt( i, value * -1 ); } } public int maximumAbsoluteValue() { int largestSoFar = Math.abs( this.getSampleValueAt(0) ); for (int i = 1; i < this.getLength(); i++ ) { int value = Math.abs( this.getSampleValueAt(i) ); if ( value > largestSoFar ) { largestSoFar = value; } } return largestSoFar; } // public int maximumAbsoluteValue() // { // int largestSoFar = Math.abs( this.getSampleValueAt(0) ); // // for (int i = 1; i < this.getLength(); i++ ) // { // int value = Math.abs( this.getSampleValueAt(i) ); // if ( value > largestSoFar ) // { // System.out.println( "Index: " + i + " value: " + value ); // largestSoFar = value; // } // } // // return largestSoFar; // } public void normalize() { int maximumValue = this.maximumAbsoluteValue(); double multiplier = 32767.0 / maximumValue; for ( SoundSample sample : this.getSamples() ) sample.setValue( (int) (sample.getValue() * multiplier) ); } } // end of class Sound, put all new methods before this