User-Defined Opcode Database

scale

Scales incoming value to user-definable range. Similar to scale object found in popular dataflow languages.

Download UDO File

Description

This opcode expects floating point input in range 0-1 and will scale this input to a minimum and maximum value variable definable at k-rate.

This opcode is based on a formula from the Csound opcode "ctrl7" and the source formula originates in

OOps/midiops2.c (of the Csound source tree)

and is Copyright (C) 1997 Gabriel Maldonado.

Csound source code is under the GNU Lesser General Public License and should be reviewed here:

http://www.gnu.org/copyleft/lesser.html

Syntax

kscl scale kin, kmin, kmax

Performance

kin -- Input value. Can originate from any k-rate source as long as that source's output is in range 0-1.

kmin -- Minimum value of the resultant scale operation.

kmax -- Maximum value of the resultant scale operation.

Code

	opcode	scale, k, kkk

kval, kmin, kmax	xin

kscl	=	kval * (kmax - kmin) + kmin

	xout	kscl

	endop

Examples

<CsoundSynthesizer>
<CsInstruments>
;//  example 1 - print the data to make sure it works

sr	=	44100
kr	=	441
ksmps	=	100
nchnls	=	2

/*--- ---*/

	opcode	scale, k, kkk

kval, kmin, kmax	xin

kscl	=	kval * (kmax - kmin) + kmin

	xout	kscl

	endop

/*--- ---*/

		instr	1

ktest	ctrl7	1, 1, 0, 1

kscaled	scale	itest, 0.5, 0.618

	printk2	kscaled

		endin

/*--- ---*/
</CsInstruments>
<CsScore>
i1	0	3600

e
</CsScore>
; # EOF
</CsoundSynthesizer>

<CsoundSynthesizer>
<CsInstruments>
;//  example 2 - more involved.
;//    _udo.h is just references to all the UDO's contained in this database.

sr	=	44100
kr	=	441
ksmps	=	100
nchnls	=	2

itmp	ftgen	1, 0, 16384, 10, 1

#include	"_udo.h"

/*--- ---*/

		instr	1

/*  substitute with midi controll if you do not have a p5 glove ! */
ka, kb, kc, kf1, kf2, kf3, kf4, kf5, kx, ky, kz		p5glove
;//  scale raw input in range 0-1
kavg	=	(((kf1+kf2+kf3+kf4+kf5)/5)/63)
kzee	=	((kz+400)/800)
/*                                                                */

;//  rescale to desired range
kscl	scale	kavg, 4.0, 12.0
kscl2	scale	kzee, 0, 8

;//  volume control
kamp	ctrl7	1, 7, 0, 150
kvol	gainslider	kamp

;//  sound synthesis
aosc	oscil	12000, cpspch(kscl), 1
aset	waveset	aosc, kscl2-1
aout	=	aset*kvol

aclp	clip	aout, 2, 32767

;	printk2	kscl ; for debugging
	outs	aclp, aclp

		endin

/*--- ---*/
</CsInstruments>
<CsScore>
;//  fun !
i1	0	1000

e
</CsScore>
; # EOF
</CsoundSynthesizer>

Credits

David Akbari - 2005. OOps/midiops2.c - Copyright (C) 1997 Gabriel Maldonado


Previous Home Next
pan_equal_power   pan_delay_and_filtered