?
Solved

Getting audio source into SequenceAudioInputStream.java

Posted on 2004-03-21
4
Medium Priority
?
252 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 1400 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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