我创建了一个p5草图,该草图使用与Arduino连接的电位计来改变声音的频率。我有第二个电位器,想让它控制另一种声音,但是我不确定该怎么做。
有没有一种方法可以更改或添加到我的草图中,以便可以一次用2个电位器来更改声音?
var serial; // variable to hold an instance of the serialport library
var options = {
baudRate: 9600
}; // set baudrate to 9600; must match Arduino baudrate
var inData; // for incoming serial data
var slider;
let carrier; // this is the oscillator we will hear
let modulator
let analyzer; // we'll use this visualize the waveform
// the carrier frequency pre-modulation
let carrierBaseFreq = 220;
// min/max ranges for modulator
let modMaxFreq = 112;
let modMinFreq = 0;
let modMaxDepth = 150;
let modMinDepth = -150;
let ctx, ctxOn;
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
console.log(i + " " + portList[i]);
}
}
var portName = '/dev/cu.usbmodem14601'; // fill in your serial port name here
function serverConnected() {
console.log('connected to server.');
}
function portOpen() {
console.log('the serial port opened.')
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
function portClose() {
console.log('The serial port closed.');
}
function serialEvent() {
inData = Number(serial.read());
}
function draw() {
background(30);
toggleAudio(inData);
// map mouseY to modulator freq between a maximum and minimum frequency
let modFreq = map(mouseY, height, 0, modMinFreq, modMaxFreq);
modulator.freq(modFreq);
// change the amplitude of the modulator
// negative amp reverses the sawtooth waveform, and sounds percussive
//
let modDepth = map(mouseX, 0, width, modMinDepth, modMaxDepth);
modulator.amp(modDepth);
// analyze the waveform
waveform = analyzer.waveform();
// draw the shape of the waveform
stroke(255);
strokeWeight(10);
beginShape();
for (let i = 0; i < waveform.length; i++) {
let x = map(i, 0, waveform.length, 0, width);
let y = map(waveform[i], -1, 1, -height / 2, height / 2);
vertex(x, y + height / 2);
}
endShape();
strokeWeight(1);
text("sensor value: " + inData, 30, 30);
// add a note about what's happening
text('Modulator Frequency: ' + modFreq.toFixed(3) + ' Hz', 20, 20);
text(
'Modulator Amplitude (Modulation Depth): ' + modDepth.toFixed(3),
20,
40
);
text(
'Carrier Frequency (pre-modulation): ' + carrierBaseFreq + ' Hz',
width / 2,
20
);
}
// helper function to toggle sound
function setup() {
ctx = getAudioContext();
ctxOn = createButton('turn on Audio');
ctxOn.mousePressed(() => {
ctx.resume().then(() => {
console.log('Audio Context is now ON');
ctxOn.hide();
});
});
let cnv = createCanvas(800, 400);
noFill();
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.open(portName, options); // open a serial port @ 9600
carrier = new p5.Oscillator('sine');
carrier.amp(0); // set amplitude
carrier.freq(carrierBaseFreq); // set frequency
carrier.start(); // start oscillating
// try changing the type to 'square', 'sine' or 'triangle'
modulator = new p5.Oscillator('sawtooth');
modulator.start();
// add the modulator's output to modulate the carrier's frequency
modulator.disconnect();
carrier.freq(modulator);
// create an FFT to analyze the audio
analyzer = new p5.FFT();
}
function toggleAudio(inData) {
inData && carrier.amp(inData/10 - 0.1, 0.01);
enter code here
function serialEvent() {
// inData = Number(serial.read()); // can use this when just looking for 1 byte msgs from Arduino
// Alternatively, read a string from the serial port, looking for new line as data separator:
var inString = serial.readStringUntil('\r\n');
// check to see that there's actually a string there:
if (inString.length > 0) {
// convert it to a number:
inData = Number(inString);
}
}
}
那是我的草图。任何帮助或指导都将很棒!