aboutsummaryrefslogtreecommitdiff
path: root/sonics/sequencing.udo
blob: b81b65a4a29f4fe496b07a8bd81f0921f9d2faa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef UDO_SEQUENCING
#define UDO_SEQUENCING ##
/*
	Sequencing base
	Slim excerpt for Partial Emergence

	This file is part of the SONICS UDO collection by Richard Knight 2021
		License: GPL-2.0-or-later
		http://1bpm.net
*/

gkseq_tempo init 120		; tempo BPM
gkseq_beat init 0			; trigger fired on each beat
gkseq_beattime init 0		; time in seconds of one beat (read only; set by BPM)
gkseq_quartertime init 0	; time in seconds of one quarter beat (read only; set by BPM)
gkseq_beathz init 0			; Hz of one beat (read only; set by BPM)
gkseq_swing init 0.2		; swing amount


/*
	Instrument to control the main beat metronome and beat time globals
*/
instr _seq_manager
	gkseq_beat metro gkseq_tempo / 60
	gkseq_beattime = 60 / gkseq_tempo
	gkseq_quartertime = gkseq_beattime / 4
	gkseq_beathz = (1 / 60) * gkseq_tempo
endin
alwayson "_seq_manager"



/*
	Get the swung time for a given time, if appropriate. If the index given is a second 16th, time will be swung
	
	kresult seq_swingtime ktime, kindex, kswing

	kresult		resulting time
	ktime		the time to consider
	kindex		beat index, beginning with 0
	kswing		the swing amount (0 to 1)
*/
opcode seq_swingtime, k, kkJ
	ktime, kindex, kswing xin
	kswing = (kswing == -1) ? gkseq_swing : kswing
	if ((kindex+1) % 2 == 0) then
		ktime = ktime + (gkseq_quartertime*kswing)
	endif
	xout ktime
endop


/*
	Get the swung time for a given time, if appropriate. If the index given is a second 16th, time will be swung
	
	iresult seq_swingtime itime, iindex, iswing

	iresult		resulting time
	itime		the time to consider
	iindex		beat index, beginning with 0
	iswing		the swing amount (0 to 1)
*/
opcode seq_swingtime, i, iij
	itime, index, iswing xin
	iswing = (iswing == -1) ? i(gkseq_swing) : iswing
	if ((index+1) % 2 == 0) then
		itime = itime + (i(gkseq_quartertime)*iswing)
	endif
	xout itime
endop


/*
	Set the tempo in BPM

	seq_settempo ktempo

	ktempo	the tempo in BPM
*/
opcode seq_settempo, 0, k
	ktempo xin
	gkseq_tempo = ktempo
endop


/*
	Set the tempo in BPM; typically for host control

	p4		the tempo in BPM
*/
instr seq_settempo
	itempo = p4
	gkseq_tempo = itempo
	turnoff
endin



#end