A Basic Question: A list player

View: New views
4 Messages — Rating Filter:   Alert me  

A Basic Question: A list player

by chikitin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Folks,

I am totaly new to Lua. I am trying to make a function called "list_playerXL", so that, it plays the sounds defined in the array named "a" one after another. If you call the function with bl=0, it doesn't randomize them, it simply plays the first n sounds, otherwise ( for any other values for b) it selects m random sounds defined in "a" and play them one after another. Here's how I have tried to do it:


first we define an array of sound as follows:



a={}

a[1]=sound...

...



a[m]=sound...


--The folowing function returns an array containing n elements randomly selected from tbl.

Code:

function n_permuter(tbl, n)
  for i = 1,  n do
    local a,b = math.random(#tbl),math.random(#tbl)
    tbl[a], tbl[b] = tbl[b], tbl[a]
  end
end

---- here's the player! but doesn't work properly.

function list_playerXL(a,m,bl)

   
  a[1]:play()
  local ii=1
 
 
if bl==0 then

 pipmak.schedule(.05,

       function()
             
         repeat              
         if not a[ii]:playing() then
             if not a[ii+1]:playing() then  

                            ii=ii+1
                            a[ii]:play()

                          end          
                      end
       
         if a[ii]:playing() then
                         n = .05
                      else

                            n = nil
                   return n

                      end          
             until ii==m-1        
                      end
          )
else

      a= n_permuter(a, m)
      list_playerXL(a, m, 0)
   
end
end


-- but when i execute the code, it only plays the first sound in the array! I tried list_playerXL(a,6,0). but it plays different number of sounds each time! It seams the repeat loop in the code doesn't work properly.
I couldn't Attach the file ( 5MB) it was saying that the file is too large when i tried to upload it.


Any help would be greatly appreciated.

cs

Re: A Basic Question: A list player

by Christian Walther :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

chikitin wrote:
> --The folowing function returns an array containing n elements  
> randomly

> selected from tbl.

No. It performs n random transpositions on tbl.

> Code:
>
> function n_permuter(tbl, n)
>  for i = 1,  n do
>    local a,b = math.random(#tbl),math.random(#tbl)
>    tbl[a], tbl[b] = tbl[b], tbl[a]
>  end
> end

The # operator is new in Lua 5.1, and Pipmak still uses 5.0 (will  
update to 5.1 in version 0.3, whenever that is). In Lua 5.0, you use  
table.getn(tbl). You should have gotten an error message about this.

> ---- here's the player! but doesn't work properly.
>
> function list_playerXL(a,m,bl)
>
>
>  a[1]:play()
>  local ii=1
>
>
> if bl==0 then
>
> pipmak.schedule(.05,
>
>       function()
>
>         repeat
>         if not a[ii]:playing() then
>             if not a[ii+1]:playing() then
>
>                            ii=ii+1
>                            a[ii]:play()
>
>                          end
>                      end
>
>         if a[ii]:playing() then
>                         n = .05
>                      else
>
>                            n = nil
>                   return n
>
>                      end
>             until ii==m-1
>                      end
>          )
> else
>
>      a= n_permuter(a, m)
>      list_playerXL(a, m, 0)
>
> end
> end
>
>
> -- but when i execute the code, it only plays the first sound in the  
> array!

I'm having some trouble understanding that code. Your scheduled  
function only ever returns nil or nothing, and therefore is only run  
once, is that what you want? What's the point of assigning .05 to n  
when that value is never used (the only place where n is used is in  
"return n", and there you set it to nil just before)? (Is the return  
statement placed where you meant to place it? Your indentation is a  
bit odd so it's easy to miss.) Your repeat loop spins a large number  
of times while sounds are playing, using 100% CPU and blocking all  
other Pipmak activity, is that what you want?

Here's how I would clean it up into something that does approximately  
what you want (untested):

function list_playerXL(a,m,bl)
   if bl ~= 0 then
     n_permuter(a, 30)
   end
   a[1]:play()
   local ii=1
   pipmak.schedule(.05,
     function()
       if ii == m then
         return nil
       end
       if not a[ii]:playing() then
         ii=ii+1
         a[ii]:play()
       end
       return .05
     end
   )
end

> I tried list_playerXL(a,6,0). but it plays different number of  
> sounds each
> time!

I'm not sure why it would do that, at a glance, but I don't have time  
to test it or analyze it in detail right now. I'll have a closer look  
tomorrow if you can't figure it out until then.

> I couldn't Attach the file ( 5MB) it was saying that the file is too  
> large
> when i tried to upload it.

Yes, we'd rather not spam all list subscribers with 5MB files. Why is  
it so big, anyway? I'd imagine that you should be able to strip down  
an example project for this to a few dozen KB. If it must be that big,  
then you're better off putting it on some web server or file hosting  
service and posting a link than sending it directly through the list.

   -Christian

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Pipmak-Users mailing list
Pipmak-Users@...
news://news.gmane.org/gmane.games.devel.pipmak.user
https://lists.sourceforge.net/lists/listinfo/pipmak-users

Re: A Basic Question: A list player

by chikitin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>
> function n_permuter(tbl, n)
>  for i = 1,  n do
>    local a,b = math.random(#tbl),math.random(#tbl)
>    tbl[a], tbl[b] = tbl[b], tbl[a]
>  end
> end

Yes. I couldn't do pass by reference in pipmak:) So, I use

local a,b = math.random(tbl),math.random(tbl)

instead.


>
I'm having some trouble understanding that code. Your scheduled  
function only ever returns nil or nothing, and therefore is only run  
once, is that what you want? What's the point of assigning .05 to n  
when that value is never used (the only place where n is used is in  
"return n", and there you set it to nil just before)? (Is the return  
statement placed where you meant to place it? Your indentation is a  
bit odd so it's easy to miss.) Your repeat loop spins a large number  
of times while sounds are playing, using 100% CPU and blocking all  
other Pipmak activity, is that what you want?

>> you raised a great point here. The schdeude function checks every .05 second if the current music a[i] is still playing! as you see if the current music it is not playing then next music a[i+1] will immidiately play ( having the schedule function with interval nil). Is there any other way to ask pipmak to do some action as soon as a sound is stopped playing?


Here's how I would clean it up into something that does approximately  
what you want (untested):

function list_playerXL(a,m,bl)
   if bl == 0 then
     n_permuter(a, 30)
   end
   a[1]:play()
   local ii=1
   pipmak.schedule(.05,
     function()
       if ii == m then
         return nil
       end
       if not a[ii]:playing() then
         ii=ii+1
         a[ii]:play()
       end
       return .05
     end
   )
end

Thanks for cleaning that up:) It is much more understandable now:)


> I tried list_playerXL(a,6,0). but it plays different number of  
> sounds each
> time!

I'm not sure why it would do that, at a glance, but I don't have time  
to test it or analyze it in detail right now. I'll have a closer look  
tomorrow if you can't figure it out until then.

> I couldn't Attach the file ( 5MB) it was saying that the file is too  
> large
> when i tried to upload it.

Yes, we'd rather not spam all list subscribers with 5MB files. Why is  
it so big, anyway? I'd imagine that you should be able to strip down  
an example project for this to a few dozen KB. If it must be that big,  
then you're better off putting it on some web server or file hosting  
service and posting a link than sending it directly through the list.

   -Christian

Hey Christian! now it works. You can try it yourself at:

http://faculty.uncfsu.edu/csarami/files/storage/Demo.pipmak - Random.zip

Thansk a lot.




cs



------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Pipmak-Users mailing list
Pipmak-Users@lists.sourceforge.net
news://news.gmane.org/gmane.games.devel.pipmak.user
https://lists.sourceforge.net/lists/listinfo/pipmak-users



Re: A Basic Question: A list player

by Christian Walther :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

chikitin wrote:
> I couldn't do pass by reference in pipmak:)

Lua always passes by reference. If you can explain what you're trying to
achieve we may be able to show you how to do it in Lua.

> Is there any
> other way to ask pipmak to do some action as soon as a sound is stopped
> playing?

No. As far as I am aware there is no such callback in OpenAL, so all
Pipmak could do is polling whether the sound is still playing, and you
can just as well do that yourself (as you're doing now).

> Hey Christian! now it works. You can try it yourself at:
>
> http://faculty.uncfsu.edu/csarami/files/storage/Demo.pipmak - Random.zip

Well, it works as long as you keep the randomization turned off - if you
enable it, you get errors because you're mixing nil values into the
table. You're using a proper permutation function now and 30 is the
wrong argument for that, it needs the table length (and since it can
determine that on its own, there is no reason to pass it as an argument).

Some more tips:

- Defining a global function in one node (99) and using it in another
(15) is a bit fragile. What if the player enters node 15 without ever
having visited node 99, e.g. when starting from a saved game? (In your
case, the older version of the function from main.lua would be used, but
that looks like a leftover - and there is no guarantee that main.lua
will have been run either.) If you want to define this function in a
central place and use it from several nodes, you should put it into a
separate file and include that in the node.luas using pipmak.dofile().
(In the future, "require" will work too.)

- If you want a function to be called when a node is entered (like in
node 15), you shouldn't put it directly into node.lua. That currently
works, but in the future node.lua may also be run at other times. You
should put this code into an onenternode handler (there's one just a few
lines above in node 15).

  -Christian


------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Pipmak-Users mailing list
Pipmak-Users@...
news://news.gmane.org/gmane.games.devel.pipmak.user
https://lists.sourceforge.net/lists/listinfo/pipmak-users