[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] round to nearest item in a collection




> On 25. Feb 2019, at 20:29, <eli.fieldsteel@xxxxxxxxx> <eli.fieldsteel@xxxxxxxxx> wrote:
> 
> Hi List,
> 
> I'm wondering if there exists a method that rounds a number to the nearest item in a given collection.

> For simple numbers, this might look like this:
> 
> rrand(40.0,90.0).roundToNearest( [ 48, 58, 68 ] );

rrand(40.0,90.0).nearestInList( [ 48, 58, 68 ] );
> 

> Or a more musical version, rounding to a particular scale degree:
> 
> rrand(40.0,90.0).roundToScale(Scale.minorPentatonic);

rrand(40.0,90.0).nearestInScale(Scale.minorPentatonic);

> 
> It'd be nice to be able to use this with UGens, e.g.
> 
> (
> {
> 	var freq, sig;
> 	freq = LFNoise0.kr(6).range(40.0,90.0);
> 	freq = freq.roundToScale(Scale.minorPentatonic);
> 	sig = SinOsc.ar(freq.midicps, 0, 0.1);
> }.play;
> )

IndexInBetween

(
// autotune.
{
   var index, in, autotuned, f0, fdiff;
   var scale = ([0, 1, 3, 4, 7, 11, 12] + 70).midicps;
   var buffer = scale.as(LocalBuf);
   in = Pulse.ar(MouseX.kr(scale.minItem, scale.maxItem)) * 0.1;
   f0 = Pitch.kr(in).at(0);
   index = IndexInBetween.kr(buffer, f0);
   fdiff = index.frac * (Index.kr(buffer, index + 1) - Index.kr(buffer, index));
   autotuned = PitchShift.ar(in, 0.1, 1 - (fdiff / f0), 0.01, 0.01);
   RLPF.ar(autotuned, [2000, 5000], 0.3)
}.play;
)

b.free;


> 
> I realize this can be accomplished in other ways, e.g. demand UGens, patterns, 'round' combined with claver mathematical manipulations, etc. But is there something in the standard library that resembles my imaginary code above? I'm considering creating a method myself, but want to be sure I'm not reinventing the wheel.

I have written it because I wondered, too!






_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: https://listarc.bham.ac.uk/marchives/sc-users/
search: https://listarc.bham.ac.uk/lists/sc-users/search/