[Solved] How to cut or avoid ending silence in sound recording? lazarus / 21-General ---------------------------------------------------------------------- cpicanco · 2024-02-09 09:09 I am trying to save some space in disc by cutting/avoiding silence at the end of sounds. First of all, to give some credits, I ported a substancial part of the following code to record sound from here https ://gist.github.com/andraantariksa/f5e6d848364b11a425625ec7fbbfc187. Now, the recording code has a buffer that has a fixed lenght, lets say, to record 15 seconds: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   MAX_RECORDING_SECONDS = 15;  LBytesPerSecond := FAudioSpec.freq * BytesPerSample;  GBufferByteSize := RECORDING_BUFFER_SECONDS * LBytesPerSecond;  GBufferByteMaxPosition := MAX_RECORDING_SECONDS * LBytesPerSecond;  GetMem(GPRecordingBuffer, GBufferByteSize); Before starting recording, I am filling the buffer with zeros: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FillChar(GPRecordingBuffer^, GBufferByteSize, 0);   The recording code (a callback) runs inside a thread managed by SDL. It moves data from a stream into the buffer: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure AudioRecorderCallback(AUserdata: Pointer;  AStream: PUInt8; ALen: Integer); cdecl;begin  Move(AStream^, GPRecordingBuffer[GBufferBytePosition], ALen);  Inc(GBufferBytePosition, ALen);end;    1) Here, I have a question:  I am not sure if it would be possible to change the size of the buffer dinamically, as needed, instead of creating it with a fixed size from the beginning. Continuing. I have my own thread that controls when stop/start recording and doing some graphics: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAudioDevice.Execute;  procedure DoWork;  var    LShouldStop : Boolean;  begin    if not Terminated then begin      SDL_PauseAudioDevice(FDeviceID, 0);      while not Terminated do begin        EnterCriticalSection(ACriticalSection);        LShouldStop := FShouldStop;        LeaveCriticalSection(ACriticalSection);        SDL_LockAudioDevice(FDeviceID);        if (GBufferBytePosition > GBufferByteMaxPosition) or          LShouldStop then begin           SDL_PauseAudioDevice(FDeviceID, 1);          SDL_UnlockAudioDevice(FDeviceID);          Break;        end;        SDL_UnlockAudioDevice(FDeviceID);        Sleep(DELTA_TIME);        Queue(@DoInvalidate);        //Log(ClassName+':'+ (GetTickCount64 - First).ToString+ ':'+ GBufferBytePosition.ToString);      end;      Queue(@BufferFinished);    end;  end;begin  NameThreadForDebugging(ClassName);  FRTLEventMainThread := RTLEventCreate;  while not Terminated do begin    //Log(ClassName+': Waiting for main ...');    RTLEventWaitFor(FRTLEventMainThread);    //Log(ClassName+': Working ...');    DoWork;  end;  Log(ClassName+': Terminated ...');end; Finally, I am saving the buffer using the fpwavwrite unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAudioRecorderComponent.SaveToFile(AFilename: string);var  LWavWriter : TWavWriter;begin  LWavWriter := TWavWriter.Create;  try    if LWavWriter.StoreToFile(AFileName+'.wav') then begin      with LWavWriter, FAudioSpec do begin        fmt.Channels         := channels;        fmt.SampleRate       := freq;        fmt.BitsPerSample    := 16;        fmt.ByteRate         :=          fmt.SampleRate * fmt.Channels * fmt.BitsPerSample div 8;        fmt.BlockAlign       := fmt.Channels * fmt.BitsPerSample div 8;      end;      LWavWriter.WriteBuf(GPRecordingBuffer^, GBufferByteSize);    LWavWriter.FlushHeader;    end else begin      Print('Error creating WAV file: ' + AFileName);    end;  finally    LWavWriter.Free;  end;end; (2) Here I have a second question: Some time ago, I wrote the following function to check if the buffer was written: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TAudioRecorderComponent.HasRecording: Boolean;  function EmptyRecording : Boolean;  var    i : integer = 0;    Zeros : array of Byte = nil;  begin    Result := True;    SetLength(Zeros, GBufferByteSize);    FillChar(Zeros[0], GBufferByteSize, 0);    while (i < GBufferByteMaxPosition) and Result do begin      if not CompareMem(GPRecordingBuffer, @Zeros[0], GBufferByteSize) then        Result := False;      Inc(i, GBufferByteSize);    end;  end;begin  Result := not EmptyRecording;end; But I am really not sure if there is a better way of doing it. As I have no clue how to increase the size of the buffer dinamically, my idea now is to cut all silence looping the buffer in reverse order, from end to start. So, what do you think? ---------------------------------------------------------------------- cpicanco · 2024-02-10 20:37 For what I want, cutting end silence is simple as: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   function GetBufferByteSize : UInt32;  var    i: Integer;  begin    for i := GBufferByteMaxPosition - 1 downto 0 do begin      if GPRecordingBuffer[i] > 0 then      begin        Result := i + 1; // Update the buffer size        Break;      end;    end;  end; // ... LWavWriter.WriteBuf(GPRecordingBuffer^, GetBufferByteSize {GBufferByteSize});