- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题的简化版本:即使我之前已经让它工作了,这个试图用 javax.sound.sampled.SourceDataLine 制造一些噪音的代码在第一个缓冲区为空后不会调用更新。
编辑:更新了 16 位立体声的代码。
调用 Output.open(format,buffersize) 时定义的样本数量播放声音。
将代码示例提炼为两个类项目:
package javaxaudiotest;
import java.io.IOException;
public class JavaxAudioTest {
public static void main(String[] args) throws IOException {
JavaxAudioDevice audio = new JavaxAudioDevice();
audio.create();
System.out.println("Hit enter to quit.");
int c = 'a';
while (c != '\n') {
c = System.in.read();
}
audio.dispose();
}
}
package javaxaudiotest;
import java.util.Arrays;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.SourceDataLine;
public class JavaxAudioDevice implements LineListener {
byte[] byteBuf = null;
boolean quit = false;
boolean hasquit = false;
boolean underflow = false;
SourceDataLine Output;
int leader = 1024;
boolean notDoneYet = false;
boolean alreadyLead = false;
byte b = 0;
public int create() {
quit = false;
AudioFormat af = new AudioFormat(44100, 16, 2, true, false);
try {
Output = AudioSystem.getSourceDataLine(af);
} catch (Exception e) {
System.err.println("Failed to create audio device. " + e.getLocalizedMessage());
return 1;
}
if (Output != null) {
Output.addLineListener(this);
try {
Output.open(af,44100*8);
} catch (Exception e) {
System.err.println("Failed to open audio device. " + e.getLocalizedMessage());
return 2;
}
}
System.err.println("Created JavaxAudioDevice");
while (!Output.isOpen());
update(null);
System.err.println(Output.getFormat().toString());
return 0;
}
public void dispose() {
quit = true;
Output.close();
Output.flush();
Output = null;
hasquit = true;
}
//LineListener
@Override
public void update(LineEvent le) {
if (le == null) System.err.println("Audio update");
else System.err.println("Audio update " + le.toString());
if ( !quit && ( le == null || le.getType() == LineEvent.Type.STOP ) ) {
boolean uf = false;
while (notDoneYet) {
uf = true;
}
notDoneYet = true;
if (uf) {
underflow = true;
System.err.println("underflow");
}
if (byteBuf == null) { //insert some leader tape
short[] shortBuf = new short[44100*8];
for (int i = 0; i < shortBuf.length; i+=2) {
shortBuf[i] = (short)((b++)*50);
shortBuf[i+1] = (short)((b++)*200);
}
byteBuf = new byte[shortBuf.length * 2];
for (int i = 0; i < byteBuf.length; ++i) {
//little endian
if (i % 2 == 0) byteBuf[i] = (byte) (shortBuf[i / 2]); //low byte
else byteBuf[i] = (byte) (shortBuf[i / 2] >> 8); //high byte
}
}
Output.write(byteBuf, 0, byteBuf.length);
Output.start();
byteBuf = null;
if (!Output.isRunning()) { //debug
System.err.println("Output not running");
} else {
System.err.println("Output running");
}
short[] shortBuf = new short[44100*8];
for (int i = 0; i < shortBuf.length; i+=2) {
if (i%2 == 0) shortBuf[i] = shortBuf[i+1] = (short)((b++)*100);
else shortBuf[i] = shortBuf[i+1] = (short)((b++)*150);
}
byteBuf = new byte[shortBuf.length * 2];
for (int i = 0; i < byteBuf.length; ++i) {
//little endian\
if (i % 2 == 0) byteBuf[i] = (byte) (shortBuf[i / 2]); //low byte
else byteBuf[i] = (byte) (shortBuf[i / 2] >> 8); //high byte
}
notDoneYet = false;
}
}
}
run:
Created JavaxAudioDevice
Audio update Open event from line com.sun.media.sound.DirectAudioDevice$DirectSDL@4cac0ef5
Audio update
Output running
Audio update Start event from line com.sun.media.sound.DirectAudioDevice$DirectSDL@4cac0ef5
Hit enter to quit.
PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Audio update Stop event from line com.sun.media.sound.DirectAudioDevice$DirectSDL@4cac0ef5
Audio update Close event from line com.sun.media.sound.DirectAudioDevice$DirectSDL@4cac0ef5
BUILD SUCCESSFUL (total time: 7 seconds)
public class JavaxAudioDevice implements AudioInterface, LineListener {
// omitted stuff
short[] curBuf;
byte[] byteBuf;
SourceDataLine Output;
int leader = 1024; //leader "tape"
@Override
public int create() {
quit = false;
AudioFormat af = new AudioFormat(44100, 16, 2, true, false);
// AudioFormat af = new AudioFormat(44100, 8, 1, true, false);
try {
Output = AudioSystem.getSourceDataLine(af);
} catch (Exception e) {
System.err.println("Failed to create audio device. " + e.getLocalizedMessage());
return 1;
}
if (Output != null) {
try {
Output.open(af,leader);
} catch (Exception e) {
System.err.println("Failed to open audio device. " + e.getLocalizedMessage());
return 2;
}
Output.addLineListener(this);
}
System.err.println("Created JavaxAudioDevice");
byte[] bytes = new byte[leader];
alreadyLead = true;
Output.write(bytes, 0, bytes.length);
Output.start();
System.err.println(Output.getFormat().toString());
return 0;
}
//omitted stuff
//LineListener
@Override
public void update(LineEvent le) {
if (le == null) System.err.println("Audio update");
else System.err.println("Audio update " + le.toString());
if ( !quit && ( le == null || le.getType() == LineEvent.Type.STOP ) ) {
boolean uf = false;
while (notDoneYet) {
uf = true;
}
notDoneYet = true;
if (uf) {
underflow = true;
System.err.println("underflow");
}
if (byteBuf == null) { //insert some leader tape
byteBuf = new byte[leader];
for (int i = 0; i < byteBuf.length; ++i)
byteBuf[i] = 0;
if (alreadyLead) System.err.println("Using audio leader when not supposed to!");
else alreadyLead = true;
}
Output.write(byteBuf, 0, byteBuf.length);
Output.start();
byteBuf = null;
if (!Output.isRunning()) { //debug
System.err.println("Output not running");
} else {
System.err.println("Output running");
}
aplay.call();
curBuf = aplay.output;
if (curBuf == null) {
//I don't know how to help you
notDoneYet = false;
System.err.println("Unable to fill buffer");
return;
}
byteBuf = new byte[curBuf.length * 2];
for (int i = 0; i < byteBuf.length; ++i) {
//little endian
if (i % 2 == 0) byteBuf[i] = (byte) (curBuf[i / 2]/*&255/**/); //low byte
else byteBuf[i] = (byte) (curBuf[i / 2] >> 8); //high byte
}
// byteBuf = new byte[curBuf.length/2]; //debug 8 bit mono
// for (int i = 0; i < byteBuf.length; ++i) {
// byteBuf[i] = (byte) (curBuf[i*2] >> 8);
// }
notDoneYet = false;
}
}
}
Created JavaxAudioDevice
Audio update Start event from line com.sun.media.sound.DirectAudioDevice$DirectSDL@13ab2987
PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Disposing
Audio update Stop event from line com.sun.media.sound.DirectAudioDevice$DirectSDL@13ab2987
Audio update Close event from line com.sun.media.sound.DirectAudioDevice$DirectSDL@13ab2987
:desktop:run
BUILD SUCCESSFUL
Total time: 33.128 secs
最佳答案
好的,我想出了我的答案。我之前对 javax.sound.sampled 主线程的实验涉及生成缓冲区并在检测到声音完成时重新启动声音。在那个实验中,我在这里尝试做的是在 SDL 中使用 LineListener,比如 Mix_HookMusic()(一个更老的实验)。
https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC60
当 SDL_mixer 用完缓冲区时,它会调用您提供的函数来制作更多。这不是 LineListener 在 java 中的工作方式。当缓冲区用完时不会调用它。
相反,把它当作它自己的线程(它是)。当您调用 SourceDataLine.start() 时,它会在其自己的线程中以 start 事件类型启动 LineListener 的事件函数并返回到主线程。因此,当 LineListener 检测到启动事件时,它会进入一个循环,该循环创建一个缓冲区并将其写入设备。 SourceDataLine.write() 当它有一个或更少的缓冲区要通过时返回。这给了循环足够的时间来制作另一个具有恰好一个缓冲区的延迟。 SourceDataLine.stop() 将启动另一个具有停止类型的 LineListener 事件函数。现在事件函数的第二个线程可以设置一个 boolean 值,这样第一个线程就知道是时候退出并退出循环了。
这是一些示例代码,它播放 5 秒钟的音,然后在退出前再静音 5 秒钟。
编辑:似乎至少写一帧(我称之为样本)对于确保 SourceDataLine.start() 能够正常工作很重要。由于我使用的是 16 位立体声,因此我需要 device.write(new byte[4],0,4);至少在我调用 device.start(); 之前
JavaxAudioTest.java
package javaxaudiotest;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class JavaxAudioTest {
public static void main(String[] args) {
SourceDataLine device;
int buffer = 44100/15; //buffer in bytes (4 per sample so 60 buffers per second)
AudioFormat af = new AudioFormat(44100, 16, 2, true, false);
try {
device = (SourceDataLine) AudioSystem.getSourceDataLine(af);
} catch (LineUnavailableException ex) {
Logger.getLogger(JavaxAudioTest.class.getName()).log(Level.SEVERE, null, ex);
return;
}
WaveGen wg = new WaveGen();
JavaxAudioDevice jad = new JavaxAudioDevice();
jad.gen = wg;
jad.device = device;
device.addLineListener(jad);
try {
device.open(af,buffer);
} catch (LineUnavailableException ex) {
Logger.getLogger(JavaxAudioTest.class.getName()).log(Level.SEVERE, null, ex);
return;
}
device.write(new byte[4], 0, 4); //absolutely essential
device.start();
System.out.println("Zzzzzzzzzz");
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(JavaxAudioTest.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("zzzzzzzzzzBWAH!");
device.close();
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(JavaxAudioTest.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Baiyo");
device.drain();
device.close();
}
}
package javaxaudiotest;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.SourceDataLine;
public class JavaxAudioDevice implements LineListener {
boolean quit = false;
byte[] byteBuf;
int SampleRate = 44100;
int buffer = SampleRate/60;
public WaveGen gen;
public SourceDataLine device;
@Override
public void update(LineEvent event) {
System.out.println(event.toString());
if (event.getType() == LineEvent.Type.STOP) {
quit = true;
}
else if (event.getType() == LineEvent.Type.START) {
while (!quit) {
short[] buf = new short[buffer];
for (int i=0; i<buf.length; ++i) {
buf[i] = gen.Output();
}
byteBuf = new byte[buf.length*4];
for (int i=0; i<buf.length; ++i) {
byteBuf[i*4] = byteBuf[i*4+2] = (byte)buf[i];
byteBuf[i*4+1] = byteBuf[i*4+3] = (byte)(buf[i]>>8);
}
device.write(byteBuf, 0, byteBuf.length);
}
}
}
}
package javaxaudiotest;
public class WaveGen {
int Wave = 0;
int Max = 1<<24;
byte Fidelity = 20;
int Level = 8;
int Boost = 2000;
int Stepper = (int)(110.0/44100*Max); //freq/44100*2^24
short Output() {
short ret = 0;
Wave += Stepper;
Wave %= Max;
ret = (short)(( (Wave>>Fidelity)-Level )*Boost);
return ret;
}
}
关于Java SourceDataLine 未在第一个缓冲区结束时调用更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37197292/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!