Solved

Getting audio source into SequenceAudioInputStream.java

Posted on 2004-03-21
4
245 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 92
for i loop in grovy 1 32
Java Message handling in Service Layer 3 38
maven project error 5 24
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now