/** * 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; } /** * Method to turn a sound into a standard, common format mono 22.05kHZ 16-bit wav * @return a standardized sound */ public Sound standardize() { double time = getLength() / getSamplingRate(); int newLength = (int)(time * SAMPLE_RATE); Sound newSound = new Sound(newLength); double rateRatio = getSamplingRate() / SAMPLE_RATE; int ampFactor = 1; if(!isStereo() && getLengthInBytes() / getLength() == 1 ) //8-bit mono ampFactor = 256; if(isStereo() && getLengthInBytes() / getLength() == 2 ) //8-bit stereo ampFactor = 256; int i = 0; while (i < newLength) { int oldIndex = (int)(i * rateRatio); newSound.setSampleValueAt(i,getSampleValueAt(oldIndex)*ampFactor); i = i + 1; } return newSound; } /** * Method to create a new sound by copying just part of * the current sound to a new sound * @param start the index to start the copy at (inclusive) * @param end the index to stop the copy at (inclusive) * @return a new sound with just the samples from start to * end in it */ public Sound clip(int start, int end) { // calculate the number of samples in the clip int lengthInSamples = end - start + 1; Sound target = new Sound(lengthInSamples); // hold clip int value = 0; // holds the current sample value int targetIndex = 0; // index in target sound // copy from start to end from source into target int i = start; while (i <= end) for (int i = start; i <= end; ) { value = this.getSampleValueAt(i); target.setSampleValueAt(targetIndex,value); i = i + 1; targetIndex = targetIndex + 1; } return target; } /** * Method to double the volume (amplitude) of the sound */ public void increaseVolume() { SoundSample[] sampleArray = this.getSamples(); SoundSample sample = null; int value = 0; int index = 0; // loop through all the samples in the array while (index < sampleArray.length) { sample = sampleArray[index]; value = sample.getValue(); sample.setValue(value * 2); index++; } } /** * Method to halve the volume (amplitude) of the sound. */ public void decreaseVolume() { } /** * Method to change the volume (amplitude) of the sound * by multiplying the current values in the sound by * the passed factor. * @param factor the factor to multiply by */ public void changeVolume(double factor) { } /** * Method to create a new sound by appending another Sound to * the current sound. Assume that both sounds have the same * smapling rate * @param other the Sound that should be added to the end of this sound * to create a new one * @return a new sound which is the concatenation of the Sound passed in * and the original Sound */ public Sound append(Sound other) { // TODO: fill in append } } // end of class Sound, put all new methods before this