Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

Robi
UOS is running on Ubuntu  MATE 18.04 64bit and Windows10 64-bit (both with Lazarus 2.0)
Something I doing wrong...
- UOS libraries are loaded ok (code isn't needed - audiofile is playing)
- Loading audiofile is ok, I load audiofile like this:
   
          Player.PlayerCreated:= uos_CreatePlayer(Player.PlayerIndex);
          Player.OutputIndex:=uos_AddIntoDevOut(Player.PlayerIndex);
          Player.InputIndex:=-1;                                    
          Player.InputIndex:=uos_AddFromFile( Player.PlayerIndex, PChar(FileName) );
          if (Player.InputIndex>-1) then
             begin
               uos_EndProc(Player.PlayerIndex,@EndOfSongPlaying);
               uos_LoopProcIn(Player.PlayerIndex, Player.InputIndex,@LoopProcPlayer);
               uos_InputSetPositionEnable(Player.PlayerIndex, Player.InputIndex, 1);

               MyForm.tbPosition.Max:=uos_InputLength(Player.PlayerIndex,Player.InputIndex);
   
               temptime := uos_InputLengthTime(Player.PlayerIndex, Player.InputIndex);
               DecodeTime(temptime, ho, mi, se, ms);
               MyForm.ed_SongLength.Text:=format('%.2d:%.2d:%.2d.%.3d', [ho, mi, se, ms]);

               MyForm.tbPosition.Position:=0;
               uos_PlayNoFreePaused(Player.PlayerIndex);

               Player.PlayingPaused:=True;
               ChangeButtonNamePlayPause;
             end;


- My problem is in procedure EndOfSongPlaying (calling when player is at the end of audiofile). I want to PAUSE playing at the end of audiofile and I want to have a chance for scroll audiofile to any time-position and play it. In procedure EndOfSongPlaying is something like this:

            uos_Pause(Player.PlayerIndex);
            Player.PlayingPaused:=True;


When I get player status (by uos_GetStatus) in this procedure, status is 0 - stopped. This is problem for me -  replay audiofile in this state from any time position by uos_Replay is impossible (or I don't know how), because
      uos_InputSeek(Player.PlayerIndex, Player.InputIndex, tbPosition.position);
is not work. I suppose, when I use
     uos_PlayNoFreePaused(Player.PlayerIndex);
audiofile is not freed from memory and status can not be 0 - stopped. My first question: Is this assumption is correct?

- In example SimplePlayer included in UOS library is follewing sentence: "If PlayerIndex exists already, it will be overwritten...". When first audiofile is added with
uos_AddFromFile( 0, PChar(FirstAudioFileName) );
and after playing first audiofile I add second audiofile with the same PlayerIndex like this:
uos_AddFromFile( 0, PChar(SecondAudioFileName) );
When I press play - player playing mixed music from both audiofiles.
My Second question: Is there any function for remove first audiofile from player before loading second audiofile in the same PlayerIndex?

/I'm sorry for my english/
Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
Hello and welcome to uos forum.

> I want to PAUSE playing at the end of audiofile and I want to have a chance for scroll audiofile to any time-position and play it. In procedure EndOfSongPlaying is something like this: ...

Hum, I think that using EndProc will not work for pausing because EndProc is called when the thread has finished.

But you may use your LoopProcPlayer method to do it (and dont use EndProc anymore):
   
Example:

var:
delay_before_end : integer = 1000; // you should choose a delay before end.

Procedure Myform.LoopProcPlayer();
begin
// Your previous code
...

// Adding this:
 
   if  (uos_InputPosition(Player.PlayerIndex, Player.InputIndex) > MyForm.tbPosition.Max - delay_before_end) then
         uos_Pause(Player.PlayerIndex);
end;
 
>  Is there any function for remove first audiofile from player before loading second audiofile in the same PlayerIndex?

Not sure to understand.
When you create a audio-thread with uos_CreatePlayer(Player.PlayerIndex), if Player.PlayerIndex was already used to create a previous thread, it will stop + remove this thread and create a new one using the same Player.PlayerIndex.

But be careful, if you want to do loops, like play a file when previous has finished, you need to use 2 threads and do a kind of "ping-pong".

Please take a look at /uos/examples/morseTL.lpi (Morse Translator), it uses the "ping-pong" feature.

Fre;D
Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
Re-hello.

In previous post:

> delay_before_end : integer = 1000; // you should choose a delay before end.

If you want to pause just before the last buffer, you may use the FramesCount you used for uos_AddIntoDevOut().

The default parameter, it is 65536 div channels.
If you want more control, please use uos_AddFromFile(...) and  uos_AddIntoDevOut(...) with custom parameters.

For example:
buffersize := 1024 * 8;

uos_AddIntoDevOut(Player.PlayerIndex,-1,-1,-1,-1,-1,buffersize,-1);
uos_AddFromFile(Player.PlayerIndex, PChar(FileName), -1, -1, buffersize);


delay_before_end := buffersize;
Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

Robi
In reply to this post by fredvs
Hello, thank you for your replies.

I wrote something like you in LoopProcPlayer, but I thought - when I want to scroll playing audiofile to any time-position and play it - I have to watch playing position for interval from 0 to (AudiofileLength - delay_before_end). I wanted to know if there is a cleaner solution.
Something, what pause playing before thread is finished (for example in the last milisecond of audiofile) - because I am using uos_PlayNoFreePaused (I think, that uos_PlayNoFree not ending thread automatically) and then I would be able to scroll audiofile to any time-position and play audiofile from this position.

fredvs wrote
Please take a look at /uos/examples/morseTL.lpi (Morse Translator), it uses the "ping-pong" feature.
I know what you mean, I saw that example... And that's exactly what I wanted to avoid...  
I want to do:
First audiofile is playing in player with Player.Index = 0. Now I pause this player and then I want to player with the same Player.Index (Player.Index = 0) add an another audiofile and play it (old audiofile is not longer interesting to me).
When I do this, player played both audiofiles at the same time with some noises - so so I thought that the old audiofile was not removed from thread.
Next I do - the new audiofile I add to a new player with (Player.Index = 1). Now I don't know, how I remove old Player with Player.Index = 0 from memory.    

In your second reply you wrote:
fredvs wrote
If you want more control, please use uos_AddFromFile(...) and  uos_AddIntoDevOut(...) with custom parameters.
I have to learn to work with the buffer size - now I don't know if is this solution for me...
Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
In reply to this post by Robi
> My Second question: Is there any function for remove first audiofile from player before loading second audiofile in the same PlayerIndex?

If you use uos_PlayNoFree() (instead of uos_Play()) you have to use uos_FreePlayer(theplayer) before to create the new one.

PS: Note that, like explained in previous post, it is better to use the "ping-pong"  feature (2 players that switch one to other), this to avoid a delay between end and play.
Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
In reply to this post by Robi
> I want to do:
> First audiofile is playing in player with Player.Index = 0.

OK.

> Now I pause this player

OK.

> and then I want to player with the same Player.Index (Player.Index = 0)
> add an another audiofile and play it (old audiofile is not longer interesting to me).

Why not simply stop the player ( and use  uos_FreePlayer(theplayer) if PlayNoFree() was used) and re-create a new one?


Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
In reply to this post by Robi
> Next I do - the new audiofile I add to a new player with (Player.Index = 1). Now I don't know, how I remove old Player with > Player.Index = 0 from memory.    

If you use uos_PlayNoFree(0), to remove from memory just add uos_FreePlayer(0) when player = 0 has stopped and when you want to re-create a player using player = 0.
Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
This post was updated on .
In reply to this post by Robi
> I have to learn to work with the buffer size - now I don't know if is this solution for me...

I think that if you use the code from previous post, it should be ok:

buffersize := 1024 * 8;
...
uos_AddIntoDevOut(Player.PlayerIndex,-1,-1,-1,-1,-1,buffersize,-1);
uos_AddFromFile(Player.PlayerIndex, PChar(FileName), -1, -1, buffersize);
uos_LoopProcIn(Player.PlayerIndex, Player.InputIndex,@LoopProcPlayer);

delay_before_end := buffersize div 8;


And for your LoopProcPlayer:

Procedure Myform.LoopProcPlayer();
begin
// Your previous code
...

// Adding this:
 
   if  (uos_InputPosition(Player.PlayerIndex, Player.InputIndex) > MyForm.tbPosition.Max - delay_before_end) then
begin
         uos_Pause(Player.PlayerIndex);
         uos_InputSeek(Player.PlayerIndex, Player.InputIndex, MyForm.tbPosition.Max - (delay_before_end));
   
end;
end;


Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

Robi
In reply to this post by fredvs
fredvs wrote
Why not simply stop the player ( and use  uos_FreePlayer(theplayer) if PlayNoFree() was used) and re-create a new one?
A want to do like this:

procedure Free_player();
begin
          if (uos_GetStatus(Player.PlayerIndex) >=0) then
            begin
              uos_FreePlayer(Player.PlayerIndex);
              // Player with Player.Index is still created, only audiofile has been freed from memory
              Player.PlayerCreated:=True;
            end
          else Player.PlayerCreated:=False;

        Player.SongLoaded:=False;
        PlayerEnabling(Player.SongLoaded);
end;
// ----------------------------------------------
procedure TMyForm.Load_song(FileName:string; PlayFromZero:boolean);
var temptime:ttime;
    ho, mi, se, ms: word;
begin
 if (uos_lib_OK) then
// if UOS libraries wad loaded successfully:  uos_lib_OK = True
    begin
        Free_player;
        if not(Player.PlayerCreated) then
          begin
            Player.PlayerCreated:= uos_CreatePlayer(Player.PlayerIndex);
            Player.OutputIndex:=uos_AddIntoDevOut(Player.PlayerIndex);
            Player.InputIndex:=-1;
          end;
       Player.InputIndex:=uos_AddFromFile( Player.PlayerIndex, PChar(FileName) );
       if (Player.InputIndex>-1) then
         begin
           Player.SongLoaded:=True;
           PlayerEnabling(Player.SongLoaded);
           uos_LoopEndProc(Player.PlayerIndex, @EndOfSongPlaying);
           uos_LoopProcIn(Player.PlayerIndex, Player.InputIndex,@LoopProcPlayer);
           uos_InputSetPositionEnable(Player.PlayerIndex, Player.InputIndex, 1);  // 1 je ze ma pocitat poziciu
           MyForm.tbPosition.Max:=uos_InputLength(Player.PlayerIndex,Player.InputIndex);
           temptime := uos_InputLengthTime(Player.PlayerIndex, Player.InputIndex);
           DecodeTime(temptime, ho, mi, se, ms);
           MyForm.ed_SongLength.Text:=format('%.2d:%.2d:%.2d.%.3d', [ho, mi, se, ms]);
           Player.SongEndPauseTime:=IncMilliSecond(temptime,DefaultEndSongPauseDelay);
           uos_PlayNoFreePaused(Player.PlayerIndex); // 0 je pocet sluciek
           Player.PlayingPaused:=True;
           ChangeButtonNamePlayPause;      
        end;
     end;
end;                        


Result - after Adding second audiofile (first added and played audiofile was correctly ended by procedure Free_Player), Player is playing some artefacts from first audiofile and noises at the same time as is playing second audiofile.
This is no way for me - I don't know how to do simply. Probably I must do it by "ping-pong".

Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
Hello.

I think the problem comes from:

procedure Free_player();
begin
...    
    uos_FreePlayer(Player.PlayerIndex); // This
 ...
end;

Because you assigned Free_player() to EndProc of the player-self.

So you ask to the player to execute a method to free_himself.

I fear that you should think about the ping-pong solution (or avoid to ask to the player to free-himself in a EndProc).

Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

fredvs
Administrator
This post was updated on .
> This is no way for me - I don't know how to do simply

Maybe not use uos_PlayNoFree() but uos_Play().

And, if you dont want that the file reach to the end, use procedure Myform.LoopProcPlayer() like explained in previous post to automatically set to pause at last buffer.

[EDIT] Updated the code of previous post about LoopProcPlayer(). And tested, it works ok here.

When you want to stop and create a new player with same index, on button.click:

procedure button.click;
begin
uos_Stop(Player.PlayerIndex);
uos_CreatePlayer(Player.PlayerIndex);
end;

If you want to create the player with EndProc, you must use the ping-pong feature, otherwise it will have problems for the thread to first free-himself and then re-create himself.

Reply | Threaded
Open this post in threaded view
|

Re: Playing with uos_PlayNoFreePaused and adding file to PlayerIndex...

Robi
Thanks again for the help.
I'll try to do it according to your suggestions. I'll let you know if I've been successful.