Changeset 514

Show
Ignore:
Timestamp:
10/26/07 16:11:06 (10 months ago)
Author:
shans
Message:

Modified sounds to only accept start (not length as well). Makes shift and
repeat easier, and makes more sense overall.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ocaml-remix/trunk/Makefile

    r512 r514  
    22 
    33COMPONENTS = remix time volume sound envelope note tone wave transparency \ 
    4                         layer track ladspa deck noteUtil 
     4                        layer track deck noteUtil 
    55TESTS = sound_test_sin layer_test_sins track_test_sins \ 
    6                         ladspa_test continuous_sound_test 
     6                        continuous_sound_test 
    77INCLUDES=-I +extlib 
    88OBJECTS=extLib unix oss ladspa_raw 
  • ocaml-remix/trunk/continuous_sound_test.ml

    r513 r514  
    22   a period of 100 samples *) 
    33 
     4let s n = Time.Seconds n;; 
     5 
    46let note = Note.from_sound (Wave.sin (Volume.constant 30000.) 440.);; 
    57let envelope = Note.from_envelope  
    6   (Envelope.adsr (Time.Seconds 0.1) (Time.Seconds 0.4) 0.8 (Time.Seconds 0.1));; 
     8    (Envelope.adsr (s 0.75) (s 0.1) (s 0.4) 0.8 (s 0.1));; 
    79let envelope2 = Note.from_envelope  
    8   (Envelope.adsr (Time.Seconds 0.2) (Time.Seconds 0.2) 0.9 (Time.Seconds 0.3));; 
     10    (Envelope.adsr (s 0.8) (s 0.2) (s 0.2) 0.9 (s 0.3));; 
    911 
    1012let continuous = (Time.Seconds 0.0, Time.Forever);; 
     
    2931 
    3032let r = Remix.create 44000 2 120.;; 
    31 Sound.preview r (Track.to_note track Sound.silence) continuous continuous;; 
     33Sound.preview r (Track.to_note track Sound.silence) (s 0.) continuous;; 
  • ocaml-remix/trunk/envelope.ml

    r512 r514  
    1 type envelope = Remix.remix -> Time.extent -> int -> float;; 
     1type envelope = Remix.remix -> Time.time -> int -> float;; 
    22 
    3 let adsr a d s r remix (start, length) pos = 
     3let adsr length a d s r remix start pos = 
    44  let a = Time.time_to_raw_samples remix a in 
    55  let d = Time.time_to_raw_samples remix d in 
    66  let r = Time.time_to_raw_samples remix r in 
    77  if length = Time.Forever then 
    8     Printf.printf "can't create a non-terminating adsr envelope\n"; 
     8    Printf.printf "can't create a non-terminating adsr envelope\n%!"; 
    99  let raw_length = Time.time_to_raw_samples remix length in 
    1010  if pos <= a  
     
    1616  else s *. float_of_int (raw_length - pos) /. float_of_int r;; 
    1717 
    18 let apply envelope sound remix sound_extent render_extent = 
    19   let ss = Time.time_to_raw_samples remix (fst sound_extent) in 
     18let apply envelope sound remix sound_start render_extent = 
     19  let ss = Time.time_to_raw_samples remix sound_start in 
    2020  let (rs, rl) = Time.extent_to_raw_samples remix render_extent in 
    21   let raw_envelope = envelope remix sound_extent in 
    22   let raw_sound = sound remix sound_extent render_extent in 
     21  let raw_envelope = envelope remix sound_start in 
     22  let raw_sound = sound remix sound_start render_extent in 
    2323  for i = rs - ss to rs - ss + rl - 1 do ( 
    2424    let r = raw_envelope i in 
  • ocaml-remix/trunk/envelope.mli

    r493 r514  
    1 (** an envelope accepts a remix, an envelope length, and a current position, 
    2     an returns a scaling factor between 0.0 and 1.0 *) 
    3 type envelope = Remix.remix -> Time.extent -> Time.raw_time -> float;; 
     1(** an envelope accepts a remix, a start time, and a current position, 
     2    and returns a scaling factor between 0.0 and 1.0 *) 
     3type envelope = Remix.remix -> Time.time -> Time.raw_time -> float;; 
    44 
    5 (** an adsr envelope requires an attack time, a decay time, a sustain level, 
    6     and a release time *) 
    7 val adsr : Time.time -> Time.time -> float -> Time.time -> envelope;; 
    8  
     5(** an adsr envelope requires a total length, an attack time, a decay time,  
     6    a sustain level, and a release time *) 
     7val adsr : Time.time -> Time.time -> Time.time -> float -> Time.time ->  
     8                                                                    envelope;; 
     9                                                                     
     10(** it is possible to apply envelopes directly to sounds.  Note that envelopes 
     11    can also be added to layers as notes. *) 
    912val apply : envelope -> Sound.sound -> Sound.sound;; 
  • ocaml-remix/trunk/ladspa.ml

    r510 r514  
    4242      ( 
    4343        let channels = Remix.channels remix in 
    44         let rs = s remix (s_s, s_l) (r_s, r_l) in 
     44        let rs = s remix s_s (r_s, r_l) in 
    4545        let len = (Array.length rs) / channels in 
    4646        let out_arr =  
  • ocaml-remix/trunk/layer.ml

    r512 r514  
    6767        let (rs, rl) = Time.extent_to_raw_time remix (s, l) in 
    6868        if rl > 0 then ( 
    69           let raw_sound = (ss undersound) remix (start, length) (s, l) in 
     69          let raw_sound = (ss undersound) remix start (s, l) in 
    7070          Array.blit raw_sound 0 sound 
    7171                         ((rs - raw_render_start) * Remix.channels remix)  
  • ocaml-remix/trunk/layer_test_sins.ml

    r512 r514  
    44let remix = Remix.create 44000 2 120.;; 
    55 
    6 let e = Envelope.adsr (Time.Seconds 0.1) (Time.Seconds 0.4) 0.8 
     6let e t = Envelope.adsr t (Time.Seconds 0.1) (Time.Seconds 0.4) 0.8 
    77                                                          (Time.Seconds 0.1);; 
    88 
    9 let a    = Note.from_sound  
    10                 (Envelope.apply e (Wave.sin    (Volume.constant 30000.) 440.));; 
    11 let hi_a = Note.from_sound  
    12                 (Envelope.apply e (Wave.square (Volume.constant 20000.) 880.));; 
    13 let e    = Note.from_sound 
    14                 (Envelope.apply e (Wave.tri    (Volume.constant 20000.) 660.));; 
     9let a t    = Note.from_sound  
     10        (Envelope.apply (e t) (Wave.sin    (Volume.constant 30000.) 440.));; 
     11let hi_a t = Note.from_sound  
     12        (Envelope.apply (e t) (Wave.square (Volume.constant 20000.) 880.));; 
     13let e t    = Note.from_sound 
     14        (Envelope.apply (e t) (Wave.tri    (Volume.constant 20000.) 660.));; 
    1515 
    1616let layer = Layer.empty ();; 
    17 Layer.add_note layer a    (Time.Seconds 2.0, Time.Seconds 4.0);; 
    18 Layer.add_note layer hi_a (Time.Seconds 6.0, Time.Seconds 2.0);; 
    19 Layer.add_note layer e    (Time.Seconds 12.0, Time.Seconds 3.0);; 
     17 
     18let add note start len =  
     19    Layer.add_note layer (note (Time.Seconds len))  
     20                          (Time.Seconds start, Time.Seconds len);; 
     21 
     22add a    2.0  4.0;; 
     23add hi_a 6.0  2.0;; 
     24add e    12.0 3.0;; 
    2025 
    2126Layer.print layer;; 
    2227let sound = Layer.to_note layer Sound.silence;; 
    2328                                                             
    24 Sound.preview remix sound (Time.Seconds 0.0, Time.Seconds 15.0)  
    25                                   (Time.Seconds 0.0, Time.Seconds 15.0);; 
     29Sound.preview remix sound (Time.Seconds 0.0) 
     30                    (Time.Seconds 0.0, Time.Seconds 15.0);; 
  • ocaml-remix/trunk/noteUtil.ml

    r513 r514  
    66 *) 
    77 
    8 let shift slength note initsound remix extent render_extent =  
     8let shift slength note initsound remix start render_extent =  
    99  let raw_rstart, raw_rlength = Time.extent_to_raw_time remix render_extent in 
    1010  let raw_shift = Time.time_to_raw_time remix slength in 
    11   Printf.printf "shift (%d,%d) by %d\n%!" raw_rstart raw_rlength raw_shift; 
    12   Printf.printf "render at (%d,%d) in (%d,%d)\n%!" (raw_rstart + raw_shift) 
    13     raw_rlength (Time.time_to_raw_time remix (fst extent)) 
    14     (Time.time_to_raw_time remix (snd extent)); 
    15   note initsound remix extent 
     11  note initsound remix start 
    1612          (Time.Samples (raw_rstart + raw_shift), Time.Samples raw_rlength);;  
    1713 
     
    4339   
    4440 *) 
    45 let repeat length note initsound remix extent render_extent = 
     41let repeat length note initsound remix start render_extent = 
    4642  let raw_start, raw_length = Time.extent_to_raw_time remix render_extent in 
    4743  let raw_replength = Time.time_to_raw_time remix length in 
     
    5046  if reps = repe then ( 
    5147    note initsound remix  
    52                   (Time.Samples (reps * raw_replength), length) render_extent  
     48                  (Time.Samples (reps * raw_replength)) render_extent  
    5349  ) else ( 
    5450    let l = Layer.empty () in 
     
    5753      add note (i * raw_replength) raw_replength 
    5854    done; 
    59     Layer.to_note l initsound remix extent render_extent 
     55    Layer.to_note l initsound remix start render_extent 
    6056     
    6157    (*  
  • ocaml-remix/trunk/sound.ml

    r512 r514  
    22type raw_sound = int array;; 
    33 
    4 (* but sounds have a start, a length, a rendered start, and a rendered  
    5    length *) 
    6 type sound = Remix.remix -> Time.extent -> Time.extent -> raw_sound;;  
     4(* but sounds have a start, a rendered start, and a rendered length *) 
     5type sound = Remix.remix -> Time.time -> Time.extent -> raw_sound;;  
    76 
    87let silence r e (s,l) = 
     
    3029  for i = 0 to (Array.length snd - 1) do IO.write_i16 out snd.(i) done;; 
    3130 
    32 let write remix output snd render_extent = 
    33   let raw_sound = snd remix render_extent render_extent in 
     31let write output remix snd start render_extent = 
     32  let raw_sound = snd remix start render_extent in 
    3433  write_raw_sound remix output raw_sound;;  
    3534 
    36 let preview remix sound sound_extent (start,length) = 
    37   let raw_start = Time.time_to_raw_time remix start in 
     35let preview remix sound start (rstart,rlength) = 
     36  let raw_start = Time.time_to_raw_time remix rstart in 
    3837  let raw_inc = Time.time_to_raw_time remix (Time.Seconds 0.2) in 
    3938  let dev = Oss.sound_open () in 
     
    4241  let cond =  
    4342    ( 
    44       if length = Time.Forever  
     43      if rlength = Time.Forever  
    4544      then (fun a -> true) 
    4645      else ( 
    47         let raw_length = Time.time_to_raw_time remix length in 
     46        let raw_length = Time.time_to_raw_time remix rlength in 
    4847        fun a -> a < raw_length 
    4948      ) 
     
    5150  while cond !t do ( 
    5251    Oss.sound_write dev  
    53         (sound remix sound_extent (Time.Samples !t, Time.Samples raw_inc)); 
     52        (sound remix start (Time.Samples !t, Time.Samples raw_inc)); 
    5453    t := !t + raw_inc) done; 
    5554  Oss.sound_close dev;; 
  • ocaml-remix/trunk/sound.mli

    r508 r514  
    11type raw_sound = int array;; 
    22 
    3 (** a sound takes a remix, a start time and a length, and generates raw 
    4     data *) 
    5 type sound = Remix.remix -> Time.extent -> Time.extent -> raw_sound;;  
     3(** a sound takes a remix, a start time, and a render extent, and generates raw 
     4    data. 
     5     
     6    Note: sounds do not fundamentally possess a length.  Sounds and other 
     7    objects which depend upon length should take this as an extra parameter. 
     8    e.g. envelopes need a length to determine when to begin release. 
     9     
     10    *) 
     11type sound = Remix.remix -> Time.time -> Time.extent -> raw_sound;;  
    612 
    713val silence : sound;; 
    814 
    9 (** write_sound remix output sound start length *) 
    10 val write : Remix.remix -> 'a IO.output -> sound -> Time.extent -> unit;; 
     15val write : 'a IO.output -> Remix.remix -> sound -> Time.time ->  
     16                                                        Time.extent -> unit;; 
    1117 
    12 val preview : Remix.remix -> sound -> Time.extent -> Time.extent -> unit;; 
     18val preview : Remix.remix -> sound -> Time.time -> Time.extent -> unit;; 
    1319 
    1420val raw_preview : Remix.remix -> raw_sound -> unit;; 
  • ocaml-remix/trunk/sound_test_sin.ml

    r499 r514  
    77 
    88let r = Remix.create 44000 2 120.;; 
    9 let four_second_A = Wave.sin (Volume.ramp 0. amp) rate;; 
    10 let envelope = Envelope.adsr (Time.Seconds 0.1) (Time.Seconds 0.4) 0.8 
    11                                                         (Time.Seconds 0.1);; 
     9let four_second_A = Wave.sin (Volume.ramp (Time.Seconds len) 0. amp) rate;; 
     10let envelope = Envelope.adsr (Time.Seconds len) (Time.Seconds 0.1)  
     11                            (Time.Seconds 0.4) 0.8 (Time.Seconds 0.1);; 
    1212 
    1313let env_a = Envelope.apply envelope four_second_A;; 
     
    1919Sound.write r out env_a (Time.Seconds 0., Time.Seconds len);; 
    2020*) 
    21 Sound.preview r env_a (Time.Seconds 0.0, Time.Seconds len)  
    22                                   (Time.Seconds 0.0, Time.Seconds len);; 
     21Sound.preview r env_a (Time.Seconds 0.0) (Time.Seconds 0.0, Time.Seconds len);; 
  • ocaml-remix/trunk/track.ml

    r513 r514  
    3939exception OutOfBounds;; 
    4040let apply_transparency sound1 extent1 sound2 extent2 transparency text  
    41       remix oext rext = 
     41      remix start rext = 
    4242  let raw_sound1 = sound1 remix extent1 rext in 
    4343  let raw_sound2 = sound2 remix extent2 rext in 
     
    121121            let trans = elem.tce_transparency in  
    122122            let raw_start, l = Time.extent_to_raw_time remix ext in 
    123             _add_s_to_out (apply_transparency sound1 ext sound2 ext  
     123            _add_s_to_out (apply_transparency sound1 (fst ext) sound2 (fst ext)  
    124124                trans (elem.tce_start, elem.tce_length)) raw_start l; 
    125125            _render_at_2 (raw_start + l) (raw_length - l) r 
  • ocaml-remix/trunk/track_test_sins.ml

    r512 r514  
    22   a period of 100 samples *) 
    33 
    4 let env = Envelope.adsr (Time.Seconds 0.2) (Time.Seconds 0.6) 0.7  
     4let env t = Envelope.adsr t (Time.Seconds 0.2) (Time.Seconds 0.6) 0.7  
    55                                                           (Time.Seconds 0.1);; 
    66let a = Wave.sin (Volume.constant 30000.) (Tone.note Tone.a 0);; 
    77let e = Wave.sin (Volume.constant 20000.) (Tone.note Tone.e 1);; 
    8 let [a; e] = List.map (Envelope.apply env) [a; e];; 
    9 let [a; e] = List.map Note.from_sound [a; e];; 
     8let a t = Envelope.apply (env t) a;; 
     9let e t = Envelope.apply (env t) e;; 
     10let a t = Note.from_sound (a t);; 
     11let e t = Note.from_sound (e t);; 
    1012 
     13let add layer note start length = 
     14  Layer.add_note layer (note (Time.Seconds length))  
     15          (Time.Seconds start, Time.Seconds length);; 
    1116 
    1217(* AAAA  AAAA EEEE  
     
    1722let trans1 = Transparency.linear_ramp 0.0 1.0;; 
    1823let trans2 = Transparency.linear_ramp 1.0 0.0;; 
    19 Layer.add_note layer1 a (Time.Seconds 0.0, Time.Seconds 4.0);; 
    20 Layer.add_note layer1 a (Time.Seconds 6.0, Time.Seconds 4.0);; 
    21 Layer.add_note layer1 e (Time.Seconds 11.0, Time.Seconds 4.0);; 
     24add layer1 a 0.0 4.0;; 
     25add layer1 a 6.0 4.0;; 
     26add layer1 e 11.0 4.0;; 
    2227 
    2328let noise = Ladspa_raw.open_plugin "noise:noise_white";; 
     
    3136 
    3237let layer2 = Layer.empty ();; 
    33 Layer.add_note layer2 e (Time.Seconds 0.0, Time.Seconds 4.0);; 
    34 Layer.add_note layer2 e (Time.Seconds 5.0, Time.Seconds 6.0);; 
    35 Layer.add_note layer2 a (Time.Seconds 12.0, Time.Seconds 4.0);; 
     38add layer2 e 0.0 4.0;; 
     39add layer2 e 5.0 6.0;; 
     40add layer2 a 12.0 4.0;; 
    3641 
    3742let layer3 = Layer.empty();; 
     
    4449let plugin = Note.from_ladspa ladspa (Note.ladspa_init setup_plugin);; 
    4550Layer.add_note layer3 plugin (Time.Seconds 2.0, Time.Seconds 7.0);; 
    46 let trans = Transparency.linear_ramp 1.0 1.0;; 
     51let trans = Transparency.constant 1.0;; 
    4752Layer.add_transparency layer3 trans (Time.Seconds 0.0, Time.Seconds 2.0);; 
    4853Layer.add_transparency layer3 trans (Time.Seconds 9.0, Time.Seconds 16.0);; 
     
    5762 
    5863let remix = Remix.create 44000 2 120.;; 
    59 Sound.preview remix sound extent extent;; 
     64Sound.preview remix sound (Time.Seconds 0.0) extent;; 
  • ocaml-remix/trunk/transparency.ml

    r513 r514  
    44          raw_transparency;; 
    55 
    6 let linear_ramp start_val end_val remix (start,length) (ostart,olength) = 
     6let linear_ramp start_val end_val remix (start, length) (ostart,olength) = 
    77  let _to_raw a = (Time.time_to_raw_time remix a) * Remix.channels remix in 
    88  let raw_start = _to_raw start in 
     
    1717  Array.init raw_olength _fill;; 
    1818 
    19 let constant value remix size (rstart, rlength) = 
     19let constant value remix (start, len) (rstart, rlength) = 
    2020  Array.create (Time.time_to_raw_samples remix rlength) value;; 
  • ocaml-remix/trunk/volume.ml

    r512 r514  
    11type volume =  
    22  | Constant of float  
    3   | Linear of float * float;; 
     3  | Linear of Time.time * float * float;; 
    44 
    55exception AmplitudeTooHigh 
     
    88  Constant v;; 
    99 
    10 let ramp s e = if (s > 32767. || s < 0.) then raise AmplitudeTooHigh; 
     10let ramp l s e = if (s > 32767. || s < 0.) then raise AmplitudeTooHigh; 
    1111  if (e > 32767. || e < 0.) then raise AmplitudeTooHigh; 
    12   Linear (s, e);; 
     12  Linear (l, s, e);; 
    1313 
    14 let amplitude remix volume position length =  
     14let amplitude remix volume position =  
    1515  match volume with  
    1616  | Constant v    -> v 
    17   | Linear (s, e) -> ( 
    18       if length = Time.Forever then 
     17  | Linear (l, s, e) -> ( 
     18      if l = Time.Forever then 
    1919        Printf.printf "I can't ramp forever\n"; 
    20       let raw_length = Time.time_to_raw_time remix length in 
     20      let raw_length = Time.time_to_raw_time remix l in 
    2121      (float_of_int position /. float_of_int raw_length) *. (e -. s) +. s 
    2222    );; 
  • ocaml-remix/trunk/volume.mli

    r512 r514  
    22 
    33val constant : float -> volume;; 
    4 val ramp : float -> float -> volume;; 
    5 val amplitude : Remix.remix -> volume -> int -> Time.time -> float;; 
     4val ramp : Time.time -> float -> float -> volume;; 
     5val amplitude : Remix.remix -> volume -> int -> float;; 
  • ocaml-remix/trunk/wave.ml

    r512 r514  
    2929    (mod_float (float_of_int stime) period) /. period *. amp);; 
    3030 
    31 let generate generator volume rate remix (ss, slen) (start, length) =  
     31let generate generator volume rate remix ss (start, length) =  
    3232  let raw_length = Time.time_to_raw_samples remix length in 
    3333  let raw_start = Time.time_to_raw_samples remix start in 
     
    3737  let offset = (raw_start - raw_ss) in 
    3838  let channels = Remix.channels remix in 
    39   let vfunc = fun p -> Volume.amplitude remix volume (p + offset) slen in 
     39  let vfunc = fun p -> Volume.amplitude remix volume  
     40            ((p + offset)/Remix.channels remix) in 
    4041  Array.init raw_length  
    4142          (fun p -> generator (vfunc p) period raw_start channels p);; 
  • ocaml-remix/trunk/wave.mli

    r498 r514  
     1(* a volume and a frequency as input *) 
    12type wave = Volume.volume -> float -> Sound.sound;; 
    23