Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (** Function that get the next element based on the current round and the module of its weight
- if round mod weight =0 the song is played
- returns (list,num) in which list is the list of childs and num the rount after this interaction**)
- let mod_check =fun ~pos ~ready_list ->
- (*just let one hundred possible values in the counter *)
- let aux = if pos+1>=101 then 1 else pos+1 in
- (* 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 *)
- let (aux_list,aux)=List.fold_left (fun (l,n) (s,w) -> if n mod w=0 then
- (l@[s],n)
- else if (List.length(l))<=0 then(l,n+1)
- else(l,n))
- ([],aux) ready_list in
- (aux_list,aux)
- (** Switch the songs according to a given function
- * the param func is the function
- *)
- class func_random ~kind ?replay_meta ~func ~strict mode children =
- let name = if strict then "quota" else "random" in
- object
- inherit switch ~name ~kind ?replay_meta ~mode (List.map snd children)
- val mutable pos = 0
- val mutable play_list=ref []
- (* Annihilate the reversal in #select once for all. *)
- val children = List.rev children
- method private select =
- (* There isn't itens in th list*)
- if (List.length(!play_list))<=0 then
- let (ready_list,n) =
- (*
- fold_left função valor_inicial lista
- :: add na lista
- l é a lista vazia, k é 0
- Ao passar por aqui ela é revertida com a lista das filhas ficando em primeiro,peso em ultimo
- *)
- List.fold_left
- (fun (l,k) (w,s) -> if s.source#is_ready then (s,w)::l,(k+w) else l,k)
- ([],0) children
- in
- if n = 0 then None else
- (*Gets the list of avaiable musics based on the givem function*)
- let (aux_list,aux)=( func ~pos:pos ~ready_list:ready_list) in
- let retval=List.hd(aux_list) in
- play_list:=List.tl(aux_list);
- pos<-aux;
- Some retval
- else (*there was itens in the list *)
- begin
- let retval=List.hd(!play_list) in
- play_list:=List.tl(!play_list);
- Some retval
- end;
- method stype =
- if List.exists (fun (_,s) -> s.source#stype = Infallible) children then
- Infallible
- else
- Fallible
- end
- let () =
- let add name descr weight_descr func =
- let kind = Lang.univ_t 1 in
- Lang.add_operator name ~descr ~category:Lang.TrackProcessing
- (common kind @
- [ "weights", Lang.list_t Lang.int_t, Some (Lang.list Lang.int_t []),
- Some weight_descr ;
- "", Lang.list_t (Lang.source_t kind), None, None ])
- ~kind:(Lang.Unconstrained kind)
- (fun p kind ->
- let children = Lang.to_source_list (List.assoc "" p) in
- let replay_meta,ts,tr =
- extract_common ~kind p (List.length children)
- in
- let weights =
- List.map Lang.to_int (Lang.to_list (List.assoc "weights" p))
- in
- let weights =
- if weights <> [] then weights else
- Utils.make_list (List.length children) 1
- in
- let children =
- if List.length weights <> List.length children then
- raise
- (Lang.Invalid_value
- ((List.assoc "weights" p),
- "there should be as many weights as sources")) ;
- (*
- Children vira uma tupla (weight,{trasition,source,cur_meta})
- *)
- List.map2
- (fun w c -> w,c)
- weights
- (List.map2
- (fun tr s -> { transition = tr ; source = s ; cur_meta = None })
- tr children)
- in
- new func_random ~kind ~replay_meta ~func:func true ts children)
- in
- add "rotate_modulus" "Play a song if the current round is a multiple of its weight" "The weight list" mod_check ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement