aboutsummaryrefslogtreecommitdiff
path: root/site/udo/tab2wav.udo
blob: 1c0b9cbde43e0a689859d2afe384f945b6e7ad92 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef UDO_TAB2WAV
#define UDO_TAB2WAV ##

/*
	Table to wave to table file ease tool

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


/*
	Output table to 16bit WAV file

	kdone tab2wav ifn, Spath [, ilen, iprocrate, iblocksize]

	kdone		trigger to specify when done
	ifn			table number to use
	Spath		file path to output to
	isamples	how many samples to save, defaults to full length
	iprocrate	processing rate in processes per second, defaults to 10
	iblocksize	block size in one process cycle, defaults to 2048
*/
opcode tab2wav, k, iSjjj
	ifn, Spath, isamples, iprocrate, iblocksize xin
	
	iprocrate = (iprocrate == -1) ? 10 : iprocrate
	iblocksize = (iblocksize == -1) ? 2048 : iblocksize
	
	ichannels = ftchnls(ifn)
	isr = ftsr(ifn)
	ilenraw = (isamples == -1) ? ftlen(ifn) : isamples
	ilen = ilenraw / ichannels

	kdone init 0

	
	if (kdone == 0) then
		; each block processing at iprocrate
		ktrig metro iprocrate
		if (ktrig == 1) then
			kcount = 0
			while (kcount < iblocksize) do
				apos = round:a(lphasor(isr / sr)) ; rounded to deal with stereo offset if required

				; if complete, set kdone
				if (downsamp(apos) >= ilen) then
					kdone = 1
					kgoto complete
				endif

				; read and write position calcs for stereo / reverse
				aposbase = apos * ichannels
				aposrL = aposbase
				aposrR = aposbase + 1
				
				if (ichannels == 2) then
					fout Spath, 14, table:a(aposrL, ifn), table:a(aposrR, ifn)				
				else
					fout Spath, 14, table:a(aposrL, ifn)
				endif			
				kcount += 1
			od
		endif

	endif
	
complete:
	xout kdone
endop




/*
	Load file to existing table without altering length/channels

	kdone wav2tab Spath, ifn [, imonochannel=0]

	kdone			trigger to specify when done
	Spath			file path to load from
	ifn				table number to use
	imonochannel	if ifn is mono and Spath is stereo, 1 specifies to load from the right channel, rather than the default (0) which is left
	iprocrate		processing rate in processes per second, defaults to 10
	iblocksize		block size in one process cycle, defaults to 2048
*/
opcode wav2tab, k, Siojj
	Spath, ifn, imonochannel, iprocrate, iblocksize xin
	iprocrate = (iprocrate == -1) ? 10 : iprocrate
	iblocksize = (iblocksize == -1) ? 2048 : iblocksize
	
	ifilechannels = filenchnls(Spath)
	ichannels = ftchnls(ifn)
	isr = ftsr(ifn)
	ilenraw = ftlen(ifn)
	ilen = ilenraw / ichannels

	kdone init 0

	
	if (kdone == 0) then
		; each block processing at iprocrate
		ktrig metro iprocrate
		if (ktrig == 1) then
			kcount = 0
			while (kcount < iblocksize) do
				apos = round:a(lphasor(isr / sr)) ; rounded to deal with stereo offset if required

				; if complete, set kdone
				if (downsamp(apos) >= ilen) then
					kdone = 1
					kgoto complete
				endif

				if (ifilechannels == 2) then
					aL, aR diskin Spath, 1
				else
					aL diskin Spath, 1
					aR = aL
				endif

				; read and write position calcs for stereo / reverse
				aposbase = apos * ichannels
				aposwL = aposbase
				aposwR = aposbase + 1
				
				
				if (ichannels == 2) then
					tablew aL, aposwL, ifn
					tablew aR, aposwR, ifn
				else
					if (imonochannel == 1 && ifilechannels == 2) then
						tablew aR, aposwL, ifn
					else
						tablew aL, aposwL, ifn
					endif
				endif			
				kcount += 1
			od
		endif

	endif
	
complete:
	xout kdone
endop


#end