/** * sort the sound samples in a sound object * * We will use the QuickSort Algorithm * * @author Pat Troy: troy AT uic DOT edu */ import java.util.*; import java.awt.*; import java.io.*; public class Lect1123b { 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); quicksort (s1); System.out.println("Before Play"); //s3.play(); s1.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; } /*********************************************************************** * Quicksort code from Sedgewick 7.1, 7.2. ***********************************************************************/ public static void quicksort(Sound a) { shuffle(a); // to guard against worst-case quicksort(a, 0, a.getLength() - 1); } // quicksort a[left] to a[right] public static void quicksort(Sound a, int left, int right) { if (right <= left) return; int i = partition(a, left, right); quicksort(a, left, i-1); quicksort(a, i+1, right); } // partition a[left] to a[right], assumes left < right private static int partition(Sound a, int left, int right) { int i = left - 1; int j = right; while (true) { while (less(a.getSampleValueAt(++i), a.getSampleValueAt(right))) // find item on left to swap ; // a[right] acts as sentinel while (less(a.getSampleValueAt(right), a.getSampleValueAt(--j))) // find item on right to swap if (j == left) break; // don't go out-of-bounds if (i >= j) break; // check if pointers cross exchangeValues(a, i, j); // swap two elements into place } exchangeValues(a, i, right); // swap with partition element return i; } // is x < y ? private static boolean less(int x, int y) { //comparisons++; return (x < y); } // exchange two values in a sound specified by index positions public static void exchangeValues (Sound s, int index1, int index2) { int temp; // error checking to validate parameters if (( index1 < 0 ) || ( index2 < 0 ) || ( index1 >= s.getLength() ) || ( index2 >= s.getLength() )) { System.out.println ("Error in exchangeValue(): invalid parameters"); return; } // exchange the sound values temp = s.getSampleValueAt (index1); s.setSampleValueAt (index1, s.getSampleValueAt(index2) ); s.setSampleValueAt (index2, temp); } // exchange a[i] and a[j] private static void exch(Sound a, int i, int j) { //exchanges++; //double swap = a[i]; //a[i] = a[j]; //a[j] = swap; } // shuffle the array a[] private static void shuffle(Sound a) { int N = a.getLength(); for (int i = 0; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); // between i and N-1 exchangeValues(a, i, r); } } }