/** * combine two sounds together into a third sound object * * the first sound will be broken into three parts * the final sound object will contain * part 1 of the first sound * the second sound * part 2 of the first sound * the second sound (again) * part 3 of the first sound * * @author Pat Troy: troy AT uic DOT edu */ import java.util.*; import java.awt.*; import java.io.*; public class Lect49d { public static void main(String[] args) { // Prompt the user for two sound files String fileName = FileChooser.pickAFile(); Sound s1 = new Sound (fileName); fileName = FileChooser.pickAFile(); Sound s2 = 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); double s2seconds = s2.getLength() / s2.getSamplingRate() ; System.out.println ("S2 has a calculated length (in seconds) of: " + s2seconds); double s3seconds = s1seconds + (2 * s2seconds); System.out.println ("S3 needs seconds of sound: " + s3seconds); System.out.println ((int) Math.ceil(s3seconds) ); Sound s3 = new Sound ( (int) Math.ceil(s3seconds) * 22100); // 22,100 samples per second double s3seconds2 = s3.getLength() / s3.getSamplingRate() ; System.out.println ("S3 has X samples " + s3.getLength() ); System.out.println ("S3 has a calculated length (in seconds) of: " + s3seconds2); // put the sound file into an array format SoundSample[] ss1array = s1.getSamples(); SoundSample[] ss2array = s2.getSamples(); SoundSample[] ss3array = s3.getSamples(); int index, s3index; SoundSample ss, ss3; s3index = 0; // copy the first third of ss1 for (index = 0; index < ss1array.length/3 ; index++) { // get the numeric value at the sample ss = ss1array[index]; int samp = ss.getValue() ; //get the SoundSample where this value will be stored ss3 = ss3array[s3index]; ss3.setValue (samp); ss3array[s3index] = ss3; s3index++; } // copy ss2 for (index = 0; index < ss2array.length ; index++) { // get the numeric value at the sample ss = ss2array[index]; int samp = ss.getValue() ; //get the SoundSample where this value will be stored ss3 = ss3array[s3index]; ss3.setValue (samp); ss3array[s3index] = ss3; s3index++; } // copy the second third of ss1 for (index = ss1array.length/3 ; index < 2*ss1array.length/3 ; index++) { // get the numeric value at the sample ss = ss1array[index]; int samp = ss.getValue() ; //get the SoundSample where this value will be stored ss3 = ss3array[s3index]; ss3.setValue (samp); ss3array[s3index] = ss3; s3index++; } // copy ss2 for (index = 0; index < ss2array.length ; index++) { // get the numeric value at the sample ss = ss2array[index]; int samp = ss.getValue() ; //get the SoundSample where this value will be stored ss3 = ss3array[s3index]; ss3.setValue (samp); ss3array[s3index] = ss3; s3index++; } // copy the last third of ss1 for (index = 2*ss1array.length/3; index < ss1array.length ; index++) { // get the numeric value at the sample ss = ss1array[index]; int samp = ss.getValue() ; //get the SoundSample where this value will be stored ss3 = ss3array[s3index]; ss3.setValue (samp); ss3array[s3index] = ss3; s3index++; } System.out.println("Before Play"); //s1.play(); s3.explore(); System.out.println("After Play"); } }