Solved

Getting audio source into SequenceAudioInputStream.java

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 85
ejb on wildfly 5 45
sql import cannot be resolved jsp 3 74
jsp CRUD operations with and without prepared statement also hibernatge 1 27
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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:

735 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