我想产生一个振幅和频率可以动态变化的正弦波。我无法使用预先存在的音频文件。包media
的文档中写道:
在某些情况下,容器格式可能只是包含编码数据的基本流。
我打算使用表示振幅(脉冲编码调制(的值流来播放音频。这是我的目标的伪代码:
double frequency;
double interval; //interval between successive additions to the audio stream
double time = 0;
streamObject; //some stream object which contains amplitudes as integers in range [-128,128]
while (true){
double value = Math.sin(time*2*Math.PI*frequency);
streamObject.write( (int)(value*128) ); //this is some method which will append values to the end of the stream
time += interval;
wait(interval); //appending values in real-time because there may be changes to the frequency
}
我的问题是类Media
的唯一构造函数采用URI参数。AudioClip
也是如此。我希望他们能把Stream
作为一个参数。
任何建议或建议都将是有益的。
据我所知,目前没有一种方法可以用JavaFX实现您想要的功能。
我建议您的输出线路使用javax.sound.sampled.SourceDataLine
。我已经将这个类用于类似theremin的实时程序、实时FM合成器和名为AudioCue的Clip
的开源增强版。
基本计划如下:
- 打开具有给定
AudioFormat
的输出线(SourceDataLine
( - 生成PCM值(例如,范围从-1到1的浮点值(,表示所需波形(一个缓冲区的值(
- 根据
AudioFormat
的要求将PCM转换为字节 - 使用
SourceDataLine
方法写入 - 在声音持续时间内循环回(2(