title: Creating flac file or flac stream using BASS dll with Delphi
body: <p>I am playing with BASS from <a href="http://www.un4seen.com/" rel="nofollow noreferrer">http://www.un4seen.com/</a>.</p>
  
  <p>I need to create a flac file(16bits) or flac stream from user speaking on Microphone.</p>
  
  <p>I have seen this demo in BASS source code.
  There is a bassenc_flac.dll as well with these functions:</p>
  
  <pre><code>function BASS_Encode_FLAC_Start(handle:DWORD; options:PChar; flags:DWORD; proc:ENCODEPROCEX; user:Pointer): HENCODE; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external bassencflacdll;
  function BASS_Encode_FLAC_StartFile(handle:DWORD; options:PChar; flags:DWORD; filename:PChar): HENCODE; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external bassencflacdll;
  </code></pre>
  
  <p>How could I change the next code to encode the audio to flac file or stream?</p>
  
  <p>From RecordTest BASS demo</p>
  
  <pre><code>(* This is called while recording audio *)
  function RecordingCallback(Handle: HRECORD; buffer: Pointer; length: DWORD; user: Pointer): boolean; stdcall;
  var level:dword;
  begin
      level:=BASS_ChannelGetLevel(Handle);
      // Copy new buffer contents to the memory buffer
      Form1.WaveStream.Write(buffer^, length);
      // Allow recording to continue
      Result := True;
  end;
  
  (* Start recording to memory *)
  procedure TForm1.StartRecording;
  begin
      if ComboBox1.ItemIndex &lt; 0 then Exit;
      if WaveStream.Size &gt; 0 then
      begin   // free old recording
          BASS_StreamFree(chan);
          WaveStream.Clear;
      end;
      // generate header for WAV file
      with WaveHdr do
      begin
          riff := 'RIFF';
          len := 36;
          cWavFmt := 'WAVEfmt ';
          dwHdrLen := 16;
          wFormat := 1;
          wNumChannels := 2;
          dwSampleRate := 44100;
          wBlockAlign := 4;
          dwBytesPerSec := 176400;
          wBitsPerSample := 16;
          cData := 'data';
          dwDataLen := 0;
      end;
      WaveStream.Write(WaveHdr, SizeOf(WAVHDR));
      // start recording @ 44100hz 16-bit stereo
      rchan := BASS_RecordStart(44100, 2, 0, @RecordingCallback, nil);
      if rchan = 0 then
      begin
          MessageDlg('Couldn''t start recording!', mtError, [mbOk], 0);
          WaveStream.Clear;
      end
      else
      begin
          bRecord.Caption := 'Stop';
          bPlay.Enabled := False;
          bSave.Enabled := False;
      end;
  end;
  
  
  (* Stop recording *)
  procedure TForm1.StopRecording;
  var
      i: integer;
    he:BassEnc.HENCODE;
  begin
      BASS_ChannelStop(rchan);
      bRecord.Caption := 'Record';
      // complete the WAV header
      WaveStream.Position := 4;
      i := WaveStream.Size - 8;
      WaveStream.Write(i, 4);
      i := i - $24;
      WaveStream.Position := 40;
      WaveStream.Write(i, 4);
      WaveStream.Position := 0;
      // create a stream from the recorded data
      chan := BASS_StreamCreateFile(True, WaveStream.Memory, 0, WaveStream.Size, 0);
      if chan &lt;&gt; 0 then
      begin
          // enable "Play" &amp; "Save" buttons
          bPlay.Enabled := True;
          bSave.Enabled := True;
      end
      else
          MessageDlg('Error creating stream from recorded data!', mtError, [mbOk], 0);
      if SaveDialog.Execute then
         WaveStream.SaveToFile(SaveDialog.FileName);
  
  end;
  </code></pre>