CSE-8A / PA5 / Sounds.java
Sounds.java
Raw
import java.lang.Math.*;

import javax.print.attribute.standard.ColorSupported;

public class Sounds{

    // DO NOT CHANGE
    static CSE8ALib lib = new CSE8ALib();

    // DO NOT CHANGE
    static final int SAMPLE_RATE = 22050;  // 22.05 KHz

    public static void main(String[] args){

        // STEP 2: use your functions here to create your sounds

	    int[] sound1 = lib.readSound("sounds/UpbeatFunk.wav"); // TODO fill in the path to your first sound (in the sounds folder)
        int[] sound2 = lib.readSound("sounds/wind.wav"); // TODO fill in the path to your second sound (in the sounds folder)
    //    lib.explore(sound1);
    //    lib.explore(sound2);

//Using Sine method 
        int []soundV1 = sineSound(1000000, 1300, 1200);
//Using adding method 
        int []soundV2 = addSounds(sound1, sound2);
//Creating new sound from the method
        int [] ultimateSound = concatSounds(soundV2, soundV1);
        lib.explore(ultimateSound);

   // test();
    }

    public static void test(){
        // test crop function
        System.out.println("\n******* Testing crop ********");
        int[] inputCropped = { 3, 10, 15, 32, 6, 6};
        int[] croppedAns = { 15, 32, 6};
    //cropped only taking the 3rd to 5th number
        int[] cropped = crop(inputCropped, 2, 5);
        System.out.print("Should be: ");
        printArray(croppedAns);
        System.out.print("Got: ");
        printArray(cropped);


        // test concatSounds
        System.out.println("\n******* Testing concatSounds ********");
        int[] inputSoundConcat1 = { 8, 2, 7};
        int[] inputSoundConcat2 = { 4, -30, 25};
        int[] concatAns = { 8, 2, 7, 4, -30, 25};
    // concatenated is combinding the two arrays 
        int[] concatenated = concatSounds( inputSoundConcat1, inputSoundConcat2 );
        System.out.print("Should be: ");
        printArray(concatAns);
        System.out.print("Got: ");
        printArray(concatenated);

        // test sineSound
        System.out.println("\n******* Testing sineSound ********");
        int[] sinAns = { 0, -165, -276, -295, -216, -65, 106, 243, 299, 256};
    //sinWave is using a formula to get the values
        int[] sineWave = sineSound(10, 12000, 300);
        System.out.print("Should be: ");
        printArray(sinAns);
        System.out.print("Got: ");
        printArray(sineWave);

        // test addSounds
        System.out.println("\n******* Testing addSounds ********");
        int[] inputSoundAdd1 = { 23, 10, 17};
        int[] inputSoundAdd2 = { 10, 20, 13};
        int[] addedAns = { 33, 30, 30};
    //added is adding the values of the integers within two arrays together
        int[] added = addSounds( inputSoundAdd1, inputSoundAdd2 );
        System.out.print("Should be: ");
        printArray(addedAns);
        System.out.print("Got: ");
        printArray(added);

    }

    // 3 parameters:
    //      1) an array of ints representing a sound
    //      2) a start index
    //      3) an end index
    // returns:
    //      a new array of ints representing the interval of that same sound that starts
    //      with the sample at the start index and ends at the sample immediately before the
    //      end index.  (You can assume that the end index given will be greater than the start index.)
    // Use a for loop
    // start_index and end_index is already given as variables
    public static int[] crop(int[] sound, int start_index, int end_index){
        int[] cropped = new int [end_index - start_index];
        for( int i = 0; i <(end_index - start_index) ; i++){
    //start_index + i is necessary so the index of sound can move to the right
            cropped[i] = sound[start_index + i];
        }
        return cropped;  

    
    }

    // STEP 1: write concatSounds function
    // 2 parameters:
    //      1) an array of ints representing the first sound
    //      2) an array of ints representing the second sound
    // returns:
    //      a new sound that contains the first sound followed by the second.
    public static int[] concatSounds(int[] sound1, int[] sound2){
        int[] concatenated = new int[sound1.length + sound2.length];
    //one for-loop for the first part of the new array
        for( int i = 0; i < sound1.length; i++){
            concatenated[i] = sound1[i];
        }
    // second for-loop for the second part of the new array
        for(int i = 3; i < sound2.length; i++){
            concatenated[i] = sound2[i-3];
        }
        return concatenated;
    }

    // STEP 1: write sineSound
    // 3  parameters:
    //      1) a length in samples
    //      2) a frequency in oscillations per second
    //      3) an amplitude
    // returns:
    //      a sound (array of ints) that has the given number of samples, where each entry is given by
    //      sin(((2 * pi * i) / samplerate) * frequency) * amplitude, where i is the index of the entry.
    public static int[] sineSound(int length, int frequency, int amplitude){
    //The length of the index is length value of sineSound
        int [] sinWave = new int[length];
        for ( int i = 0; i < length; i++){
            sinWave[i] = lib.doubleToInt(lib.sin(2*Math.PI*i/SAMPLE_RATE*frequency) * amplitude);
        }
        return sinWave;
    }

    // STEP 1: write addSounds
    // 2 parameters:
    //      1) array of ints representing one sound
    //      2) array of ints representing another sound
    // returns:
    //      a new sound (array of ints) where the amplitude at each index
    //      is the sum of the amplitudes of the input sounds at that index.
    public static int[] addSounds(int[] sound1, int[] sound2){
        int [] added = new int[sound1.length];
    //The addition of two arrays 
        for( int i = 0; i < 3; i++){
            added[i] = sound1[i] + sound2[i];
        }
        return added;
    }

    // DO NOT CHANGE
    // changeVolume
    // 2 parameters:
    //      1) array of ints representing one sound
    //      2) double representing the scaling factor
    // returns:
    //      an array of ints representing the scaled input sound
    // NOTE: to lower the volume, scalar should be less than 1
    // NOTE: to raise the volume, scalar should be greater than 1
    public static int[] changeVolume(int[] sound, double scalar){
        int[] res = new int[sound.length];
        for(int i = 0; i < sound.length; i++){
            res[i] = lib.doubleToInt(lib.intToDouble(sound[i]) * scalar);
        }
        return res;
    }

    // DO NOT CHANGE
    // printArray
    // 1 parameter:
    //      1) array of ints representing one sound
    // returns:
    //      this function does not return anything
    public static void printArray(int[] array){
        System.out.print("{");
        for(int i = 0; i < array.length-1; i++){
            System.out.print(array[i] + ", ");
        }
        if (array.length > 0) {
            System.out.print(array[array.length-1]);
        }
        System.out.println("}");
    }

}