Solved

Getting audio source into SequenceAudioInputStream.java

Posted on 2004-03-21
4
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

696 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