3 Replies Latest reply: Feb 10, 2011 3:35 PM by captfoss RSS

    Recording from Mic Truncated

    837633
      Hi,

      I'm recording from a mic into a memory buffer. All works fine, except that it always cuts off the last second even though I believe I'm properly draining the target data line before closing it.

      I've tested with the AudioRecorder and SimpleAudioRecorder from the [url http://www.jsresources.org/examples/audio_playing_recording.html]JResources examples and a few other [url http://www.quadmore.com/swingrecorder/]examples out there. They seems to suffer from the same issue.

      Is this just a matter of some sort of latency when the recording is shut down?

      Here are the controlling methods for my recorder.
      public void startRecording()
      {
         state_ = State.RUNNING;
         line_.start();
         thread_ = new Thread()
         {
            public void run()
            {
               recordLoop();
            }
         };
         thread_.start();
      }
      private void recordLoop()
      {
         while(state_ != State.STOPPED)
         {
            int bytesRecorded = line_.read(block_, 0, blockSize_);
            if(bytesRecorded != 0)
               out_.write(block_, 0, bytesRecorded);
         }
      }
      public void stopRecording() throws Exception
      {
         line_.stop();
         line_.drain();
         state_ = State.STOPPED;
         thread_.join();
      }
      I'm running jdk 1.6.0_22-b04 on Windows XP.

      Thanks for any ideas!!!
        • 1. Re: Recording from Mic Truncated
          captfoss
          Everything looks ok to me. Just being nitpicky, the one (poor) assumption I do see is that you assume out_.write(block_, 0, bytesRecorded) will accept the entire block of data, and that's not necessarily true.
          • 2. Re: Recording from Mic Truncated
            837633
            Thanks, I'll make that fix to the write block.

            I think the issue is simply that it takes the mic longer than expected to finish pumping all its data. What I have is a GUI app. Lets say the user has been recording for 10 seconds and then hits the stop button. If I call stopRecording immediately, it will only have captured about 9 seconds. What I do now is peek at the output buffer to see how many seconds have actually been recorded and then wait until its captured 10 seconds, then I stop recording. So there's a little delay after they hit the stop while waiting for the mic to pump enough data.

            Seems to work so I'm happy. Maybe its specific to my recording setup?
            • 3. Re: Recording from Mic Truncated
              captfoss
              windroid wrote:
              Seems to work so I'm happy. Maybe its specific to my recording setup?
              Ummmm, the "one second" is probably specific to your recording setup... but I would imagine there is a delta_t associated with starting a recording and the recording starting.

              I'd imagine the TDL drain should handle that, but if there's like a hardware buffer on the soundcard that Java just doesn't have permission to flush... then you'd have to handle that yourself as you have. If the soundcare or the OS is doing some real-time transcoding to give JavaSound data in the format it wants, for instance, there could be a hidden unflushable buffer...