1 2 3 Previous Next 36 Replies Latest reply: Oct 26, 2010 12:13 PM by captfoss RSS

    JavaSound questions

    Will Pittenger
      I am looking at modifying http://www.jsresources.org/examples/SimpleAudioRecorder.java.html to demonstrate my knowledge of the JavaSound API. The changes are to help ensure I am getting input from the Microphone and add the ability to reset and/or pause the recording. To do the latter, I plan to call TargetDataLine.read instead of AudioSystem.write.

      What I don't know:
      * I don't yet see how to ensure I get the microphone rather than just any SourceDataLine. I seem to get the microphone anyway, but I don't know why. For all I know, on other machines, the same code might give me Line In or a audio CD. Is there a fix? Can I be assured of always getting the Microphone?
      * I am leaving it as the WAV file type, but how do I specify compression options?
        • 1. Re: JavaSound questions
          DarrylBurke
          797731 wrote:
          I am looking at modifying...l to demonstrate my knowledge of the JavaSound API.
          Moving to the Java Sound forum.

          db
          • 2. Re: JavaSound questions
            Will Pittenger
            (This thread was meant to continue
            captfoss wrote:
            JMF isn't included by default in the JRE and hasn't been supported in 8 years... JavaSound is included in the JRE and, as such, is currently supported.
            So you have the option of old, unsupported code that you'll have to handle packaging / linking with your code, or, you can use something built-in that'll work with applications, applets, and web-start apps.
            So the ease of use of the language should be, honestly, the least important design consideration for a programmer, in this instance at least.
            And to actually answer your question, no...neither of them are "easy" to understand...but JavaSound is built around the stream concept, so if you're familiar with using any sort of InputStream and OutputStream architecture, you have some background into JavaSound.
            So, as a basic introduction to JavaSound...
            To record a file in JavaSound, you copy data from a TargetDataLine to a file... to pause the recording, you simply continue to read the data from the TDL but discontinue writing it to the file...to rewind the recording, you simply seek backwards in the file and overwrite existing data...
            And there are "helper" classes that you need to use. AudioInputStream must be used to handle the reading from and writing to a file. AudioSystem.write should be used to write an AudioInputStream to file... and you have to do some "tricks" with the AIS to be able to write (AIS can be constructed with an InputStream, and I like to use a PipedOutputStream->PipedInputStream->AudioInputStream ordering to be able to "write" to an AudioInputStream as it is an InputStream and you can't write directly to it).
            To better understand what "lines" are, you should read the article I gave you...
            http://www.vsj.co.uk/java/display.asp?id=370
            To see those classes "in action", you should look at the sample code...
            http://www.jsresources.org/examples/
            And if you're wanting to learn how the whole system works together, RTFM
            http://download.oracle.com/javase/1.5.0/docs/guide/sound/programmer_guide/contents.html
            It is giving me grief. I seem to be having problems in addition to what I noted above. How do I use AudioSystem.write without write waiting for the end up the input stream? If it doesn't return, I don't see a way to pause or reset the recording.
            • 3. Re: JavaSound questions
              captfoss
              Will,

              Several of your questions are addressed by the following excellent article about JavaSound:
              http://www.vsj.co.uk/java/display.asp?id=370

              But you already know that because I've asked you to read it twice before ;-)

              As for the blocking nature of AudioSystem.write, don't use that until you're ready to finalize the file... write your audio data into a temporary file first, and then when you're ready to save... attach an input stream to the temporary file, wrap that in an AudioInputStream, and hand the AIS to AudioSystem.write...
              • 4. Re: JavaSound questions
                Will Pittenger
                Unfortunately, there is a problem, I am having problems getting sound directly out of the line with AudioInputStream.read. Even though I opened the line and started the stream, both isRunning and isActive return false.
                • 5. Re: JavaSound questions
                  captfoss
                  Will Pittenger wrote:
                  Unfortunately, there is a problem, I am having problems getting sound directly out of the line with AudioInputStream.read. Even though I opened the line and started the stream, both isRunning and isActive return false.
                  You'd have to post some code for me to have anything to say... and please post it using the comment tags as described in the forum's "Welcome to the new home" sticky...
                  • 6. Re: JavaSound questions
                    Will Pittenger
                    See http://pastebin.com/jFWVW3qG. I put the entire source of my modified version of SimpleAudioRecorder.java there.
                    • 7. Re: JavaSound questions
                      Will Pittenger
                      Would closing an attached PipeInputStream cause AudioInputStream to tell AudioSystem.write to end? Or would AudioSystem.write need some other signal to write out the end of the file?
                      • 8. Re: JavaSound questions
                        captfoss
                        Will Pittenger wrote:
                        Would closing an attached PipeInputStream cause AudioInputStream to tell AudioSystem.write to end? Or would AudioSystem.write need some other signal to write out the end of the file?
                        I don't think the close event would cascade through the streams like that. It's possible you could be closing the input stream as a "pause" event.

                        You should probably close the AIS itself to signal to the AudioSystem to finish out the file.
                        • 9. Re: JavaSound questions
                          Will Pittenger
                          Did you ever figure out why I was unable to start the line in that code I posted? Since then, I have written a class that will go in our real program. (It isn't intended for that simple program I was demoing with.) You might look at it (http://pastebin.com/XihBncE9) and tell me if you see any problems. I am hoping I won't run into that line start problem here.
                          • 10. Re: JavaSound questions
                            Will Pittenger
                            I should add some notes on that new code.

                            *The selectLine function was based loosely on code from http://www.vsj.co.uk/java/display.asp?id=370.  One thing that has troubled me is how that sample used source lines to get a microphone port.  But if I have my terminology right, it should be target line.
                            *The FileWriter inner class is based on your suggestion with the pipe streams.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                            • 11. Re: JavaSound questions
                              Will Pittenger
                              Well, locating the port isn't enough. There doesn't seem to be a way to get from the information for a port to a TargetDataLine.
                              • 12. Re: JavaSound questions
                                Will Pittenger
                                Well, to clarify, I appear to need a way to connect a target data line to the Microphone port. I don't see that. My code does locate the port (which I already had) and its mixer (which I didn't), but I don't see a way to connect a target data line to it.
                                • 13. Re: JavaSound questions
                                  captfoss
                                  I'm not sure that you're understanding a few key concepts...

                                  TargetDataLines don't attach to anything, they come pre-attached to mixers. The same way SourceDataLines come attached to mixers, just on the other side.

                                  Mixers dump into TDLs and you read from TDLs. You write to SDLs and they dump into Mixers.

                                  Input ports (microphone, line in, etc) are equviolent to SDLs. They dump into a Mixer.
                                  Output ports (speakers, line out, etc) are equivolent to TDLs. Mixers dump into them.

                                  The difference is, you can't read or write directly from a port, you have to read/write from output/input associated with the Mixer it's attached to.

                                  So, to "read" from a microphone port, you fine the port, get it's mixer, and get one of the TargetDataLines from the Mixer. If you need the microphone to feed into another Mixer, then you have to manually read data from the TDL on Mixer1 to the SDL on Mixer2...
                                  • 14. Re: JavaSound questions
                                    captfoss
                                    Also as a P.S. I'm happy to help you with specific code snippets you can post on the forum, but I'm not going to go to external sites to look through full code files.
                                    1 2 3 Previous Next