summaryrefslogtreecommitdiff
path: root/lib/LiSaX.ck
blob: 7c97585f08a8dd52b0c375ee080a6703eba72aca (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
/*
 * Version of LiSa that supports reading in files (like a SndBuf)
 * LiSa supports only one channel so mixing can be performed
 */
public class LiSaX extends LiSa {
	/*
	 * Read in file, mixing its channels using a gain vector
	 * mix == null means to mix all of the channels with gain 1/channels
	 */
	fun void
	read(string file, float mix[])
	{
		SndBuf buf;

		file => buf.read;
		/*
		 * buf.samples() returns number of frames
		 * (i.e. samples in one channel)
		 */
		buf.samples()::samp => duration;

		for (0 => int frame; frame < buf.samples(); frame++) {
			0 => float v;

			for (0 => int c; c < buf.channels(); c++) {
				float g;

				if (mix == null)
					1.0/buf.channels() => g;
				else
					mix[c] => g;
				buf.valueAt(frame*buf.channels() + c)*g +=> v;
			}

			valueAt(v, frame::samp);
		}
	}

	/*
	 * Read in file, mixing its channels equally
	 */
	fun void
	read(string file)
	{
		read(file, null);
	}
}