Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recording input stream to wav directly #14

Closed
akatser opened this issue Dec 2, 2016 · 10 comments
Closed

Recording input stream to wav directly #14

akatser opened this issue Dec 2, 2016 · 10 comments

Comments

@akatser
Copy link

akatser commented Dec 2, 2016

Hi, SimpleRecorder example writes input stream in memory and saves wav file after clicking stop buttion. How to write input strem directly to wave file ?

@akatser akatser changed the title Record wav file Recording input stream to wav directly Dec 2, 2016
@fredvs
Copy link
Owner

fredvs commented Dec 2, 2016

Hello.

What do you want to do ?
Wav-file needs a special wav header at begin of file, with some infos like resolution, sample-rate, duration,...

Fre;D

@akatser
Copy link
Author

akatser commented Dec 3, 2016

I want to save input in wave file for a long time. In example SimpleRecorder input stream writes to memory and after stop saves to file. So, recording of one hour uses lots of memory. I want to write buffer to wave file directly.
Wave header is not problem :)

@fredvs
Copy link
Owner

fredvs commented Dec 3, 2016

Hello.
When recording, the buffer is saved in a temporally file.
Recording hours uses a big temporally file but not lot of RAM.

@akatser
Copy link
Author

akatser commented Dec 5, 2016

Hi, please check my code:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  uos_flat, LazUTF8;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ComboBox1: TComboBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  s1,s2 : string;
  PlayerIndex1: cardinal;
  In1Index : integer;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var x:integer;
begin
  {$IFDEF Windows}
{$if defined(cpu64)}
s1 := application.Location + 'lib\Windows\64bit\LibPortaudio-64.dll';
s2 := application.Location + 'lib\Windows\64bit\LibSndFile-64.dll';
{$else}
s1 := application.Location + 'lib\Windows\32bit\LibPortaudio-32.dll';
s2 := application.Location + 'lib\Windows\32bit\LibSndFile-32.dll';
{$endif}
{$ENDIF}
 x:=0;
uos_LoadLib(Pchar(s1), pchar(s2), nil, nil, nil);
uos_GetInfoDevice();
while x < UOSDeviceCount do
    begin

      if   (uosDeviceInfos[x].ChannelsIn > 0) and (uosDeviceInfos[x].HostAPIName = 'MME') then
            ComboBox1.Items.Add(IntToStr(uosDeviceInfos[x].DeviceNum)+ '-'+  SysToUTF8(uosDeviceInfos[x].DeviceName));
      inc(X);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
PlayerIndex1 := 0;
  uos_CreatePlayer(PlayerIndex1);
  uos_addIntoFile(PlayerIndex1, Pchar('d:\temp\test.wav') ,44100,2,2,-1 );
  In1Index := uos_AddFromDevIn(PlayerIndex1, 2, -1, -1, -1,-1, 4096);
  uos_Play(PlayerIndex1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  uos_Stop(PlayerIndex1);
end;

end.

This is RAM usage after 12 min of recording :
logger2

This is RAM usage after stop button click :
logger3

@fredvs
Copy link
Owner

fredvs commented Dec 5, 2016

Indeed, lot of ram are used.

Does the ram used increase with time or is it stable to 1,2 gigas ?

What if you use cmem (memory manager) instead ?

`unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  cmem, // C memory manager
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  uos_flat, LazUTF8;`

@akatser
Copy link
Author

akatser commented Dec 6, 2016

App can't comlile with cmem
image

Used ram increases with time when recording in on.

@fredvs
Copy link
Owner

fredvs commented Dec 7, 2016

cmem must be the first unit of the main code (program).
Tested SimpleRecorder on Windows 10 with cmem and no problem here.

Used ram increases with time when recording in on.

It is normal that recording uses lot of ram.
For latency it will try to use the most ram possible to access less time the disk.
The question was, is all the ram is used, does it still does recording ?
Fre;D

@akatser
Copy link
Author

akatser commented Dec 8, 2016

Hi, after one hour of recording (with cmem and without) programm crashes.

Error string is :
StreamOut[x].Data.Buffer[0], StreamIn[x2].Data.outframes * StreamIn[x2].Data.Channels * rat);

@fredvs
Copy link
Owner

fredvs commented Dec 15, 2016

Hello.

Could you try with last commit da316a2..5f11b62 ?

Thanks.

@fredvs
Copy link
Owner

fredvs commented Apr 3, 2017

Hello.

Thanks to Phoenix fixed bug in uos_AddIntoFile() --> for recording using TFileStream.
--> max length = Available free-space-disk.

Added also uos_AddIntoFileFromMem() --> --> for recording using TMemoryStream.
--> max length = Available RAM (but it could have a better latency).

All this in last commit: de823d5..af9530b.
Fre;D

@fredvs fredvs closed this as completed Apr 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants