On the way to fun things
Snapshot from Will Hurt's work AP1
Snapshot from Will Hurt's work AP1
其他還有 mousePressed、keyDown 等 function
在這兩個 function 中可以 call p5 methods
function setup() {
createCanvas(400, 400);
background(0, 0, 0);
}
function draw() {
rect(width / 2, height / 2, 24, 24);
}
function sketch(p) {
p.setup = function() {
p.createCanvas(p.width, p.height);
}
p.draw = function() {
p.background(155, 100, 40);
p.rect(p.mouseX, p.mouseY, 40, 40);
}
}
new p5(
sketch,
document.getElemendById('myCanvas')
);
讓它可以跟 webpack、React 等一起使用,會需要 namespacing 的做法
減法合成 Subtractive Synthesis
產生聲波
去掉不要的頻段
控制聲音的生命週期
+
Oscillator
Filter
Envelope
產生聲波
去掉不要的頻段
控制聲音的生命週期
有四種波形分類:
設定音高用 frequency
設定音量用 amp(amplitude)
example
const osc = new Tone.Oscillator(440, 'triangle');
function setup() {
createCanvas(windowWidth, windowHeight);
background(223, 180, 76);
osc.toDestination();
}
function keyPressed() {
Tone.start();
osc.start();
background(155, 41, 21);
}
function keyReleased() {
osc.stop();
background(223, 180, 76);
}
常見的 3 種 filter:
resonance(res) 控制下降幅度
frequency(cut-off) 控制高峰點
example
const osc = new Tone.Oscillator(440, 'triangle');
const filter = new Tone.Filter(20, 'bandpass');
osc.connect(filter);
filter.toDestination();
function setup() {
createCanvas(windowWidth, windowHeight);
background(223, 180, 76);
}
function draw() {
const freq = map(mouseX, 0, width, 10, 22050);
filter.set({ frequency: freq });
// filter.frequency.rampTo(freq);
}
function keyPressed() {
Tone.start();
osc.start();
background(155, 41, 21);
}
function keyReleased() {
osc.stop();
background(223, 180, 76);
}
example
let isPlaying = false;
// 生出一個振盪器
const osc = new p5.Oscillator('triangle');
osc.freq(440);
osc.amp(0.5);
// 生出一個濾波器
const filter = new p5.Filter('lowpass');
filter.freq(261);
filter.res(80);
// 讓濾波器跟振盪器連在一起
osc.connect(filter);
function setup() {
createCanvas(windowWidth, windowHeight);
}
function mouseClicked() {
if (isPlaying) {
isPlaying = false;
osc.stop();
} else {
isPlaying = true;
osc.start();
}
}
ADSR
通常另外會設定 Attack Level & Sustain Level
example
const osc = new Tone.Oscillator(440, 'triangle');
const filter = new Tone.Filter(261, 'lowpass');
const env = new Tone.Envelope({
attack: 0.2,
decay: 0.1,
sustain: 0.5,
release: 0.4,
});
osc.start();
osc.connect(env);
env.connect(filter);
filter.toDestination();
function setup() {
createCanvas(windowWidth, windowHeight);
background(223, 180, 76);
}
function keyPressed() {
Tone.start();
env.triggerAttack();
background(155, 41, 21);
}
function keyReleased() {
env.triggerRelease();
background(223, 180, 76);
}
如果以上還是覺得不好玩...
Modulation Technique
Sound Effects
...等
Fin.