Forum > General

fpwavwriter or creating a .wav file

(1/2) > >>

AnthonyTekatch:
I would like to create a .wav file from my own signal formula.

I could not find any documentation, examples, or tutorial on how to use fpwavwriter.pas from here: https://github.com/graemeg/freepascal/tree/master/packages/fcl-sound/src

or is there another documented method to create a .wav file ?

taazz:
something along the lines of

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function SaveToWav(const aFilename:String; const aBuffer; const aBufferSize; aBitsPerSample, aSampleRate, aChannelCount :Integer):Boolean;var  vTmp :TWavWriter;begin  Result := False;  vTmp := TWavWriter.Create;  try    vTmp.StoreToFile(aFilename);    vTmp.fmt.BitsPerSample := aBitsPerSample;    vTmp.fmt.SampleRate    := aSampleRate;    vTmp.fmt.Channels      := aChannelCount;    vTmp.WriteBuf(aBuffer, aBufferSize);    vTmp.FlushHeader;    Result := true;  finally    vTmp.Free;  end;end; There is a writeln in the writebuffer method that needs to be deleted to work properly on windows. ee

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TWavWriter.WriteBuf(var Buffer; BufferSize: Integer): Integer;var  sz: Integer;begin  //WriteLn('[TWavWriter.WriteBuf] BufferSize = ', BufferSize); <-- comment this line its a debug pollution.  Result := 0;  with fStream do begin    sz := Write(Buffer, BufferSize);    if sz < 0 then Exit;    Inc(Result, sz);  end;end; 

AnthonyTekatch:
Thank you.

What should the buffer look like?

I think it should be in bytes according to the fstream.write definition , but an aBitsPerSample of 16 or 32 messes things up. And, aBufferSize must be in bytes even if the wav data is 16 or 32 bits per sample.

Your example throws these errors in FPC 3.04:
Error: Can't assign values to const variable
Error: Incompatible type for arg no. 2: Got "<Formal type>", expected "LongInt"

I think the SaveToWav function parameter definitions should match the parameter types of the TWavWriter parameters as follows:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function SaveToWav(const aFilename:String; var aBuffer; aBufferSize:LongInt; aBitsPerSample, aSampleRate, aChannelCount :Integer):Boolean;

wp:
In the attachment, there's my wavegenerator. I did not know of the fpwavwriter those days and used my own wav routines. Also, fpwavwriter is not very helpful because it does not tell what the buffer should look like.

AnthonyTekatch:

--- Quote from: wp on March 27, 2018, 08:13:23 pm ---In the attachment, there's my wavegenerator. I did not know of the fpwavwriter those days and used my own wav routines. Also, fpwavwriter is not very helpful because it does not tell what the buffer should look like.

--- End quote ---

Ah, thank you for that!

It works, and is well commented. I will be able to use it.

Navigation

[0] Message Index

[#] Next page

Go to full version