我在使用Tone.js从buffer对象创建声音时遇到了一些困难
这应该很简单,但我在不熟悉的领域。越来越无用的建议,从chatGPT困惑我非常。
下面是代码(我知道代码有点乱,很抱歉,我甚至不能简化它)
import React, { useEffect, useState } from 'react';
import * as Tone from 'tone';
function AudioPlayer() {
const [isPlaying, setIsPlaying] = useState(false);
const chunkSize = 1024;
const bufferArray = new Float32Array(chunkSize);
const sampleRate = 16384;
let t = 0;
let x = 0;
const context = Tone.context;
const buffer = context.createBuffer(1, chunkSize, sampleRate);
// const source = context.createBufferSource(); <= do I really need this!
const player = new Tone.Player(buffer).toDestination();
useEffect(() => {
while (isPlaying){
for (let i = 0; i < chunkSize; i++) {
bufferArray[i] = Math.sin(2 * Math.PI * 440 * x);
t++;
x = t / sampleRate;
}
// buffer.fromArray(bufferArray); <= is there a simple method that just override on buffer?
}
}, [isPlaying]);
const handlePlayClick = async () => {
setIsPlaying(true);
player.start()
};
const handleStopClick = () => {
setIsPlaying(false);
player.stop()
};
return (
<div>
{!isPlaying && (
<button onClick={handlePlayClick}>Play</button>
)}
{isPlaying && (
<button onClick={handleStopClick}>Stop</button>
)}
</div>
);
}
export default AudioPlayer;
我只想填充buffer(bufferArray[chunkSize])
并覆盖bufferArray
并连续播放它。
2条答案
按热度按时间vngu2lb81#
我几乎不知道我在做什么,我不确定我是否理解这个问题,但我玩了一点;以下内容有帮助吗?:
我看的东西:
juud5qan2#
这里有一个答案-不完全是,但工作-为我问的问题。