Recent

Author Topic: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event  (Read 19269 times)

fauri

  • New Member
  • *
  • Posts: 15
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #15 on: February 21, 2017, 11:04:37 pm »
I used in the OnDestroy event, but eventually a "sisgrv" error occurs. So I chose not to use it any more. Regardless, I think uos_Play should release memory as soon as it finishes playing the sound.

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #16 on: February 21, 2017, 11:50:55 pm »
Quote
I think uos_Play should release memory as soon as it finishes playing the sound.

Yes, but does not release the loaded libraries, so uos_free is needed at end of application.
Please see SimplePlayer or other uos demos.

Sorry but I tested here SimplePlayer with Windows too and no increase of memory.
Nor any memory leak.

I think that I will need to see your code...

Fre;D
« Last Edit: February 21, 2017, 11:54:53 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

fauri

  • New Member
  • *
  • Posts: 15
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #17 on: February 22, 2017, 12:16:47 am »
I do not understand, what OnDestroy event ?
In Form's OnDestroy.
Sorry but I tested here SimplePlayer with Windows too and no increase of memory.
Nor any memory leak.
Insert a TTimer and paste the Play's Button code inside OnTimer Event. You'll see a gradual memory increase.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #18 on: February 22, 2017, 02:19:21 am »
hello,
for a drum machine , you can also try to use midi with soundfonts. For lazarus you can use Bass library and the bassmidi addon from Unseen.
In attachment, example of the synth example of Bassmidi converted to Lazarus from delphi wrapper.

Friendly, J.P


Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

fauri

  • New Member
  • *
  • Posts: 15
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #19 on: February 22, 2017, 04:10:46 am »
Hi, Jurassic Pork! It seems an amazing feature, and maybe I'll take a look in the future. But, in fact, I'm looking for a open source solution. Thanks anyway for your tip!

Cheers!

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #20 on: February 22, 2017, 12:22:48 pm »
Quote
Insert a TTimer and paste the Play's Button code inside OnTimer Event. You'll see a gradual memory increase.

Please, give me your code (or part of code), I can not reproduce your problem.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #21 on: February 22, 2017, 04:32:08 pm »
Hello.

I am back from the front.

OK, there is a (litle) problem while creating new thread (player).
With LCL, a few memory (some bytes) are used at each creation/destroy of a thread.
[EDIT] Fixed in last commit: 5fc7602..43c3fd2

If you agree, could you test this on SimplePlayer demo:

- Set the FramesCount to 512 in uos_AddFromFile() and uos_AddIntoDevOut().

- Add a TTimer : timer1, enabled to false and with interval of 50.

- Paste this for procedure timer1.onTimer:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   timer1.enabled := false;
  4.   uos_seek(PlayerIndex1, InputIndex1,0);
  5.   timer1.enabled := true;
  6. end;

and at end of procedure Button3Click():

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. begin
  3. ...
  4. uos_Play(PlayerIndex1);
  5.  
  6. timer1.Enabled:=true; // ==> Add this.
  7. ...

Here it works very fast with regular interval and you should be a excellent drummer to follow that beat.

Fre;D
« Last Edit: February 23, 2017, 11:52:41 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

fauri

  • New Member
  • *
  • Posts: 15
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #22 on: February 22, 2017, 07:25:25 pm »
I made a little new project with your informations, and it's look ok, but if the interval is bigger than the sound duration, uos_Seek doesn't work anymore.

This is a problem with a drum machine, with sounds being played at different times.

Taking advantage, with uos_AddFromFile inside a loop, would it be always seeking in the file system, or a kind of cache is created?

Thanks a lot for your assistence, Fred!

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #23 on: February 22, 2017, 07:34:28 pm »
Quote
but if the interval is bigger than the sound duration, uos_Seek doesn't work anymore.

Use for first play : uos_PlayNoFree(PlayerIndex1);
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. begin
  3. ...
  4. uos_PlayNoFree(PlayerIndex1); // Change this
  5.  
  6. timer1.Enabled:=true; // ==> Add this.
  7. ...
  8. ;

and in timer1.ontimer:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   timer1.enabled := false;
  4.   if uos_GetStatus(PlayerIndex1) = 0 then
  5.   uos_PlayNoFree(PlayerIndex1) // if player has stopped
  6.   else
  7.   uos_seek(PlayerIndex1, PlayerIndex1, InputIndex1); // if player is still playing
  8.   timer1.enabled := true;
  9.  end;    

[EDIT]
Quote
Taking advantage, with uos_AddFromFile inside a loop, would it be always seeking in the file system, ?

Yes, it will do: seeking if the audio-file exists, testing to see what kind of format, decoding the header, getting the meta-data, calculate the duration, and much more...


Fre;D
« Last Edit: February 22, 2017, 07:50:28 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

fauri

  • New Member
  • *
  • Posts: 15
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #24 on: February 22, 2017, 10:24:39 pm »
The problem is when more sounds need be played at same time. uos_PlayNoFree not works very well in this case.

I made two simple projects to show to you what's happens. I'll send to you in a PM.

Cheers!

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #25 on: February 23, 2017, 01:13:50 am »
hello,
another way to make a drum machine with lazarus is the tonegrid project :

Quote
What is this?
=============

Program is inspired by Tone Matrix as can be seen for example here
http://www.youtube.com/watch?v=OZFMpWKTAjY . On the Y axis we have
tones of a musical scale, on the X axis we have a timeline. In other
words it is a simplistic version of an audio sequencer. In my
implementation, the sound is synthesized as a single sine wave of
given frequency (fundamental frequency of a tone), which sounds very
minimalistic, or you can select to emit MIDI messages. On the other
hand it extends the basic idea by several features, like control over
scale mode, size, beats-per-minute etc.

The program has been written as an exercise in Free Pascal / Lazarus
and should be regarded as a pre-alpha release. Right now user needs to
build the project in Lazarus IDE himself. An installer will possibly
be added in the future.


Supported platforms:
====================

Program was developed entirely, and works in Linux environment. It has
been successfully compiled for 32bit Windows as well. It should be
possible to compile it also for Mac OS X (without MIDI support), as
Lazarus and OpenAL (see below) are both available for this platform,
and the code is completely cross-platform, however I did not have
access to Mac to test it.

i have tried this projet on windows 7 only with midi device. It is OK but to build a drum machine you need to  make some changes in the source code :
- in the original project the midi channel is always the first canal. To build a drum machine you need to select the channel 10 :
Quote
General MIDI Level 1 Percussion Key Map
On MIDI Channel 10, each MIDI Note number ("Key#") corresponds to a different drum sound, as shown below. GM-compatible instruments must have the sounds on the keys shown here. While many current instruments also have additional sounds above or below the range show here, and may even have additional "kits" with variations of these sounds, only these sounds are supported by General MIDI Level 1 devices.
Key#
Drum Sound : 35
Acoustic Bass Drum :36
Bass Drum 1  : 37
etc ...

in attachment, the program in action ,

Friendly, J.P



Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

fauri

  • New Member
  • *
  • Posts: 15
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #26 on: February 23, 2017, 06:23:41 am »
@JP
A interesting idea. But I liked your previously advice (about bass library)... any open source solution for midi loading and soundfonts usage? Maybe load into program just simple loops made in midi format for each rhythm...

EDIT: I found Allegro Library, which supports Wavetable synthesis format. As I understand, is possible convert and use sf2 files too.
« Last Edit: February 23, 2017, 10:25:21 am by fauri »

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #27 on: February 23, 2017, 01:47:46 pm »
@ Jurassic Pork: I think that I must follow your advice: using midi.

Dealing with files and a timer does not gives good result, the synchro is never perfect.

Hum, what midi open-source multi-os project do you prefer ?

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #28 on: February 23, 2017, 11:05:22 pm »
hello,
Hum, what midi open-source multi-os project do you prefer ?
Fre;D

project or library ?
what kind of project ?

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: UOS: Store a sound in memory and only use uos_Play inside a OnTimer event
« Reply #29 on: February 23, 2017, 11:48:12 pm »
Hello JP.

I would prefer a light library, with minimal dependencies.
If there are no Pascal header, no problem, I will translate the c header.

Thanks.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018