p5.js 如何修改声音文件中的特定频率?
How to modify a particular frequency in a sound file in p5.js?
所以基本上我在 p5.js 中制作音频均衡器,但我不知道如何调制特定频率。在声音文件中
如果有人知道请提供一些见解。
我相信您要找的是带阻滤波器。这与带通滤波器相反。带通滤波器使给定中心周围的频率保持不变,同时降低上下频率的幅度。带阻滤波器则相反,它会降低给定中心周围的频率幅度,同时保持上方和下方的频率不变。在 p5.sound 中,这是使用过滤器类型 'notch'
完成的。此过滤器类型(如 p5.BandPass
)没有特殊的 class,因此您只需使用通用 p5.Filter
并将类型传递给构造函数。这是一个基于 p5.sound 参考的示例:
let fft, noise, filter;
function setup() {
let cnv = createCanvas(windowWidth, windowHeight);
cnv.mousePressed(makeNoise);
fill(255, 0, 255);
filter = new p5.Filter('notch');
// (lower res = wider band rejection)
// Note: When using a 'notch' filter, in order to filter out the
// same range a frequencies that you would be filtering for
// using a 'bandpass' filter, you need to use a much smaller res
// value. I'm not sure why this is.
filter.res(0.2);
// This example uses p5.Noise for demonstation purposes, but it
// should work just as well with a p5.SoundFile created with
// loadSound
noise = new p5.Noise();
// Don't play the noise directly
noise.disconnect();
// Instead pass it to the filter (which will be connected to
// audio output by default).
noise.connect(filter);
// This is just for the display
fft = new p5.FFT();
}
function draw() {
background(220);
// set the BandPass frequency based on mouseX
let freq = map(mouseX, 0, width, 20, 22050, true);
filter.freq(freq);
// draw filtered spectrum
let spectrum = fft.analyze();
noStroke();
for (let i = 0; i < spectrum.length; i++) {
let x = map(i, 0, spectrum.length, 0, width);
let h = -height + map(spectrum[i], 0, 255, height, 0);
rect(x, height, width / spectrum.length, h);
}
if (!noise.started) {
text('tap here and drag to change frequency', 10, 20, width - 20);
} else {
text('Frequency: ' + round(freq) + 'Hz', 20, 20, width - 20);
}
}
function makeNoise() {
// see also: `userStartAudio()`
noise.start();
noise.amp(0.2, 0.2);
}
function mouseReleased() {
noise.amp(0, 0.2);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/addons/p5.sound.min.js"></script>
所以基本上我在 p5.js 中制作音频均衡器,但我不知道如何调制特定频率。在声音文件中 如果有人知道请提供一些见解。
我相信您要找的是带阻滤波器。这与带通滤波器相反。带通滤波器使给定中心周围的频率保持不变,同时降低上下频率的幅度。带阻滤波器则相反,它会降低给定中心周围的频率幅度,同时保持上方和下方的频率不变。在 p5.sound 中,这是使用过滤器类型 'notch'
完成的。此过滤器类型(如 p5.BandPass
)没有特殊的 class,因此您只需使用通用 p5.Filter
并将类型传递给构造函数。这是一个基于 p5.sound 参考的示例:
let fft, noise, filter;
function setup() {
let cnv = createCanvas(windowWidth, windowHeight);
cnv.mousePressed(makeNoise);
fill(255, 0, 255);
filter = new p5.Filter('notch');
// (lower res = wider band rejection)
// Note: When using a 'notch' filter, in order to filter out the
// same range a frequencies that you would be filtering for
// using a 'bandpass' filter, you need to use a much smaller res
// value. I'm not sure why this is.
filter.res(0.2);
// This example uses p5.Noise for demonstation purposes, but it
// should work just as well with a p5.SoundFile created with
// loadSound
noise = new p5.Noise();
// Don't play the noise directly
noise.disconnect();
// Instead pass it to the filter (which will be connected to
// audio output by default).
noise.connect(filter);
// This is just for the display
fft = new p5.FFT();
}
function draw() {
background(220);
// set the BandPass frequency based on mouseX
let freq = map(mouseX, 0, width, 20, 22050, true);
filter.freq(freq);
// draw filtered spectrum
let spectrum = fft.analyze();
noStroke();
for (let i = 0; i < spectrum.length; i++) {
let x = map(i, 0, spectrum.length, 0, width);
let h = -height + map(spectrum[i], 0, 255, height, 0);
rect(x, height, width / spectrum.length, h);
}
if (!noise.started) {
text('tap here and drag to change frequency', 10, 20, width - 20);
} else {
text('Frequency: ' + round(freq) + 'Hz', 20, 20, width - 20);
}
}
function makeNoise() {
// see also: `userStartAudio()`
noise.start();
noise.amp(0.2, 0.2);
}
function mouseReleased() {
noise.amp(0, 0.2);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/addons/p5.sound.min.js"></script>