All right, I don't usually do this, but I've come for you for homework help. I have an assignment due tomorrow, and I can't figure out what's wrong with my program. I'm not asking for an answer -- I don't want to cheat -- I just need a little guidance.
The assignment is to create a "sentence" of Sound objects, each sound being one word. I have the algorithm, but I'm just not coding it right, apparently. First I load the three audio files of the three words I want to make the sentence out of: "always," "and," and "around." I'm looping through each SoundSample in the first word and copying them into a newly created empty Sound object, and I do the same for each word, but my output is simply the first word (with a lot of silence afterward).
I can't figure out why the other two words aren't being copied. Could someone help? My code is below. It uses an outside library (including Sound, SoundSample created by the course creators; I believe my program should be readable without me having to post the separate classes.
// A class to create an audio sentence out of individual words
// Coded on December 1, 2008
// by Jared Cerbin
public class CreateSentence
{
/**
* Run the program.
* @param args the command line arguments
*/
public static void main(String[] args)
{
Sound word1 = new Sound(FileChooser.getMediaPath("always.wav"));
Sound word2 = new Sound(FileChooser.getMediaPath("and.wav"));
Sound word3 = new Sound(FileChooser.getMediaPath("around.wav"));
Sound[] sentence = {word1, word2, word3};
makeSentence(sentence, 1).explore();
}
/**
* Make the sentence with specified words and specified pause length
* @return the sentence
* @param soundArray an array of Sound objects to make the sentence
* @param pauseLength the length of the pause between the words, in tens of seconds
*/
public static Sound makeSentence(Sound[] soundArray, int pauseLength)
{
// Create constants for the number of words in the sentence
// and the sampling rate of the sentence, and create variables
// for the number of samples in each word and the total samples
// in the finished sentence
final int NUMBER_OF_WORDS = soundArray.length;
final double SAMPLING_RATE = soundArray[0].getSamplingRate();
int[] numberOfSamples = new int[NUMBER_OF_WORDS];
int totalSamples = 0;
// Loop through the words
for(int i = 0; i < NUMBER_OF_WORDS; i++)
{
// Get the number of samples in each word and add
// the number to the total number of samples
numberOfSamples[i] = soundArray[i].getNumSamples();
totalSamples += numberOfSamples[i];
}
// Add the samples for the pauses to the total
totalSamples += pauseLength * 0.1 * SAMPLING_RATE;
// Create an empty Sound object with the necessary length
Sound sentence = new Sound(totalSamples);
int j = 0;
// Loop as many times as there are words
for(int i = 0; i < NUMBER_OF_WORDS; i++)
{
// Create an array of SoundSample objects of the current word
SoundSample[] wordSamples = soundArray[i].getSamples();
// Loop through the SoundSamples in the new array
while(j < soundArray[i].getNumSamples())
{
// Set the value in the sentence to the value in the word
sentence.setSampleValueAt(j, soundArray[i].getSampleValueAt(j));
j++;
}
}
return sentence;
}
}