lib/minim/src/ddf/minim/javasound/JSBufferedSampleRecorder.java
/**
   * Saves the audio in the internal buffer to a file using the current settings for 
   * file type and file name.
   */
Saves the audio in the internal buffer to a file using the current settings for
file type and file name.
Saves the audio in the internal buffer to a file using the current settings for file type and file name .
public AudioRecordingStream save()
  {
    if ( isRecording() )
    {
      system.error("You must stop recording before you can write to a file.");
    }
    else
    {
      int channels = format.getChannels();
      int length = left.capacity();
      int totalSamples = ( buffers.size() / channels ) * length;
      FloatSampleBuffer fsb = new FloatSampleBuffer(channels,
                                                    totalSamples,
                                                    format.getSampleRate());
      if ( channels == 1 )
      {
        for (int i = 0; i < buffers.size(); i++)
        {
          int offset = i*length;
          FloatBuffer fb = (FloatBuffer)buffers.get(i);
          fb.rewind();
          fb.get(fsb.getChannel(0), offset, length);
        }
      }
      else
      {
        for (int i = 0; i < buffers.size(); i+=2)
        {
          int offset = (i/2)*length;
          FloatBuffer fbL = (FloatBuffer)buffers.get(i);
          FloatBuffer fbR = (FloatBuffer)buffers.get(i+1);
          fbL.rewind();
          fbL.get(fsb.getChannel(0), offset, length);
          fbR.rewind();
          fbR.get(fsb.getChannel(1), offset, length);
        }
      }
      int sampleFrames = fsb.getByteArrayBufferSize(format) / format.getFrameSize();
      ByteArrayInputStream bais = new ByteArrayInputStream(fsb.convertToByteArray(format));
      AudioInputStream ais = new AudioInputStream(bais, format, sampleFrames);
      if (AudioSystem.isFileTypeSupported(type, ais))
      {
        File out = new File( name );
        try
        {
          AudioSystem.write(ais, type, out);
        }
        catch (IOException e)
        {
          system.error("AudioRecorder.save: Error attempting to save buffer to "
              + name + "\n" + e.getMessage());
        }
        if (out.length() == 0)
        {
          system.error("AudioRecorder.save: Error attempting to save buffer to "
              + name + ", the output file is empty.");
        }
      }
      else
      {
        system.error("AudioRecorder.save: Can't write " + type.toString()
            + " using format " + format.toString() + ".");
      }
    }
    String filePath = filePath();
    AudioInputStream ais = system.getAudioInputStream(filePath);
    SourceDataLine sdl = system.getSourceDataLine(ais.getFormat(), 1024);
    long length = AudioUtils.frames2Millis(ais.getFrameLength(), format);
    BasicMetaData meta = new BasicMetaData(filePath, length, ais.getFrameLength());
    JSPCMAudioRecordingStream recording = new JSPCMAudioRecordingStream(system, meta, ais, sdl, 1024);
    return recording;
  }
public AudioRecordingStream save ( ) { if ( isRecording ( ) ) { system . error ( "You must stop recording before you can write to a file." ) ; } else { int channels = format . getChannels ( ) ; int length = left . capacity ( ) ; int totalSamples = ( buffers . size ( ) / channels ) * length ; FloatSampleBuffer fsb = new FloatSampleBuffer ( channels , totalSamples , format . getSampleRate ( ) ) ; if ( channels == 1 ) { for ( int i = 0 ; i < buffers . size ( ) ; i ++ ) { int offset = i * length ; FloatBuffer fb = ( FloatBuffer ) buffers . get ( i ) ; fb . rewind ( ) ; fb . get ( fsb . getChannel ( 0 ) , offset , length ) ; } } else { for ( int i = 0 ; i < buffers . size ( ) ; i += 2 ) { int offset = ( i / 2 ) * length ; FloatBuffer fbL = ( FloatBuffer ) buffers . get ( i ) ; FloatBuffer fbR = ( FloatBuffer ) buffers . get ( i + 1 ) ; fbL . rewind ( ) ; fbL . get ( fsb . getChannel ( 0 ) , offset , length ) ; fbR . rewind ( ) ; fbR . get ( fsb . getChannel ( 1 ) , offset , length ) ; } } int sampleFrames = fsb . getByteArrayBufferSize ( format ) / format . getFrameSize ( ) ; ByteArrayInputStream bais = new ByteArrayInputStream ( fsb . convertToByteArray ( format ) ) ; AudioInputStream ais = new AudioInputStream ( bais , format , sampleFrames ) ; if ( AudioSystem . isFileTypeSupported ( type , ais ) ) { File out = new File ( name ) ; try { AudioSystem . write ( ais , type , out ) ; } catch ( IOException e ) { system . error ( "AudioRecorder.save: Error attempting to save buffer to " + name + "\n" + e . getMessage ( ) ) ; } if ( out . length ( ) == 0 ) { system . error ( "AudioRecorder.save: Error attempting to save buffer to " + name + ", the output file is empty." ) ; } } else { system . error ( "AudioRecorder.save: Can't write " + type . toString ( ) + " using format " + format . toString ( ) + "." ) ; } } String filePath = filePath ( ) ; AudioInputStream ais = system . getAudioInputStream ( filePath ) ; SourceDataLine sdl = system . getSourceDataLine ( ais . getFormat ( ) , 1024 ) ; long length = AudioUtils . frames2Millis ( ais . getFrameLength ( ) , format ) ; BasicMetaData meta = new BasicMetaData ( filePath , length , ais . getFrameLength ( ) ) ; JSPCMAudioRecordingStream recording = new JSPCMAudioRecordingStream ( system , meta , ais , sdl , 1024 ) ; return recording ; }
Saves the audio in the internal buffer to a file using the current settings for
 file type and file name.
Saves the audio in the internal buffer to a file using the current settings for file type and file name .
// copy all the floats in fb to the first channel // of fsb, starting at the index offset, and copy // the whole FloatBuffer over. // this is fine because the recording will always be  // in a raw format (WAV, AU, etc).