Generate square / sinusoidal waves lazarus / 21-General ---------------------------------------------------------------------- Ramses · 2021-01-11 23:53 Hi all, im new to this forum, and im a beginner with the raspberry pi (but i have programmed with lazarus/delphi till many years on windows system) i need some help to generate square and sinusoidal waves Im using a Raspbery Pi 3 and i dont figure out how to generate my wave on this board  ... I want to make a very simple program with 2 button: Button1: generate a 30hz squared wave Button2: generate a 55hz sinusoidal wave Thats it! What is the easiest way to generate those wave? Witch pin is the easiest to use? Thanks for your help ---------------------------------------------------------------------- jamie · 2021-01-12 00:22 If it were windows I could show you how to do that using the sound system, at lease the Sine wave that is. But you have a little mini there I am sure it does have some sort of facility for audio out.. But if you have a DAC (Digital To Analog Converter) you can gain access to as an output pin, I don't think 55Hz is too fast of a change for it..  You can create the Sine wave using time sliced (timer) broken into steps (PCM) to set the output value of the DAC pin. Does this sound like something you understand ? ---------------------------------------------------------------------- Ramses · 2021-01-12 00:45 Sure i understand, the problem is that i not familiar with raspberry board.. on windows/delphi is a way more easier! i dont know if its possible to do that only by code, or with a special library, or if i have to use an external circuit to generate wave.. this board is new to me.. The only thing i need is a code exemple that generate square/sinusoidal wave... (if its possible to do this only by code ) ---------------------------------------------------------------------- jamie · 2021-01-12 00:58 I've taken AVR and PIC chips and done that using PWM (Pulse width modulation) through a Resistor and CAP network into a buffer OP-Amp to generate the SINE wave by calculating the a PWM at high frequency rate so to create a Analog output and set the output to represent a sine wave.  I am sure libs exist to do this but the pi is a little more than just a micro chip  They made a few different Pi's which processor do you have ? ---------------------------------------------------------------------- Fred vS · 2021-01-12 01:17 Hello. There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS. https://github.com/fredvs/alsa_sound. ---------------------------------------------------------------------- Ramses · 2021-01-12 01:25 Quote from: jamie on January 12, 2021, 01:58:19 amI've taken AVR and PIC chips and done that using PWM (Pulse width modulation) through a Resistor and CAP network into a buffer OP-Amp to generate the SINE wave by calculating the a PWM at high frequency rate so to create a Analog output and set the output to represent a sine wave.  I am sure libs exist to do this but the pi is a little more than just a micro chip  They made a few different Pi's which processor do you have ? i use Raspberry Pi 3 Model B(CPU: 4× ARM Cortex-A53, 1.2GHz) ---------------------------------------------------------------------- Ramses · 2021-01-12 01:33 Quote from: Fred vS on January 12, 2021, 02:17:32 amHello. There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS. https://github.com/fredvs/alsa_sound. Thanks for that, so if it use the sound, the output is probably the sound card output? That look good, i wil ltry to install this `alsa sound` to see if it work for what i need (but im not very familiar with installation of this on linux...) ---------------------------------------------------------------------- Fred vS · 2021-01-12 02:09 Quote from: Ramses on January 12, 2021, 02:33:08 amQuote from: Fred vS on January 12, 2021, 02:17:32 amHello. There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS. https://github.com/fredvs/alsa_sound. Thanks for that, so if it use the sound, the output is probably the sound card output? That look good, i wil ltry to install this `alsa sound` to see if it work for what i need (but im not very familiar with installation of this on linux...) If you want to produce a sine-wave between 20 Hz and 18.000 HZ you may "visualize" it with your ears and so the easier is with the build-in sound card output of your Rpi (via the output mini jack or via the output hdmi). libasound.so is the low level audio interface of most Linux distro, included Raspbian OS. ---------------------------------------------------------------------- Ramses · 2021-01-12 03:16 Quote from: Fred vS on January 12, 2021, 03:09:47 amQuote from: Ramses on January 12, 2021, 02:33:08 amQuote from: Fred vS on January 12, 2021, 02:17:32 amHello. There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS. https://github.com/fredvs/alsa_sound. Thanks for that, so if it use the sound, the output is probably the sound card output? That look good, i wil ltry to install this `alsa sound` to see if it work for what i need (but im not very familiar with installation of this on linux...) If you want to produce a sine-wave between 20 Hz and 18.000 HZ you may "visualize" it with your ears and so the easier is with the build-in sound card output of your Rpi (via the output mini jack or via the output hdmi). libasound.so is the low level audio interface of most Linux distro, included Raspbian OS. alright, so did you have an simple exemple code that i can put in my button code to generate a 35hz tone for exemple? ---------------------------------------------------------------------- MarkMLl · 2021-01-12 09:17 What are you expecting it to come out of? Are you looking for an audio signal, a frequency on one of the GPIO pins, or what? MarkMLl ---------------------------------------------------------------------- MarkMLl · 2021-01-12 09:52 Quote from: jamie on January 12, 2021, 01:58:19 amI am sure libs exist to do this but the pi is a little more than just a micro chip A Raspberry Pi is a fully-fledged Linux-based computer, rather more than "just a micro chip". What that means in practice is that it's not possible to ackle signals directly, you've got to go through the OS hence https://wiki.freepascal.org/Raspberry_Pi#Native_hardware_access plus other links on that page. One detail is that there are two different kernel APIs for accessing the GPIO pins: one goes through /sys and the other through /dev. I've played with the former a bit and I think it's possible to get a few 10s of Hz out of it, the latter should be more efficient. I don't know whether the underlying chip (or the OS support for it) has a counter/timer circuit which can generate a square wave directly. MarkMLl ---------------------------------------------------------------------- Ramses · 2021-01-12 13:38 Quote from: MarkMLl on January 12, 2021, 10:17:14 amWhat are you expecting it to come out of? Are you looking for an audio signal, a frequency on one of the GPIO pins, or what? MarkMLl I think that an audio signal is better for me, but a frequency on one pin is ok too, as long as i can read the signasl with an oscilloscope, its ok for me ---------------------------------------------------------------------- MarkMLl · 2021-01-12 13:52 Quote from: Ramses on January 12, 2021, 02:38:04 pmI think that an audio signal is better for me, but a frequency on one pin is ok too, as long as i can read the signasl with an oscilloscope, its ok for me In that case look at the stuff that Fred's given you, alternatively my equivalent https://github.com/MarkMLl/asound Broadly speaking both of those came from the thread at https://forum.l azarus.freepascal.org/index.php/topic,49502.msg383415.html#msg383415 My code's got a lot of extra verbosity in it since it's intended to be usable with both static an dynamic linkage, but I think you'll find the specific example at https://github.com/MarkMLl/asound/blob/main/examples/alsapcmdemo.pas useful which is applicable to any ALSA interface. I'd not recommend ackling a GPIO bit since it will be fairly erratic unless you have an RTOS (Real-Time Operating System) which Linux isn't. MarkMLl ---------------------------------------------------------------------- avra · 2021-01-13 11:07 Delphi code for generating frequency based wave forms can be found here: http://delphiforfun.org/programs/SoundGen.htm You would need to adapt it first to Lazarus, then to Linux, and then to Pi. ---------------------------------------------------------------------- Ramses · 2021-01-13 15:51 Quote from: MarkMLl on January 12, 2021, 02:52:31 pmBroadly speaking both of those came from the thread at https://forum.lazarus.freepascal .org/index.php/topic,49502.msg383415.html#msg383415 My code's got a lot of extra verbosity in it since it's intended to be usable with both static an dynamic linkage, but I think you'll find the specific example at https://github.com/MarkMLl/asound/blob/main/examples/alsapcmdemo.pas useful which is applicable to any ALSA interface. Thanks for that, it worked perfectly for sinusoidal wave, i can set the frequency and duration, so it is exactly what i needed!  Now i have to produce square wave too, any idea how to do that? ---------------------------------------------------------------------- MarkMLl · 2021-01-13 15:53 Quote from: Ramses on January 13, 2021, 04:51:33 pmNow i have to produce square wave too, any idea how to do that? Change the half-wave generation to a simple level rather than a sum of sin() functions. The other half-wave will (obviously) "just work". MarkMLl ---------------------------------------------------------------------- Fred vS · 2021-01-13 16:25 > Now i have to produce square wave too, any idea how to do that? Hello. In the link I give you previously: https://github.com/fredvs/alsa_sound. In /alsa_sound/src/alsa_sound.pas: 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 ALSAbeep(frequency, duration, volume: cint; warble: Boolean;WaveType: cint; CloseLib : boolean): Boolean; // WaveType: 0=sine, 1=square, 2=tooth Just take a look at "WaveType" parameter ( 0=sine, 1=square, 2=tooth (or triangle)) and the code about it. 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 SetWave (var SA: TAr360; WaveType, Volume: integer); inline;var i : integer;begin    if NOT (waveType in [0..2]) then waveType := 0;        case WaveType of      0: begin          for I := 0 to 359 do        SA[I] := round(sin(pi * I / 180.0) * volume);  // create sine wave pattern         end;       1: begin          for I := 0 to 359 do        if I < 180 then SA[i] := +1*volume else  SA[i] := -1* volume;//  sqare wave         end;       2: begin          for I := 0 to 359 do          SA[i] := (round((360 - i)/180) -1)*volume;   //   saw tooth (triangle) wave         end;      end;end; Fre;D ---------------------------------------------------------------------- Ramses · 2021-01-15 03:47 Quote from: Fred vS on January 13, 2021, 05:25:22 pm Just take a look at "WaveType" parameter ( 0=sine, 1=square, 2=tooth (or triangle)) and the code about it. Thanks for that! i have try it today, and it is very easy to use! thats exactly what i needed! for now it seem to work great, so next week i will test it with an oscilloscope to make sure everything is ok, thats the next step. So thanks again for your help! ---------------------------------------------------------------------- Ramses · 2021-01-15 20:50 One more question: when i use Alsabeep, My program freeze completely while the sound is playing, and i can not do anything or press any button while sound is playing.. is there any way to prevent program from freezing? here is the code i use 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 ALSAbeep(40, 10000, 100, true,0, true); ---------------------------------------------------------------------- MarkMLl · 2021-01-15 21:05 Well, you don't show what the code /really/ is, only how you're calling it, and since it's obviously not something I wrote I can only make a couple of general suggestions. The "right" way would probably be to put it in a thread, I don't know whether ALSA provides any equivalent functionality. Alternatively you could insert an Application.ProcessMessages into the loop, but (a) protect it in case you later move the code to a thread, (b) expect it to mess up latency, and (c) if you call it too often it will /really/ mess up latency... you probably don't need it more than 10 times a second. MarkMLl ---------------------------------------------------------------------- Fred vS · 2021-01-15 22:31 Quote from: Ramses on January 15, 2021, 09:50:02 pmOne more question: when i use Alsabeep, My program freeze completely while the sound is playing, and i can not do anything or press any button while sound is playing.. is there any way to prevent program from freezing? here is the code i use 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 ALSAbeep(40, 10000, 100, true,0, true); Hello. Like MarkMLI explained, you need to run ALSAbeep() inside a other thread if you want that your main thread still runs while the sound is playing, Alternatively you may use a other library for that, like uos: https://github.com/fredvs/uos Each player is a independent thread. Take a look at example consolesynth.pas: https://github.com/fredvs/uos/blob/master/examples/consolesynth.pas ---------------------------------------------------------------------- Ramses · 2021-01-17 14:35 Quote from: Fred vS on January 15, 2021, 11:31:22 pmLike MarkMLI explained, you need to run ALSAbeep() inside a other thread if you want that your main thread still runs while the sound is playing, Alternatively you may use a other library for that, like uos: https://github.com/fredvs/uos Well, i have put a speaker on my raspberry, and i tried the library you told me, and i never figure out how to use it! i never get any sound on my speaker..I dont figure out how to use this library to generate a sound in my button... So i have try to run ALSAbeep inside a thread, but after all day long of trying, iḿ still not able to do that.. i have read a LOT of documentation but it still not working... I dont figure out how to do that! (i know i'm a beginer in this) I show you a very basic code that work, maybe you can tell me how to modify my code to make it work in a thread? or how to use the new library to generate sound when pressing my button? 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,alsa_sound; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  ALSAbeep(40, 10000, 100, true,0, true);end; end. ---------------------------------------------------------------------- Fred vS · 2021-01-17 14:54 Quote from: Ramses on January 17, 2021, 03:35:11 pmQuote from: Fred vS on January 15, 2021, 11:31:22 pmLike MarkMLI explained, you need to run ALSAbeep() inside a other thread if you want that your main thread still runs while the sound is playing, Alternatively you may use a other library for that, like uos: https://github.com/fredvs/uos Well, i have put a speaker on my raspberry, and i tried the library you told me, and i never figure out how to use it! i never get any sound on my speaker..I dont figure out how to use this library to generate a sound in my button... Hello. Here how to use uos: 1) Go on site: https://github.com/fredvs/uos 2) Click on the green button [ Code ] + Download ZIP. 3) Unzip the downloaded file somewhere. 4) Run Lazarus - Open Project 5) Choose in /uos/examples/ a demo project, for example  /uos/examples/simpleplayer.lpi (or in your case consolesynth.lpi) 6) Compile and run it using Lazarus. 7) Check the code to understand how it works. Fre;D ---------------------------------------------------------------------- MarkMLl · 2021-01-17 16:02 Quote from: Ramses on January 17, 2021, 03:35:11 pmnot working... I dont figure out how to do that! (i know i'm a beginer in this) Not working HOW????? Where's your example code????? Sorry, asking us to fix a not-quite-right attempt is one thing, but asking us to do it from scratch is something else entirely. Did you try putting Application.ProcessMessages in your loop which was the fallback suggestion? What happened? MarkMLl ---------------------------------------------------------------------- Ramses · 2021-01-17 17:11 Quote from: MarkMLl on January 17, 2021, 05:02:35 pmNot working HOW????? Where's your example code????? Did you try putting Application.ProcessMessages in your loop which was the fallback suggestion? What happened? i have not posted any exemple code, because i tried to follow like 5 or 6 tutorial about "multithreading" that i found on the web, so i decided to not post all those "test" i made..... For your last question, yes i have try putting Application.ProcessMessages: for example, have try to split the 10 second sound in 10 X 1 second song separate by "application.processmessages", but on the speaker the sound cut 10 time ( i hear a silence each time application.processmessages is called) ... i want to generate a wave with no "silence" in it.. now im looking at the library UOS (from the post of "Fred vS"), when i compile it i hear a sound in my speaker, but there is no graphical user interface, and i want to generate my wave by pressing a button, so iḿ actually trying to copy the code from "consolesynth" inside my graphical program, thats what iḿ working on... ---------------------------------------------------------------------- Fred vS · 2021-01-17 17:20 Quote from: Ramses on January 17, 2021, 06:11:34 pmnow im looking at the library UOS (from the post of "Fred vS"), when i compile it i hear a sound in my speaker, but there is no graphical user interface, and i want to generate my wave by pressing a button, so iḿ actually trying to copy the code from "consolesynth" inside my graphical program, thats what iḿ working on... Hello. All the "consoleXXX.lpi" demos are console applications, no GUI applications, all others are GUI demos.. Did you try, like proposed in previous post, SimplePlayer.lpi demo, it is a graphical application using LCL (Lazarus component) ? ---------------------------------------------------------------------- Ramses · 2021-01-17 17:50 Quote from: Fred vS on January 17, 2021, 06:20:36 pmDid you try, like proposed in previous post, SimplePlayer.lpi demo, it is a graphical application using LCL (Lazarus component) ? yes i tried it, but for what i understand, this demo is made to play a song from my computer, and i dont figure out how to generate sin/square wave from it.. so not really what i need.. or maybe i have not understand correctly how to use it? ---------------------------------------------------------------------- Fred vS · 2021-01-17 18:05 Quote from: Ramses on January 17, 2021, 06:50:43 pmQuote from: Fred vS on January 17, 2021, 06:20:36 pmDid you try, like proposed in previous post, SimplePlayer.lpi demo, it is a graphical application using LCL (Lazarus component) ? yes i tried it, but for what i understand, this demo is made to play a song from my computer, and i dont figure out how to generate sin/square wave from it.. so not really what i need.. or maybe i have not understand correctly how to use it? Ok, last try. In the graghic application that you have done, in the procedure Button1Click, insert the code of procedure TuosConsole.ConsolePlay; from uos demo consoleplay.pas inside your Button1Click.  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 program consolesynth;... procedure TuosConsole.ConsolePlay;  begin  ...  // Copy all the code inside  end; And paste into your project.  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 TForm1.Button1Click(Sender: TObject);begin// Insert the code of procedure TuosConsole.ConsolePlay;end; ---------------------------------------------------------------------- MarkMLl · 2021-01-17 18:17 Quote from: Ramses on January 17, 2021, 06:11:34 pmQuote from: MarkMLl on January 17, 2021, 05:02:35 pmNot working HOW????? Where's your example code????? Did you try putting Application.ProcessMessages in your loop which was the fallback suggestion? What happened? i have not posted any exemple code, because i tried to follow like 5 or 6 tutorial about "multithreading" that i found on the web, so i decided to not post all those "test" i made..... FFS man, I'm trying to /help/ here: you've said you've tried something but it didn't work for you so SHOW US WHAT YOU TRIED! Post the whole project with your attempt at threads and we'll try to sort you out. QuoteFor your last question, yes i have try putting Application.ProcessMessages: for example, have try to split the 10 second sound in 10 X 1 second song separate by "application.processmessages", but on the speaker the sound cut 10 time ( i hear a silence each time application.processmessages is called) ... i want to generate a wave with no "silence" in it.. When I said "you probably don't need it more than 10 times a second" I meant "don't call it every millisecond". I definitely didn't mean "call it every second", since apart from anything else if you do that then your GUI will only come to life once a second. What's more if you look at the example I pointed you at in my Github repository it EXPLICITLY says 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 (* Output the buffer, retrying/recovering as necessary. If this is irregular or *)(* results in error messages, try increasing the value in the latency parameter *)(* to snd_pcm_set_params() above.                                             *)  I don't put comments into code purely for the fun of it... Somewhat later: I've added an APM to the outer loop, which was trivial since it was only running at 20Hz. I've heard no jitter etc. so far. MarkMLl ---------------------------------------------------------------------- Ramses · 2021-01-17 18:51 Quote from: Fred vS on January 17, 2021, 07:05:24 pmIn the graghic application that you have done, in the procedure Button1Click, insert the code of procedure TuosConsole.ConsolePlay; from uos demo consoleplay.pas Ok, i have do that, now i got an error when i compile QuoteThreading has been used before cthreads was initialized. Make cthreads one of the first units in your uses clause. and my code now look like that: 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 unit Unit1; {$mode objfpc}{$H+} interface uses {$IFDEF UNIX}  cthreads, {$ENDIF}  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  uos_flat; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1;         res, x, y, z: integer;  ordir, opath, SoundFilename, PA_FileName, PC_FileName, SF_FileName, MP_FileName: string;  PlayerIndex1, InputIndex1, OutputIndex1: integer; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin            ordir := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0)));  {$IFDEF Windows}     {$if defined(cpu64)}    PA_FileName := ordir + 'lib\Windows\64bit\LibPortaudio-64.dll';    SF_FileName := ordir + 'lib\Windows\64bit\LibSndFile-64.dll';     {$else}    PA_FileName := ordir + 'lib\Windows\32bit\LibPortaudio-32.dll';    SF_FileName := ordir + 'lib\Windows\32bit\LibSndFile-32.dll';     {$endif}    SoundFilename := ordir + 'sound\test.ogg'; {$ENDIF}      {$if defined(CPUAMD64) and defined(linux) }  SF_FileName := ordir + 'lib/Linux/64bit/LibSndFile-64.so';  PA_FileName := ordir + 'lib/Linux/64bit/LibPortaudio-64.so';    SoundFilename := ordir + 'sound/test.ogg';   {$ENDIF}    {$if defined(cpu86) and defined(linux)}    PA_FileName := ordir + 'lib/Linux/32bit/LibPortaudio-32.so';    SF_FileName := ordir + 'lib/Linux/32bit/LibSndFile-32.so';   SoundFilename := ordir + 'sound/test.ogg';   {$ENDIF}   {$if defined(linux) and defined(cpuaarch64)}  PA_FileName := ordir + 'lib/Linux/aarch64_raspberrypi/libportaudio_aarch64.so';  SF_FileName := ordir + 'lib/Linux/aarch64_raspberrypi/libsndfile_aarch64.so';  SoundFilename := ordir + 'sound/test.ogg';  {$ENDIF}   {$if defined(linux) and defined(cpuarm)}    PA_FileName := ordir + 'lib/Linux/arm_raspberrypi/libportaudio-arm.so';    SF_FileName := ordir + ordir + 'lib/Linux/arm_raspberrypi/libsndfile-arm.so';      SoundFilename := ordir + 'sound/test.ogg';   {$ENDIF}  {$IFDEF freebsd}    {$if defined(cpu64)}    PA_FileName := ordir + 'lib/FreeBSD/64bit/libportaudio-64.so';    SF_FileName := ordir + 'lib/FreeBSD/64bit/libsndfile-64.so';    {$else}    PA_FileName := ordir + 'lib/FreeBSD/32bit/libportaudio-32.so';    SF_FileName := ordir + 'lib/FreeBSD/32bit/libsndfile-32.so';    {$endif}    SoundFilename := ordir + 'sound/test.ogg'; {$ENDIF}  {$IFDEF Darwin}  {$IFDEF CPU32}    opath := ordir;    opath := copy(opath, 1, Pos('/UOS', opath) - 1);    PA_FileName := opath + '/lib/Mac/32bit/LibPortaudio-32.dylib';    SF_FileName := opath + '/lib/Mac/32bit/LibSndFile-32.dylib';    SoundFilename := ordir + '/sound/test.ogg';    {$ENDIF}    {$IFDEF CPU64}    opath := ordir;  opath := copy(opath, 1, Pos('/UOS', opath) - 1);    PA_FileName := opath + '/lib/Mac/64bit/LibPortaudio-64.dylib';    SF_FileName := opath + '/lib/Mac/64bit/LibSndFile-64.dylib';    SoundFilename := ordir + '/sound/test.ogg';    {$ENDIF} {$ENDIF}     // Load the libraries    // function uos_loadlib(PortAudioFileName, SndFileFileName, Mpg123FileName, Mp4ffFileName, FaadFileName,  opusfilefilename: PChar) : LongInt;     res := uos_LoadLib(PChar(PA_FileName), PChar(SF_FileName), nil, nil, nil, nil);     writeln;    if res = 0 then      writeln('Libraries are loaded.')    else      writeln('Libraries did not load.');     if res = 0 then    begin      writeln();       //    writeln('Libraries version: '+ uos_GetInfoLibraries());       //// Create the player.    //// PlayerIndex : from 0 to what your computer can do !      //// If PlayerIndex exists already, it will be overwriten...       PlayerIndex1 := 0;       if uos_CreatePlayer(PlayerIndex1) then      begin         //// add a Input from audio-file with default parameters        //////////// PlayerIndex : Index of a existing Player        ////////// FileName : filename of audio file        //  result : -1 nothing created, otherwise Input Index in array         InputIndex1 := uos_AddFromFile(PlayerIndex1, PChar((SoundFilename)), -1, -1, -1);          writeln('InputIndex1 = ' + IntToStr(InputIndex1));         if InputIndex1 > -1 then        begin          //// add a Output into device with default parameters        //////////// PlayerIndex : Index of a existing Player          //  result : -1 nothing created, otherwise Output Index in array        {$if defined(cpuarm) or defined(cpuaarch64)}  // need a lower latency        OutputIndex1 := uos_AddIntoDevOut(PlayerIndex1, -1, 0.3, -1, -1, -1, -1, -1) ;       {$else}         //OutputIndex1 := uos_AddIntoDevOut(PlayerIndex1);          OutputIndex1 := uos_AddIntoDevOut(PlayerIndex1, -1, -1, -1, -1, -1, -1, -1);       {$endif}           writeln('OutputIndex1 = ' + IntToStr(OutputIndex1));         //   uos_AddIntoFile(PlayerIndex1,pchar('/home/fred/mytest.ogg'), -1, -1, -1, -1, 3);           if OutputIndex1 > -1 then          begin           /////// everything is ready, here we are, lets play it...           uos_Play(PlayerIndex1);             sleep(1000);            writeln;            writeln('Title: ' + uos_InputGetTagTitle(PlayerIndex1, InputIndex1));            sleep(1500);            writeln();            writeln('Artist: ' + uos_InputGetTagArtist(PlayerIndex1, InputIndex1));            writeln;             sleep(2000);          end;        end;      end;    end; end; end. So my "cthreads" is the first in "uses", but it told me "Make cthreads one of the first units in your uses clause." I dont see what iḿ doing wrong.. ---------------------------------------------------------------------- Fred vS · 2021-01-17 19:01 Ok, very last try. First copy the directory /uos/examples/lib and paste into the directory of your project. Paste also the units that are in /uos/src/ into the directory of your project. For this project you will need only uos.pas, uos_flat.pas and uos_portaudio.pas. Then in your form 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 unit yourfromunit; {$mode objfpc}{$H+}interface uses    ... // add all the other units needed for your project   uos_flat;   ...  type  { TForm1 }  TForm1 = class(TForm)     // all included Formcreate and Formdestroy   .... var  Form1: TForm1;    ... // all your variables     // this for uos     res: integer;      PlayerIndex1, inindex1: integer; implementation ... // all your methods procedure TForm1.FormCreate(Sender: TObject);var  PA_FileName: string;begin// load the library  PA_FileName := './lib/Linux/arm_raspberrypi/libportaudio-arm.so';  res := uos_LoadLib(PChar(PA_FileName), nil, nil, nil, nil, nil);end;     ... procedure TForm1.Button1Click(Sender: TObject);begin  if res = 0 then    begin      //// Create the player.      //// PlayerIndex : from 0 to what your computer can do !      //// If PlayerIndex exists already, it will be overwriten...      PlayerIndex1 := 0;      inindex1     := -1;       if uos_CreatePlayer(PlayerIndex1) then      inindex1 :=         uos_AddFromSynth(PlayerIndex1, -1, -1, -1, 420, 420, -1, -1, -1, -1, -1, 0, -1, -1, -1);       if uos_AddIntoDevOut(PlayerIndex1,-1,0.3,-1,-1, 0,-1,-1) > - 1 then       /////// everything is ready, here we are, lets play it...        uos_Play(PlayerIndex1);   end; end; procedure TForm1.FormDestroy(Sender: TObject);   begin   uos_free();  end;... Please check in uos source the parameters of each method. ---------------------------------------------------------------------- Ramses · 2021-01-17 19:49 Ok, i have do everything you told me, the program compile and run, but when i press the buton i got QuoteError: Project raised exception class 'External:SIGSEGV'. My code now is: 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,uos_flat; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure FormCreate(Sender: TObject);    procedure Button1Click(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private   public   end; var  Form1: TForm1;     res: integer;     PlayerIndex1, inindex1: integer; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  PA_FileName: string;begin// load the library  PA_FileName := './lib/Linux/arm_raspberrypi/libportaudio-arm.so';  res := uos_LoadLib(PChar(PA_FileName), nil, nil, nil, nil, nil);end;   procedure TForm1.Button1Click(Sender: TObject);begin  if res = 0 then    begin      //// Create the player.      //// PlayerIndex : from 0 to what your computer can do !      //// If PlayerIndex exists already, it will be overwriten...      PlayerIndex1 := 0;      inindex1     := -1;       if uos_CreatePlayer(PlayerIndex1) then        inindex1 :=         uos_AddFromSynth(PlayerIndex1, -1, -1, -1, 420, 420, -1, -1, -1, -1, -1, 0, -1, -1, -1);       if uos_AddIntoDevOut(PlayerIndex1,-1,0.3,-1,-1, 0,-1,-1) > - 1 then       /////// everything is ready, here we are, lets play it...        uos_Play(PlayerIndex1);   end; end; procedure TForm1.FormDestroy(Sender: TObject);  begin   uos_free();  end; end. ---------------------------------------------------------------------- Fred vS · 2021-01-17 20:26 Hello. Hum, it should work. What is the code of your main program, did you add cthreads unit like this ?: 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 program project1; {$mode objfpc}{$H+} uses  cthreads, // THIS ADDED (remove the if ... end if there are added)  Interfaces,   Forms, Unit1  { you can add units after this }; {$R *.res} begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;  Application.CreateForm(TForm1, Form1);  Application.Run;end. ---------------------------------------------------------------------- Ramses · 2021-01-17 20:46 Quote from: Fred vS on January 17, 2021, 09:26:19 pmWhat is the code of your main program, did you add cthreads unit like this ?: i removed the "if...end" so now it is exactly like your exemple, but still get the exact same error: "Error: Project raised exception class 'External:SIGSEGV'" Here is my main program: 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 program project1; {$mode objfpc}{$H+} uses  cthreads,  Interfaces, // this includes the LCL widgetset  Forms, Unit1  { you can add units after this }; {$R *.res} begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;  Application.CreateForm(TForm1, Form1);  Application.Run;end. I tried to found what section of the code raise the error, and the error appear at this line: "uos_CreatePlayer(PlayerIndex1)" ---------------------------------------------------------------------- Fred vS · 2021-01-17 20:57 Hello. Could you try the project in attachment? Just unzip it and load project1.lpi + complile + run. Here it works. ---------------------------------------------------------------------- Ramses · 2021-01-17 21:14 Quote from: Fred vS on January 17, 2021, 09:57:32 pmJust unzip it and load project1.lpi + complile + run. Worked perfectly here... dont know why my project doesnt work, maybe i forgot to copy some files?? anyway thanks a lot for that, now because your project work perfectly, i can start building what i want on this code! it look like my issue is finally fixed now! Thanks for that, and how mutch for the trouble? ---------------------------------------------------------------------- Fred vS · 2021-01-17 21:18 Hello Ramses. Nice that you make it work.  Fre;D