Changeset 499

Show
Ignore:
Timestamp:
08/18/07 13:44:24 (1 year ago)
Author:
shans
Message:

Volume ramps handled correctly for instruments.
Volumes now an abstract type.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ocaml-remix/trunk/instrument.ml

    r498 r499  
    1313  instrument.position <- instrument.position +. length;; 
    1414 
    15 let rec add_notes instrument layer amplitude notes = match notes with 
    16   | (t,l)::r -> ( 
    17       add_note instrument layer amplitude t l;  
    18       add_notes instrument layer amplitude r) 
    19   | [] -> ();; 
     15let calc_length l = List.fold_right (fun (t,l) s -> l +. s) l 0.;; 
     16 
     17let add_notes instrument layer volume notes =  
     18  let start_pos = instrument.position in 
     19  let len = calc_length notes in 
     20  let rec _add_notes notes =  
     21    match notes with 
     22    | (t,l)::r -> ( 
     23        let spos = instrument.position -. start_pos in 
     24        let epos = spos +. l in 
     25        let s = Volume.amplitude volume spos len in 
     26        let e = Volume.amplitude volume epos len in 
     27        let newv = Volume.ramp s e in 
     28        add_note instrument layer newv t l;  
     29        _add_notes r) 
     30    | [] -> () in 
     31  _add_notes notes;; 
    2032   
  • ocaml-remix/trunk/instrument_test.ml

    r498 r499  
    1515 
    1616let layer = Layer.empty ();; 
    17 Instrument.add_notes instrument1 layer (Volume.Constant 30000.) (List.map tonote 
    18   [(c, 0, 0.5); (d, 0, 0.5); (e, 0, 0.5); (f, 0, 0.5);  
    19    (g, 0, 0.5); (a, 0, 0.5); (b, 0, 0.5); (c, 1, 2.5)]);; 
     17Instrument.add_notes instrument1 layer (Volume.ramp 5000. 30000.)  
     18  (List.map tonote [(c, 0, 0.5); (d, 0, 0.5); (e, 0, 0.5); (f, 0, 0.5);  
     19                    (g, 0, 0.5); (a, 0, 0.5); (b, 0, 0.5); (c, 1, 2.5)]);; 
    2020 
    21 Instrument.add_notes instrument2 layer (Volume.Constant 30000.) (List.map tonote 
    22   [(g, -1, 0.5); (a, -1, 0.5); (b flat, -1, 0.5); (c, 0, 0.5);  
    23    (d, 0, 0.5); (e flat, 0, 0.5); (f sharp, 0, 0.5); (g, 0, 2.5)]);; 
     21Instrument.add_notes instrument2 layer (Volume.ramp 30000. 5000.)  
     22  (List.map tonote [(g, -1, 0.5); (a, -1, 0.5); (b flat, -1, 0.5); (c, 0, 0.5);  
     23              (d, 0, 0.5); (e flat, 0, 0.5); (f sharp, 0, 0.5); (g, 0, 2.5)]);; 
    2424 
    2525let sound = Layer.to_sound layer;; 
  • ocaml-remix/trunk/layer_test_sins.ml

    r498 r499  
    77                                                          (Time.Seconds 0.1);; 
    88 
    9 let a    = Envelope.apply e (Wave.sin    (Volume.Constant 30000.) 440.);; 
    10 let hi_a = Envelope.apply e (Wave.square (Volume.Constant 20000.) 880.);; 
    11 let e    = Envelope.apply e (Wave.tri    (Volume.Constant 20000.) 660.);; 
     9let a    = Envelope.apply e (Wave.sin    (Volume.constant 30000.) 440.);; 
     10let hi_a = Envelope.apply e (Wave.square (Volume.constant 20000.) 880.);; 
     11let e    = Envelope.apply e (Wave.tri    (Volume.constant 20000.) 660.);; 
    1212 
    1313let layer = Layer.empty ();; 
  • ocaml-remix/trunk/sound_test_sin.ml

    r498 r499  
    77 
    88let r = Remix.create 44000 2 120.;; 
    9 let four_second_A = Wave.sin (Volume.Linear (0., amp)) rate;; 
     9let four_second_A = Wave.sin (Volume.ramp 0. amp) rate;; 
    1010let envelope = Envelope.adsr (Time.Seconds 0.1) (Time.Seconds 0.4) 0.8 
    1111                                                        (Time.Seconds 0.1);; 
  • ocaml-remix/trunk/track_test_sins.ml

    r498 r499  
    55let env = Envelope.adsr (Time.Seconds 0.2) (Time.Seconds 0.6) 0.7  
    66                                                           (Time.Seconds 0.1);; 
    7 let a = Wave.sin (Volume.Constant 30000.) (Tone.note Tone.a 0);; 
    8 let e = Wave.sin (Volume.Constant 20000.) (Tone.note Tone.e 1);; 
     7let a = Wave.sin (Volume.constant 30000.) (Tone.note Tone.a 0);; 
     8let e = Wave.sin (Volume.constant 20000.) (Tone.note Tone.e 1);; 
    99let [a; e] = List.map (Envelope.apply env) [a; e];; 
    1010 
  • ocaml-remix/trunk/volume.ml

    r498 r499  
    11type volume = Constant of float | Linear of float * float;; 
    22 
     3exception AmplitudeTooHigh 
     4 
     5let constant v = if (v > 32767. || v < 0.) then raise AmplitudeTooHigh; 
     6  Constant v;; 
     7 
     8let ramp s e = if (s > 32767. || s < 0.) then raise AmplitudeTooHigh; 
     9  if (e > 32767. || e < 0.) then raise AmplitudeTooHigh; 
     10  Linear (s, e);; 
     11 
     12let amplitude volume position length = match volume with  
     13  | Constant v    -> v 
     14  | Linear (s, e) -> 
     15    (position /. length) *. (e -. s) +. s;; 
  • ocaml-remix/trunk/volume.mli

    r498 r499  
    1 type volume = Constant of float | Linear of float * float;; 
     1type volume;; 
    22 
     3val constant : float -> volume;; 
     4val ramp : float -> float -> volume;; 
     5val amplitude : volume -> float -> float -> float;; 
  • ocaml-remix/trunk/wave.ml

    r498 r499  
    3030 
    3131let generate generator volume rate remix (ss, slen) (start, length) =  
    32   let (startvol, endvol) = (match volume with 
    33     | Volume.Constant v -> (v,v) 
    34     | Volume.Linear (s,e) -> (s, e)) in 
    35   if (startvol > 32767. || startvol < 0.) then raise AmplitudeTooHigh; 
    36   if (endvol > 32767. || endvol < 0.) then raise AmplitudeTooHigh; 
    3732  let raw_length = Time.time_to_raw_samples remix length in 
    3833  let raw_start = Time.time_to_raw_samples remix start in 
     
    4338  let offset = (raw_start - raw_ss) in 
    4439  let channels = Remix.channels remix in 
    45   let vfunc = fun p -> (float_of_int (p + offset) /. float_of_int raw_slen) *. 
    46                        (endvol -. startvol) +. startvol in 
     40  let vfunc = fun p -> Volume.amplitude volume (float_of_int (p + offset))  
     41                                                  (float_of_int raw_slen) in 
    4742  Array.init raw_length  
    4843          (fun p -> generator (vfunc p) period raw_start channels p);;