Solved

Getting audio source into SequenceAudioInputStream.java

Posted on 2004-03-21
4
247 Views
Last Modified: 2012-06-27
Sorry about asking this question in another way! Hello to everyone out there, I hope someone can help me with this problem!    Can you make a note in the code to point this out to me. I'm very new at programing and learn from example best. Thanks in advance.


How does a person get an audio source into this program?




//SequenceAudioInputStream.java

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.BooleanControl;



public class SequenceAudioInputStream
     extends          AudioInputStream
{
     private static final boolean     DEBUG = true;

     private List               m_audioInputStreamList;
     private     int               m_nCurrentStream;



     public SequenceAudioInputStream(AudioFormat audioFormat, Collection audioInputStreams)
     {
          super(new ByteArrayInputStream(new byte[0]), audioFormat, AudioSystem.NOT_SPECIFIED);
          m_audioInputStreamList = new ArrayList(audioInputStreams);
          m_nCurrentStream = 0;
     }



     // TODO: remove
     private boolean addAudioInputStream(AudioInputStream audioStream)
     {
          if (DEBUG)
          {
               out("SequenceAudioInputStream.addAudioInputStream(): called.");
          }
          // Contract.check(audioStream != null);
          if (!getFormat().matches(audioStream.getFormat()))
          {
               if (DEBUG)
               {
                    out("SequenceAudioInputStream.addAudioInputStream(): audio formats do not match, trying to convert.");
               }
               AudioInputStream     asold = audioStream;
               audioStream = AudioSystem.getAudioInputStream(getFormat(), asold);
               if (audioStream == null)
               {
                    out("###  SequenceAudioInputStream.addAudioInputStream(): could not convert.");
                    return false;
               }
               if (DEBUG)
               {
                    out(" converted");
               }
          }
          // Contract.check(audioStream != null);
          synchronized (m_audioInputStreamList)
          {
               m_audioInputStreamList.add(audioStream);
               m_audioInputStreamList.notifyAll();
          }
          if (DEBUG)
          {
               out("SequenceAudioInputStream.addAudioInputStream(): enqueued " + audioStream);
          }
          return true;
     }



     private AudioInputStream getCurrentStream()
     {
          return (AudioInputStream) m_audioInputStreamList.get(m_nCurrentStream);
     }



     private boolean advanceStream()
     {
          m_nCurrentStream++;
          boolean     bAnotherStreamAvailable = (m_nCurrentStream < m_audioInputStreamList.size());
          return bAnotherStreamAvailable;
     }



     public long getFrameLength()
     {
          long     lLengthInFrames = 0;
          Iterator     streamIterator = m_audioInputStreamList.iterator();
          while (streamIterator.hasNext())
          {
               AudioInputStream     stream = (AudioInputStream) streamIterator.next();
               long     lLength = stream.getFrameLength();
               if (lLength == AudioSystem.NOT_SPECIFIED)
               {
                    return AudioSystem.NOT_SPECIFIED;
               }
               else
               {
                    lLengthInFrames += lLength;
               }
          }
          return lLengthInFrames;
     }



     public int read()
          throws     IOException
     {
          AudioInputStream     stream = getCurrentStream();
          int     nByte = stream.read();
          if (nByte == -1)
          {
               /*
                 The end of the current stream has been signaled.
                 We try to advance to the next stream.
               */
               boolean     bAnotherStreamAvailable = advanceStream();
               if (bAnotherStreamAvailable)
               {
                    /*
                      There is another stream. We recurse into this method
                      to read from it.
                    */
                    return read();
               }
               else
               {
                    /*
                      No more data. We signal EOF.
                    */
                    return -1;
               }
          }
          else
          {
               /*
                 The most common case: We return the byte.
               */
               return nByte;
          }
     }



     public int read(byte[] abData, int nOffset, int nLength)
          throws     IOException
     {
          AudioInputStream     stream = getCurrentStream();
          int     nBytesRead = stream.read(abData, nOffset, nLength);
          if (nBytesRead == -1)
          {
               /*
                 The end of the current stream has been signaled.
                 We try to advance to the next stream.
               */
               boolean     bAnotherStreamAvailable = advanceStream();
               if (bAnotherStreamAvailable)
               {
                    /*
                      There is another stream. We recurse into this method
                      to read from it.
                    */
                    return read(abData, nOffset, nLength);
               }
               else
               {
                    /*
                      No more data. We signal EOF.
                    */
                    return -1;
               }
          }
          else
          {
               /*
                 The most common case: We return the length.
               */
               return nBytesRead;
          }
     }



     public long skip(long lLength)
          throws     IOException
     {
          throw new IOException("skip() is not implemented in class SequenceInputStream. Mail <Matthias.Pfisterer@web.de> if you need this feature.");
     }



     public int available()
          throws     IOException
     {
          return getCurrentStream().available();
     }



     public void close()
          throws     IOException
     {
          // TODO: should we close all streams in the list?
     }



     public void mark(int nReadLimit)
     {
          throw new RuntimeException("mark() is not implemented in class SequenceInputStream. Mail <Matthias.Pfisterer@web.de> if you need this feature.");
     }



     public void reset()
          throws     IOException
     {
          throw new IOException("reset() is not implemented in class SequenceInputStream. Mail <Matthias.Pfisterer@web.de> if you need this feature.");
     }



     public boolean markSupported()
     {
          return false;
     }



     private static void out(String strMessage)
     {
          System.out.println(strMessage);
     }
}



/*** SequenceAudioInputStream.java ***/
0
Comment
Question by:Drop_of_Rain
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
Javatm earned 350 total points
ID: 10647492
> How does a person get an audio source into this program?

Through this procedure :

AudioInputStream asold = audioStream;
audioStream = AudioSystem.getAudioInputStream(getFormat(), asold);

private AudioInputStream getCurrentStream()
{
 return (AudioInputStream) m_audioInputStreamList.get(m_nCurrentStream);
}

Read this for documentation :
http://java.sun.com/j2se/1.4.2/docs/api/javax/sound/sampled/AudioInputStream.html

Hope that helps . . .
Javatm
0
 

Author Comment

by:Drop_of_Rain
ID: 10651358
Javatm thanks for the answer. I'll use it. Thanks again.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10693469
http://www.freewebs.com/cube-j

Always glad to help . . .
Friend : Javatm
0
 

Author Comment

by:Drop_of_Rain
ID: 10693501
If you are still around I have another question up. I need to find out how to coordinate some storeed files and a procedure.

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20933943.html

Give me some feedback

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question