-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine_Bitwise.sc
More file actions
69 lines (58 loc) · 1.99 KB
/
Engine_Bitwise.sc
File metadata and controls
69 lines (58 loc) · 1.99 KB
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
// This is an exploration inspired by the idea of the Mask1 synth.
// It's meant for the bitwise Norns script.
// A related application in sample chopping
// Can also add wavetable morphing effects
(
// Create buffer and fill it
b = Buffer.alloc(s, 2048); // 2x size for wavetable format
)
(
// Function to process regular buffer then convert to wavetable
~processBitmask = { |numBits=8, mask=255, operation=\zero|
var size = 1024; // Regular buffer size
var segmentSize = size/numBits;
var signal = Signal.fill(size, { |i|
sin(2pi * i/size) // Generate sine wave
});
// Process each segment according to mask
numBits.do({ |i|
var startIndex = (i * segmentSize).asInteger;
var endIndex = ((i + 1) * segmentSize).asInteger;
// TODO: support contiguous region operations (e.g. flipLong flips all contiguous 1 values of bitmask.)
// TODO take an array of bitmasks and operations, apply in serial
// FIXME /repeat errors if the 1 bit is set to 1
if((mask >> i & 1) == 1, {
startIndex.to(endIndex-1).do({ |j|
signal[j] = switch(operation,
\zero, { 0 },
\invert, { signal[j] * -1 },
\half, { signal[j] * 0.5 },
\double, { signal[j] * 2.0 },
\repeat, { signal[(j-segmentSize).wrap2(size)] },
\fold, { signal[j].fold2(0.5) },
{ signal[j] }
);
});
});
});
// Convert to wavetable format and load into buffer
b.loadCollection(signal.asWavetable);
};
)
(
// b.sine1([1]); // Fill with basic sine wave
~processBitmask.(8, 2r10001000, \repeat);
b.plot
)
(
// Simple synth to play the wavetable
SynthDef(\bitmaskOsc, { |out=0, freq=440, amp=0.5, gate=1|
var sig = Osc.ar(b, freq, 0, amp);
var env = EnvGen.kr(Env.asr(0.01, 1, 0.1), gate, doneAction: 2);
Out.ar(out, sig * env ! 2);
}).add;
)
// Test it
Synth(\bitmaskOsc, [freq: 110]);
Server.freeAll
Server.killAll