Advertisement
Guest User

Liquidsoap rotate function

a guest
Oct 5th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.90 KB | None | 0 0
  1.  
  2. (** Function that get the next element based on the current round and the module of its weight
  3. if  round mod weight =0 the song is played
  4. returns (list,num) in which list is the list of childs and num the rount after this interaction**)
  5. let mod_check =fun  ~pos ~ready_list ->
  6.    
  7.         (*just let one hundred possible values in the counter *)
  8.         let aux = if pos+1>=101 then 1 else pos+1 in
  9.          
  10.         (* if the round is a multiple of the source it's put on the play_list, if isn't then we increase the music counter, i'm assumming here that the musics are in a ascendent order, otherwise it should loop the whole list before increment the counter *)
  11.         let (aux_list,aux)=List.fold_left   (fun (l,n) (s,w) -> if n mod w=0 then
  12.                                         (l@[s],n)
  13.                                         else if (List.length(l))<=0 then(l,n+1)
  14.                                         else(l,n))
  15.                             ([],aux) ready_list in
  16.            
  17.             (aux_list,aux)
  18.    
  19.  
  20. (** Switch the songs according to a given function
  21. * the param func is the function
  22.  *)
  23. class func_random ~kind ?replay_meta ~func ~strict  mode children =
  24.  let name = if strict then "quota" else "random" in
  25. object
  26.   inherit switch ~name ~kind ?replay_meta ~mode (List.map snd children)
  27.  
  28.   val mutable pos = 0
  29.   val mutable play_list=ref []
  30.   (* Annihilate the reversal in #select once for all. *)
  31.   val children = List.rev children
  32.  
  33.   method private select =
  34.     (* There isn't itens in th list*)
  35.     if (List.length(!play_list))<=0 then
  36.    
  37.             let (ready_list,n) =
  38.    
  39.              (*
  40.               fold_left função valor_inicial lista
  41.               :: add na lista
  42.                 l é a lista vazia, k é 0
  43.                 Ao passar por aqui ela é revertida com a lista das filhas ficando em primeiro,peso em ultimo
  44.               *)
  45.               List.fold_left
  46.             (fun (l,k) (w,s) -> if s.source#is_ready then (s,w)::l,(k+w) else l,k)
  47.             ([],0) children
  48.             in
  49.    
  50.               if n = 0 then None else
  51.             (*Gets the list of avaiable musics based on the givem function*)
  52.                 let (aux_list,aux)=( func ~pos:pos ~ready_list:ready_list) in
  53.             let retval=List.hd(aux_list) in
  54.             play_list:=List.tl(aux_list);
  55.             pos<-aux;
  56.             Some retval
  57.    
  58.     else (*there was itens in the list *)
  59.         begin
  60.              let retval=List.hd(!play_list) in
  61.              play_list:=List.tl(!play_list);
  62.    
  63.             Some  retval
  64.         end;
  65.   method stype =
  66.     if List.exists (fun (_,s) -> s.source#stype = Infallible) children then
  67.       Infallible
  68.     else
  69.       Fallible
  70. end
  71. let () =
  72.   let add name  descr weight_descr func =
  73.     let kind = Lang.univ_t 1 in
  74.     Lang.add_operator name ~descr ~category:Lang.TrackProcessing
  75.       (common  kind @
  76.        [ "weights", Lang.list_t Lang.int_t, Some (Lang.list Lang.int_t []),
  77.          Some weight_descr ;
  78.          "", Lang.list_t (Lang.source_t kind), None, None ])
  79.       ~kind:(Lang.Unconstrained kind)
  80.       (fun p kind ->
  81.          let children = Lang.to_source_list (List.assoc "" p) in
  82.          let replay_meta,ts,tr =
  83.            extract_common ~kind p (List.length children)
  84.          in
  85.          let weights =
  86.            List.map Lang.to_int (Lang.to_list (List.assoc "weights" p))
  87.          in
  88.          let weights =
  89.            if weights <> [] then weights else
  90.              Utils.make_list (List.length children) 1
  91.          in
  92.          let children =
  93.            if List.length weights <> List.length children then
  94.              raise
  95.                (Lang.Invalid_value
  96.                   ((List.assoc "weights" p),
  97.                    "there should be as many weights as sources")) ;
  98.             (*
  99.             Children vira uma tupla (weight,{trasition,source,cur_meta})       
  100.             *)
  101.            List.map2
  102.              (fun w c -> w,c)
  103.              weights
  104.              (List.map2
  105.                 (fun tr s -> { transition = tr ; source = s ; cur_meta = None })
  106.                 tr children)
  107.          in
  108.            new  func_random ~kind ~replay_meta ~func:func true ts children)
  109.   in
  110. add "rotate_modulus" "Play a song if the current round is a multiple of its weight" "The weight list" mod_check ;
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement