7 Replies Latest reply: Jan 17, 2013 3:08 AM by Kayaman RSS

    how playing the sound from sound card backward?

    985166
      hello

      I write the following program for playing backward the sounds from the sound card:
      package sound;
      
      import java.io.ByteArrayOutputStream;
      
      import javax.sound.sampled.*;
      
      public class SoundLimiter {
      
            //final AudioFormat format = new AudioFormat(44100, 8, 1, true, false);
           final AudioFormat format = AudioSystem.getClip().getFormat();
           final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, 1);
           final SourceDataLine soundLine = (SourceDataLine) AudioSystem.getLine(info);
           
            boolean stopped = false;
            int total;
           public SoundLimiter() throws LineUnavailableException {
      
                TargetDataLine line;
                
                if (!AudioSystem.isLineSupported(info)) {
                    // Handle the error ... 
      
                }
                // Obtain and open the line.
                try {
                    line = (TargetDataLine) AudioSystem.getLine(info);
                    line.open(format);
      
                    // playing back audio
                    line.start();
                 // Assume that the TargetDataLine, line, has already
                 // been obtained and opened.
                 ByteArrayOutputStream out  = new ByteArrayOutputStream();
                 int numBytesRead;
                 byte[] data = new byte[line.getBufferSize() / 5];
      
                 // Begin audio capture.
                 line.start();
      
                 soundLine.open(format);
                 int totalToRead = data.length;
                    while (total < totalToRead && !stopped){
                        //numBytesRead = stream.read(data, 0, numBytesRead);
                         numBytesRead =  line.read(data, 0, data.length);
                        if (numBytesRead == -1) break;
                        total += numBytesRead; 
                        soundLine.write(data, 0,numBytesRead);
                        //(data, 0, numBytesRead)
                    }
                } catch (LineUnavailableException ex) {
                    // Handle the error ... 
                     ex.getStackTrace();               
                }     
           }
           
           public static void main(String[] args) {
                try {
                     new SoundLimiter();
                } catch (LineUnavailableException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
           }
           
      }
      Using the above code i received the following error:
      {
      Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian, and buffers of 1 to 1 bytes is supported.
           at javax.sound.sampled.AudioSystem.getLine(Unknown Source)
           at sound.SoundLimiter.<init>(SoundLimiter.java:12)
           at sound.SoundLimiter.main(SoundLimiter.java:59)
      }

      Edited by: 982163 on Jan 16, 2013 1:46 AM

      Edited by: 982163 on Jan 16, 2013 2:32 AM
        • 1. Re: how playing the sound from sound card backward?
          Kayaman
          Well, first of all, use the
           tags to format your code.
          Second of all, your code looks pretty horrible (fair enough, you posted in New to Java).
          Lastly, the error message seems to be quite clear, you're trying to open a SourceDataLine with an unsupported format.
          
          Where did you get this code?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
          • 2. Re: how playing the sound from sound card backward?
            985166
            Hello

            I updated the code.
            I made the program from samples from java sound tutorial:
            {
            http://docs.oracle.com/javase/tutorial/sound/playing.html
            http://docs.oracle.com/javase/tutorial/sound/accessing.html
            http://docs.oracle.com/javase/tutorial/sound/capturing.html
            }

            The error message is clear for me also but I do not know how to modify the program.

            So can you tell me how to access a SourceDataLine with a supported format?

            Edited by: 982163 on Jan 16, 2013 2:51 AM
            • 3. Re: how playing the sound from sound card backward?
              Kayaman
              982163 wrote:
              but I do not know how to modify the program.
              That's why you need to learn to program before actually writing programs.
              • 4. Re: how playing the sound from sound card backward?
                985166
                Very unhelpful message...

                Please answer only if you know the answer do not spam the topic
                • 5. Re: how playing the sound from sound card backward?
                  gimbal2
                  982163 wrote:
                  Very unhelpful message...
                  No its not, its the truth.
                  Please answer only if you know the answer do not spam the topic
                  You don't get to decide that. I warn you: everyone that took the same attitude as you are now is no longer posting in this forum. I advise you to rethink your strategies regarding getting help.
                  • 6. Re: how playing the sound from sound card backward?
                    985166
                    982163 wrote:
                    Very unhelpful message...
                    No its not, its the truth.

                    I do not need general answers , I need answers regarding my question.
                    I suppose is my mistake to post here.


                    Please answer only if you know the answer do not spam the topic
                    You don't get to decide that. I warn you: everyone that took the same attitude as you are now is no longer posting in this forum. I advise you to rethink your strategies regarding getting help.
                    Ok The moderator will decide I understood.

                    For your consideration I run the exact code from another computer and I received another error
                    {
                    Exception in thread "main" java.lang.ClassCastException: com.sun.media.sound.MixerSourceLine cannot be cast to javax.sound.sampled.TargetDataLine
                         at SoundLimiter.<init>(SoundLimiter.java:25)
                         at SoundLimiter.main(SoundLimiter.java:57)
                    }
                    Same computers had windows xp installed but different sound cards.
                    • 7. Re: how playing the sound from sound card backward?
                      Kayaman
                      Aurelian_cl wrote:
                      I do not need general answers , I need answers regarding my question.
                      Everyone does, but since you're not paying us anything, you'll have to do with what you get.
                      I suppose is my mistake to post here.
                      No, your mistake was taking on a project that is beyond your means. Instead of learning yourself, you're asking us to do your work. That's not going to happen.
                      For your consideration I run the exact code from another computer and I received another error
                      {
                      Exception in thread "main" java.lang.ClassCastException: com.sun.media.sound.MixerSourceLine cannot be cast to javax.sound.sampled.TargetDataLine
                           at SoundLimiter.<init>(SoundLimiter.java:25)
                           at SoundLimiter.main(SoundLimiter.java:57)
                      }
                      Well, you're doing a bad cast at line 25. And I can't see how you'd even try to cast a SourceLine to a TargetLine.

                      Same computers had windows xp installed but different sound cards.
                      That shouldn't be an issue.