Changeset 514
- Timestamp:
- 10/26/07 16:11:06 (10 months ago)
- Files:
-
- ocaml-remix/trunk/Makefile (modified) (1 diff)
- ocaml-remix/trunk/continuous_sound_test.ml (modified) (2 diffs)
- ocaml-remix/trunk/envelope.ml (modified) (2 diffs)
- ocaml-remix/trunk/envelope.mli (modified) (1 diff)
- ocaml-remix/trunk/ladspa.ml (modified) (1 diff)
- ocaml-remix/trunk/layer.ml (modified) (1 diff)
- ocaml-remix/trunk/layer_test_sins.ml (modified) (1 diff)
- ocaml-remix/trunk/noteUtil.ml (modified) (4 diffs)
- ocaml-remix/trunk/sound.ml (modified) (4 diffs)
- ocaml-remix/trunk/sound.mli (modified) (1 diff)
- ocaml-remix/trunk/sound_test_sin.ml (modified) (2 diffs)
- ocaml-remix/trunk/track.ml (modified) (2 diffs)
- ocaml-remix/trunk/track_test_sins.ml (modified) (5 diffs)
- ocaml-remix/trunk/transparency.ml (modified) (2 diffs)
- ocaml-remix/trunk/volume.ml (modified) (2 diffs)
- ocaml-remix/trunk/volume.mli (modified) (1 diff)
- ocaml-remix/trunk/wave.ml (modified) (2 diffs)
- ocaml-remix/trunk/wave.mli (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ocaml-remix/trunk/Makefile
r512 r514 2 2 3 3 COMPONENTS = remix time volume sound envelope note tone wave transparency \ 4 layer track ladspadeck noteUtil4 layer track deck noteUtil 5 5 TESTS = sound_test_sin layer_test_sins track_test_sins \ 6 ladspa_testcontinuous_sound_test6 continuous_sound_test 7 7 INCLUDES=-I +extlib 8 8 OBJECTS=extLib unix oss ladspa_raw ocaml-remix/trunk/continuous_sound_test.ml
r513 r514 2 2 a period of 100 samples *) 3 3 4 let s n = Time.Seconds n;; 5 4 6 let note = Note.from_sound (Wave.sin (Volume.constant 30000.) 440.);; 5 7 let 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));; 7 9 let 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));; 9 11 10 12 let continuous = (Time.Seconds 0.0, Time.Forever);; … … 29 31 30 32 let r = Remix.create 44000 2 120.;; 31 Sound.preview r (Track.to_note track Sound.silence) continuouscontinuous;;33 Sound.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;;1 type envelope = Remix.remix -> Time.time -> int -> float;; 2 2 3 let adsr a d s r remix (start, length)pos =3 let adsr length a d s r remix start pos = 4 4 let a = Time.time_to_raw_samples remix a in 5 5 let d = Time.time_to_raw_samples remix d in 6 6 let r = Time.time_to_raw_samples remix r in 7 7 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%!"; 9 9 let raw_length = Time.time_to_raw_samples remix length in 10 10 if pos <= a … … 16 16 else s *. float_of_int (raw_length - pos) /. float_of_int r;; 17 17 18 let apply envelope sound remix sound_ extent render_extent =19 let ss = Time.time_to_raw_samples remix (fst sound_extent)in18 let apply envelope sound remix sound_start render_extent = 19 let ss = Time.time_to_raw_samples remix sound_start in 20 20 let (rs, rl) = Time.extent_to_raw_samples remix render_extent in 21 let raw_envelope = envelope remix sound_ extent in22 let raw_sound = sound remix sound_ extent render_extent in21 let raw_envelope = envelope remix sound_start in 22 let raw_sound = sound remix sound_start render_extent in 23 23 for i = rs - ss to rs - ss + rl - 1 do ( 24 24 let r = raw_envelope i in ocaml-remix/trunk/envelope.mli
r493 r514 1 (** an envelope accepts a remix, a n 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 *) 3 type envelope = Remix.remix -> Time.time -> Time.raw_time -> float;; 4 4 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 *) 7 val 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. *) 9 12 val apply : envelope -> Sound.sound -> Sound.sound;; ocaml-remix/trunk/ladspa.ml
r510 r514 42 42 ( 43 43 let channels = Remix.channels remix in 44 let rs = s remix (s_s, s_l)(r_s, r_l) in44 let rs = s remix s_s (r_s, r_l) in 45 45 let len = (Array.length rs) / channels in 46 46 let out_arr = ocaml-remix/trunk/layer.ml
r512 r514 67 67 let (rs, rl) = Time.extent_to_raw_time remix (s, l) in 68 68 if rl > 0 then ( 69 let raw_sound = (ss undersound) remix (start, length)(s, l) in69 let raw_sound = (ss undersound) remix start (s, l) in 70 70 Array.blit raw_sound 0 sound 71 71 ((rs - raw_render_start) * Remix.channels remix) ocaml-remix/trunk/layer_test_sins.ml
r512 r514 4 4 let remix = Remix.create 44000 2 120.;; 5 5 6 let e = Envelope.adsr(Time.Seconds 0.1) (Time.Seconds 0.4) 0.86 let e t = Envelope.adsr t (Time.Seconds 0.1) (Time.Seconds 0.4) 0.8 7 7 (Time.Seconds 0.1);; 8 8 9 let a = Note.from_sound10 (Envelope.apply e(Wave.sin (Volume.constant 30000.) 440.));;11 let hi_a = Note.from_sound12 (Envelope.apply e(Wave.square (Volume.constant 20000.) 880.));;13 let e = Note.from_sound14 (Envelope.apply e(Wave.tri (Volume.constant 20000.) 660.));;9 let a t = Note.from_sound 10 (Envelope.apply (e t) (Wave.sin (Volume.constant 30000.) 440.));; 11 let hi_a t = Note.from_sound 12 (Envelope.apply (e t) (Wave.square (Volume.constant 20000.) 880.));; 13 let e t = Note.from_sound 14 (Envelope.apply (e t) (Wave.tri (Volume.constant 20000.) 660.));; 15 15 16 16 let 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 18 let add note start len = 19 Layer.add_note layer (note (Time.Seconds len)) 20 (Time.Seconds start, Time.Seconds len);; 21 22 add a 2.0 4.0;; 23 add hi_a 6.0 2.0;; 24 add e 12.0 3.0;; 20 25 21 26 Layer.print layer;; 22 27 let sound = Layer.to_note layer Sound.silence;; 23 28 24 Sound.preview remix sound (Time.Seconds 0.0 , Time.Seconds 15.0)25 (Time.Seconds 0.0, Time.Seconds 15.0);;29 Sound.preview remix sound (Time.Seconds 0.0) 30 (Time.Seconds 0.0, Time.Seconds 15.0);; ocaml-remix/trunk/noteUtil.ml
r513 r514 6 6 *) 7 7 8 let shift slength note initsound remix extent render_extent =8 let shift slength note initsound remix start render_extent = 9 9 let raw_rstart, raw_rlength = Time.extent_to_raw_time remix render_extent in 10 10 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 16 12 (Time.Samples (raw_rstart + raw_shift), Time.Samples raw_rlength);; 17 13 … … 43 39 44 40 *) 45 let repeat length note initsound remix extent render_extent =41 let repeat length note initsound remix start render_extent = 46 42 let raw_start, raw_length = Time.extent_to_raw_time remix render_extent in 47 43 let raw_replength = Time.time_to_raw_time remix length in … … 50 46 if reps = repe then ( 51 47 note initsound remix 52 (Time.Samples (reps * raw_replength) , length) render_extent48 (Time.Samples (reps * raw_replength)) render_extent 53 49 ) else ( 54 50 let l = Layer.empty () in … … 57 53 add note (i * raw_replength) raw_replength 58 54 done; 59 Layer.to_note l initsound remix extent render_extent55 Layer.to_note l initsound remix start render_extent 60 56 61 57 (* ocaml-remix/trunk/sound.ml
r512 r514 2 2 type raw_sound = int array;; 3 3 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 *) 5 type sound = Remix.remix -> Time.time -> Time.extent -> raw_sound;; 7 6 8 7 let silence r e (s,l) = … … 30 29 for i = 0 to (Array.length snd - 1) do IO.write_i16 out snd.(i) done;; 31 30 32 let write remix output sndrender_extent =33 let raw_sound = snd remix render_extent render_extent in31 let write output remix snd start render_extent = 32 let raw_sound = snd remix start render_extent in 34 33 write_raw_sound remix output raw_sound;; 35 34 36 let preview remix sound s ound_extent (start,length) =37 let raw_start = Time.time_to_raw_time remix start in35 let preview remix sound start (rstart,rlength) = 36 let raw_start = Time.time_to_raw_time remix rstart in 38 37 let raw_inc = Time.time_to_raw_time remix (Time.Seconds 0.2) in 39 38 let dev = Oss.sound_open () in … … 42 41 let cond = 43 42 ( 44 if length = Time.Forever43 if rlength = Time.Forever 45 44 then (fun a -> true) 46 45 else ( 47 let raw_length = Time.time_to_raw_time remix length in46 let raw_length = Time.time_to_raw_time remix rlength in 48 47 fun a -> a < raw_length 49 48 ) … … 51 50 while cond !t do ( 52 51 Oss.sound_write dev 53 (sound remix s ound_extent (Time.Samples !t, Time.Samples raw_inc));52 (sound remix start (Time.Samples !t, Time.Samples raw_inc)); 54 53 t := !t + raw_inc) done; 55 54 Oss.sound_close dev;; ocaml-remix/trunk/sound.mli
r508 r514 1 1 type raw_sound = int array;; 2 2 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 *) 11 type sound = Remix.remix -> Time.time -> Time.extent -> raw_sound;; 6 12 7 13 val silence : sound;; 8 14 9 (** write_sound remix output sound start length *) 10 val write : Remix.remix -> 'a IO.output -> sound ->Time.extent -> unit;;15 val write : 'a IO.output -> Remix.remix -> sound -> Time.time -> 16 Time.extent -> unit;; 11 17 12 val preview : Remix.remix -> sound -> Time. extent-> Time.extent -> unit;;18 val preview : Remix.remix -> sound -> Time.time -> Time.extent -> unit;; 13 19 14 20 val raw_preview : Remix.remix -> raw_sound -> unit;; ocaml-remix/trunk/sound_test_sin.ml
r499 r514 7 7 8 8 let 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.811 (Time.Seconds 0.1);;9 let four_second_A = Wave.sin (Volume.ramp (Time.Seconds len) 0. amp) rate;; 10 let envelope = Envelope.adsr (Time.Seconds len) (Time.Seconds 0.1) 11 (Time.Seconds 0.4) 0.8 (Time.Seconds 0.1);; 12 12 13 13 let env_a = Envelope.apply envelope four_second_A;; … … 19 19 Sound.write r out env_a (Time.Seconds 0., Time.Seconds len);; 20 20 *) 21 Sound.preview r env_a (Time.Seconds 0.0, Time.Seconds len) 22 (Time.Seconds 0.0, Time.Seconds len);; 21 Sound.preview r env_a (Time.Seconds 0.0) (Time.Seconds 0.0, Time.Seconds len);; ocaml-remix/trunk/track.ml
r513 r514 39 39 exception OutOfBounds;; 40 40 let apply_transparency sound1 extent1 sound2 extent2 transparency text 41 remix oext rext =41 remix start rext = 42 42 let raw_sound1 = sound1 remix extent1 rext in 43 43 let raw_sound2 = sound2 remix extent2 rext in … … 121 121 let trans = elem.tce_transparency in 122 122 let raw_start, l = Time.extent_to_raw_time remix ext in 123 _add_s_to_out (apply_transparency sound1 ext sound2 ext123 _add_s_to_out (apply_transparency sound1 (fst ext) sound2 (fst ext) 124 124 trans (elem.tce_start, elem.tce_length)) raw_start l; 125 125 _render_at_2 (raw_start + l) (raw_length - l) r ocaml-remix/trunk/track_test_sins.ml
r512 r514 2 2 a period of 100 samples *) 3 3 4 let env = Envelope.adsr(Time.Seconds 0.2) (Time.Seconds 0.6) 0.74 let env t = Envelope.adsr t (Time.Seconds 0.2) (Time.Seconds 0.6) 0.7 5 5 (Time.Seconds 0.1);; 6 6 let a = Wave.sin (Volume.constant 30000.) (Tone.note Tone.a 0);; 7 7 let 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];; 8 let a t = Envelope.apply (env t) a;; 9 let e t = Envelope.apply (env t) e;; 10 let a t = Note.from_sound (a t);; 11 let e t = Note.from_sound (e t);; 10 12 13 let add layer note start length = 14 Layer.add_note layer (note (Time.Seconds length)) 15 (Time.Seconds start, Time.Seconds length);; 11 16 12 17 (* AAAA AAAA EEEE … … 17 22 let trans1 = Transparency.linear_ramp 0.0 1.0;; 18 23 let 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);;24 add layer1 a 0.0 4.0;; 25 add layer1 a 6.0 4.0;; 26 add layer1 e 11.0 4.0;; 22 27 23 28 let noise = Ladspa_raw.open_plugin "noise:noise_white";; … … 31 36 32 37 let 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);;38 add layer2 e 0.0 4.0;; 39 add layer2 e 5.0 6.0;; 40 add layer2 a 12.0 4.0;; 36 41 37 42 let layer3 = Layer.empty();; … … 44 49 let plugin = Note.from_ladspa ladspa (Note.ladspa_init setup_plugin);; 45 50 Layer.add_note layer3 plugin (Time.Seconds 2.0, Time.Seconds 7.0);; 46 let trans = Transparency. linear_ramp 1.01.0;;51 let trans = Transparency.constant 1.0;; 47 52 Layer.add_transparency layer3 trans (Time.Seconds 0.0, Time.Seconds 2.0);; 48 53 Layer.add_transparency layer3 trans (Time.Seconds 9.0, Time.Seconds 16.0);; … … 57 62 58 63 let remix = Remix.create 44000 2 120.;; 59 Sound.preview remix sound extentextent;;64 Sound.preview remix sound (Time.Seconds 0.0) extent;; ocaml-remix/trunk/transparency.ml
r513 r514 4 4 raw_transparency;; 5 5 6 let linear_ramp start_val end_val remix (start, length) (ostart,olength) =6 let linear_ramp start_val end_val remix (start, length) (ostart,olength) = 7 7 let _to_raw a = (Time.time_to_raw_time remix a) * Remix.channels remix in 8 8 let raw_start = _to_raw start in … … 17 17 Array.init raw_olength _fill;; 18 18 19 let constant value remix size(rstart, rlength) =19 let constant value remix (start, len) (rstart, rlength) = 20 20 Array.create (Time.time_to_raw_samples remix rlength) value;; ocaml-remix/trunk/volume.ml
r512 r514 1 1 type volume = 2 2 | Constant of float 3 | Linear of float * float;;3 | Linear of Time.time * float * float;; 4 4 5 5 exception AmplitudeTooHigh … … 8 8 Constant v;; 9 9 10 let ramp s e = if (s > 32767. || s < 0.) then raise AmplitudeTooHigh;10 let ramp l s e = if (s > 32767. || s < 0.) then raise AmplitudeTooHigh; 11 11 if (e > 32767. || e < 0.) then raise AmplitudeTooHigh; 12 Linear ( s, e);;12 Linear (l, s, e);; 13 13 14 let amplitude remix volume position length=14 let amplitude remix volume position = 15 15 match volume with 16 16 | Constant v -> v 17 | Linear ( s, e) -> (18 if l ength= Time.Forever then17 | Linear (l, s, e) -> ( 18 if l = Time.Forever then 19 19 Printf.printf "I can't ramp forever\n"; 20 let raw_length = Time.time_to_raw_time remix l engthin20 let raw_length = Time.time_to_raw_time remix l in 21 21 (float_of_int position /. float_of_int raw_length) *. (e -. s) +. s 22 22 );; ocaml-remix/trunk/volume.mli
r512 r514 2 2 3 3 val constant : float -> volume;; 4 val ramp : float -> float -> volume;;5 val amplitude : Remix.remix -> volume -> int -> Time.time ->float;;4 val ramp : Time.time -> float -> float -> volume;; 5 val amplitude : Remix.remix -> volume -> int -> float;; ocaml-remix/trunk/wave.ml
r512 r514 29 29 (mod_float (float_of_int stime) period) /. period *. amp);; 30 30 31 let generate generator volume rate remix (ss, slen)(start, length) =31 let generate generator volume rate remix ss (start, length) = 32 32 let raw_length = Time.time_to_raw_samples remix length in 33 33 let raw_start = Time.time_to_raw_samples remix start in … … 37 37 let offset = (raw_start - raw_ss) in 38 38 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 40 41 Array.init raw_length 41 42 (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 *) 1 2 type wave = Volume.volume -> float -> Sound.sound;; 2 3
