Results 1 to 5 of 5
  1. #1
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default Combining Individual Sounds - Urgent

    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.

    Java Code:
    // 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;
      }
    }
    "Things are made of littler things that jiggle."

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have no idea since I'm not familiar with the Sound class. Question though, why do you make the wordSamples array but don't use it?

  3. #3
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    Ah! I think I found the error! You're right, I'm not using wordSamples. I think I need to put it in the inside loop. I'll test around and post back soon. Thanks!
    "Things are made of littler things that jiggle."

  4. #4
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    Yes! I got it! Thanks for your help!
    "Things are made of littler things that jiggle."

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Wave Sounds
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-22-2008, 01:44 PM
  2. Turn off sounds in Eclipse
    By gio.fou in forum Eclipse
    Replies: 2
    Last Post: 09-06-2008, 08:31 PM
  3. Help combining loops into 1 program.
    By kewlgeye in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 09:58 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. Sounds don't play when running applet
    By leonard in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •