/** * sort the sound samples in a sound object * * We will use the Selection Sort Algorithm * This algorthim, finds the smallest value and places * that value into the first position. * Then it finds (or selects) the second smallest value and * places that value into the second position * Repeats this process untill all values are in increasing order * * @author Pat Troy: troy AT uic DOT edu */ import java.util.*; import java.awt.*; import java.io.*; public class Lect1123a { public static void main(String[] args) { // Prompt the user for two sound files String fileName = FileChooser.pickAFile(); Sound s1 = new Sound (fileName); // calculate the length for the final, combined sound double s1seconds = s1.getLength() / s1.getSamplingRate() ; System.out.println ("S1 has a calculated length (in seconds) of: " + s1seconds); Sound s3 = sortSound (s1); System.out.println("Before Play"); //s3.play(); s3.explore(); System.out.println("After Play"); } public static Sound sortSound (Sound s) { // put the sound file into an array format SoundSample[] ssarray = s.getSamples(); int index; // loop for all value in the array for (index = 0; index < ssarray.length ; index = index + 1 ) { // find the position of the smallest value from position index to the // end of the array int smallPos = findSmallest (ssarray, index, ssarray.length - 1); // exchange the smallest value with the value in index swapValues (ssarray, index, smallPos); } return s; } public static int findSmallest (SoundSample[] ssarray, int startPos, int endPos) { int smallPos = startPos; int index; // loop through all values in the array and compare each value to the value // at smallPos for ( index = startPos + 1 ; index <= endPos ; index++) { if (ssarray[index].getValue() < ssarray[smallPos].getValue() ) smallPos = index; } return smallPos; } public static void swapValues (SoundSample[] ssarray, int index1, int index2) { SoundSample ss1, ss2; int samp1, samp2; // get the numeric value at the sample ss1 = ssarray[index1]; samp1 = ss1.getValue() ; //get the SoundSample where this value will be stored ss2 = ssarray[index2]; samp2 = ss2.getValue (); // exchange the values in samp1 and samp2 int temp; temp = samp1; samp1 = samp2; samp2 = temp; // store the two value back into the array ss1.setValue (samp1); ssarray[index1] = ss1; ss2.setValue (samp2); ssarray[index2] = ss2; } }