Changeset 6696

Show
Ignore:
Timestamp:
06/29/09 16:17:17 (9 months ago)
Author:
smimram
Message:

Square synth.

Location:
trunk/liquidsoap/src/synth
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/liquidsoap/src/synth/keyboard_sdl.ml

    r6695 r6696  
    7070                  let c = Sdlkey.char_of_key k.Sdlevent.keysym in 
    7171                  let n = note_of_char c in 
    72                     Printf.printf "Playing note %d.\n%!" n; 
     72                    (* Printf.printf "Playing note %d.\n%!" n; *) 
    7373                    ans := (0,Midi.Note_on (n, velocity))::!ans 
    7474              | Some (Sdlevent.KEYUP k) -> 
    7575                  let c = Sdlkey.char_of_key k.Sdlevent.keysym in 
    7676                  let n = note_of_char c in 
    77                     Printf.printf "Stopping note %d.\n%!" n; 
     77                    (* Printf.printf "Stopping note %d.\n%!" n; *) 
    7878                    ans := (0,Midi.Note_off (n, velocity))::!ans 
    7979              | _ -> () 
  • trunk/liquidsoap/src/synth/synth.ml

    r6695 r6696  
    6464end 
    6565 
    66 type sine_gs = unit 
    67 type sine_ns = 
     66type simple_gs = unit 
     67(* Period is 1. *) 
     68type simple_ns = 
    6869    { 
    69       sine_phase : float; 
    70       sine_freq : float; 
    71       sine_ampl : float; 
     70      simple_phase : float; 
     71      simple_freq : float; 
     72      simple_ampl : float; 
    7273    } 
    7374 
    74 class sine = 
     75class simple f = 
    7576object (self) 
    76   inherit [sine_gs, sine_ns] base 
     77  inherit [simple_gs, simple_ns] base 
    7778 
    7879  method state_init = () 
    7980 
    80   method note_init n v = { sine_phase = (*Random.float (2. *. pi)*) 0.; sine_freq = freq_of_note n; sine_ampl = v } 
     81  method note_init n v = { simple_phase = (*Random.float 1.*) 0.; simple_freq = freq_of_note n; simple_ampl = v } 
    8182 
    8283  method synth_note_mono gs ns freq buf ofs len = 
    83     let phase i = ns.sine_phase +. float i /. freq *. ns.sine_freq *. 2. *. pi in 
     84    let phase i = ns.simple_phase +. float i /. freq *. ns.simple_freq in 
    8485      for i = ofs to ofs + len - 1 do 
    85         buf.(i) <- buf.(i) +. ns.sine_ampl *. sin (phase i) 
     86        buf.(i) <- buf.(i) +. ns.simple_ampl *. f (phase i) 
    8687      done; 
    87       gs, { ns with sine_phase = phase len } 
     88      gs, { ns with simple_phase = fst (modf (phase len)) } 
    8889end 
     90 
     91class sine = object inherit simple (fun x -> sin (x *. 2. *. pi)) end 
     92 
     93class square = object inherit simple (fun x -> let x = fst (modf x) in if x < 0.5 then 1. else -1.) end 
  • trunk/liquidsoap/src/synth/synth_op.ml

    r6695 r6696  
    7171       let src = Lang.to_source (f "") in 
    7272         new synth (new Synth.sine :> Synth.synth) src 0) 
     73 
     74let () = 
     75  Lang.add_operator "synth.square" 
     76    [ "", Lang.source_t, None, None ] 
     77    ~category:Lang.SoundSynthesis 
     78    ~descr:"Square synthesiser." 
     79    (fun p _ -> 
     80       let f v = List.assoc v p in 
     81       let src = Lang.to_source (f "") in 
     82         new synth (new Synth.square :> Synth.synth) src 0)