我正在使用Java声音API和Java 1.7.当我在Linux上运行我的应用程序时,我很难快速地从TargetDataline读取以跟上记录的内容(java版“1.7.0_51”,Java(TM)SE运行时环境(版本1.7.0_51-b13),Java HotSpot(TM)64位服务器VM(构建24.51-b03,混合模式),Red Hat Enterprise linux 5).在我的windows 7笔记本电脑上运行相同的程序时,我没有这个问题.我有点难过.
为了隔离这个问题,我编写了一个程序,它从TargetDataline中捕获一段时间(交互式确定)并记录每次在固定字节数的阻塞读取中花费的时间,然后将它们与平均值一起打印出来读取时间,已用时间和捕获的音频时间.
我的测试程序如下:
import java.io.BufferedReader;import java.io.IOException;import java.io.inputStreamReader;import java.util.ArrayList;import java.util.linkedList;import java.util.List;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.Dataline;import javax.sound.sampled.lineUnavailableException;import javax.sound.sampled.mixer;import javax.sound.sampled.TargetDataline;/** * This is a test of mic capture delay for given buffer and fetch settings. */public class MicCaptureDelayTest { /** * the audio format used for capturing and transmitting */ private static final AudioFormat format = new AudioFormat(8000,16,1,true,true); /** * This is the target data line buffer size to request,in bytes. */ private static final int MIC_BUFFER_SIZE = 1000; /** * This is the number of bytes to try to fetch from the target data line at a * time. */ private static final int MIC_FETCH_SIZE = 480; /** * Searches for available mixers on the system that have a microphone. * @return a List of matching mixers */ private static List
当我在linux环境中运行时,大约10秒钟的录音,输出如下:
Starting.StopPing.read times (ms): 54.00 18.10 36.62 36.32 35.99 18.10 18.25 54.26 18.30 35.56 18.12 35.51 36.74 17.22 36.70 35.29 18.33 35.60 18.23 54.72 19.00 37.99 18.14 18.37 53.91 18.37 35.34 36.00 18.00 36.00 18.00 54.71 17.22 18.12 36.18 36.64 36.08 18.00 54.34 18.26 18.27 35.44 18.30 54.77 18.33 18.24 36.51 35.47 36.52 18.35 17.14 54.96 18.13 36.73 17.21 54.95 18.28 18.37 36.54 36.72 35.56 18.37 17.23 54.46 18.36 35.53 18.08 36.00 36.00 17.99 54.30 18.06 35.22 18.00 18.00 53.93 18.32 35.63 36.64 18.16 35.21 18.30 55.65 18.23 18.35 35.55 36.32 35.60 18.30 36.33 36.21 17.22 36.54 18.32 54.96 17.19 18.36 35.62 36.67 35.25 18.29 18.37 54.63 18.37 36.54 18.35 53.91 18.37 17.23 36.70 36.09 36.01 17.19 18.33 53.91 18.37 36.56 18.36 35.53 36.58 18.16 53.84 18.26 36.03 18.0818.12 54.24 18.08 36.14 36.1918.12 36.08 18.11 53.80 18.2818.37 36.55 18.13 53.99 18.0036.12 35.54 18.28 36.56 17.2053.96 18.00 18.01 36.67 36.5336.71 17.19 18.37 54.37 18.0235.97 18.00 54.00 18.00 18.0036.00 35.99 36.34 18.37 18.3553.93 18.13 36.63 18.33 36.3336.34 18.33 36.55 35.51 36.6618.29 18.06 54.00 17.99 36.0818.25 36.64 36.38 18.37 35.5536.66 18.21 36.73 17.19 54.2718.13 35.55 18.18 36.31 35.5618.34 53.90 18.36 18.09 36.1518.22 53.90 18.32 18.37 53.8918.19 36.04 17.20 53.94 18.3118.37 36.55 36.70 36.61 18.3517.18 53.97 18.32 36.55 19.0118.99 57.00 18.99 38.01 18.9838.00 18.99 36.99 36.35 18.3736.55 36.70 18.04 38.00 19.0038.00 37.99 18.99 37.99 19.0037.06 36.43 36.03 18.00 18.0054.47 18.25 36.70 18.22 18.3753.55 18.33 35.59 36.59 18.2935.36 18.37 54.89 18.24 36.4418.33 18.36 53.52 18.13 36.3635.57 18.20 35.52 18.20 53.7818.18 18.16 35.49 36.67 36.5418.37 36.53 36.67 17.19 36.6518.29 54.87 17.14 18.24 36.6835.49 35.61 18.27 18.36 53.7718.24 35.43 18.35 53.90 18.3718.24 38.00 38.00 37.99 18.9919.01 37.98 19.00 57.00 18.9919.00 38.00 18.99 55.01 18.9835.99 18.00 18.01 54.98 18.0037.00 17.99 36.00 36.00 17.9954.01 18.98 18.00 36.02 18.9853.16 18.34 35.59 36.20 17.9836.00 18.00 54.00 17.99 18.0036.00 35.99 36.01 17.99 18.0054.00 17.98 35.99 18.00 54.28Mean read time (ms): 30.210176811594206Time captured (s): 10.35Time elapsed (s): 10.466399
在我的windows环境中类似大约10秒录制的输出如下所示:
Starting.StopPing.read times (ms):44.96 30.13 29.97 29.97 30.0429.96 29.96 30.00 29.99 30.0029.92 30.01 30.02 30.01 29.9929.85 45.12 30.03 29.92 29.9629.98 30.00 29.98 30.00 0.2444.73 29.94 30.04 29.96 29.8629.96 30.05 29.85 30.17 30.0230.00 29.94 29.99 29.99 30.0429.97 44.99 29.99 30.08 29.8830.05 29.95 29.97 29.87 0.1544.95 29.98 29.91 30.08 29.9830.00 30.01 29.96 29.94 30.0430.01 29.96 29.88 30.00 29.9530.04 44.99 29.99 29.96 30.0330.00 30.07 29.94 30.01 0.2144.77 29.95 30.02 30.01 30.0029.96 29.98 30.00 30.00 29.9429.99 30.04 29.93 29.99 30.0229.98 44.99 29.99 29.96 30.0130.03 29.95 30.00 29.97 0.2144.81 29.88 30.05 29.99 29.9930.01 29.97 29.99 29.99 29.9829.99 30.00 29.97 29.98 29.9730.01 44.95 29.97 30.03 30.0030.00 30.00 29.99 29.97 0.2144.79 29.95 30.00 29.99 29.9529.98 29.93 30.06 29.94 30.0829.97 30.00 29.97 29.99 29.9829.94 45.05 30.04 29.91 30.0029.99 29.97 30.01 29.98 0.2144.79 29.94 29.99 29.89 30.0630.03 29.96 30.04 29.98 29.9030.04 30.00 29.98 30.00 29.9730.07 44.96 29.98 29.93 30.0729.98 29.90 30.00 29.94 0.1344.97 29.98 29.99 29.94 30.0230.00 29.93 29.99 30.02 30.0129.99 29.96 30.02 29.90 29.9330.01 45.04 30.06 29.99 29.9829.94 30.04 30.00 29.92 0.2044.83 29.94 29.99 30.00 30.0130.02 29.87 30.03 29.94 30.0329.99 30.00 30.07 29.90 29.9530.05 44.97 30.01 29.98 29.9730.01 29.99 30.00 29.97 0.2144.77 29.96 30.00 30.03 29.9130.00 30.01 30.03 29.93 29.9829.99 29.99 29.93 30.04 30.0430.01 44.92 30.04 29.97 29.9130.08 29.89 29.97 29.88 0.1545.01 30.09 29.89 30.01 30.0129.97 29.95 29.96 30.05 30.0429.88 30.00 29.99 29.94 30.0529.98 44.99 30.01 30.00 29.9929.95 30.00 29.88 30.11 0.2144.78 30.01 29.96 29.99 29.9829.98 29.99 30.01 29.91 29.8230.10 29.99 30.15 29.96 29.9329.98 45.05 29.97 29.99 30.0229.96 29.98 29.95 30.04 0.2144.74 30.02 29.97 29.97 30.0329.99 29.93 29.94 30.07 29.9929.99 29.94 30.02 29.97 29.9030.01 45.12 29.91 30.03 29.9530.03 29.97 29.87 30.09 0.2044.79 29.98 29.97 29.99 30.0130.01 29.97 29.99 29.99 30.0129.99 29.94 30.01 30.00 29.9829.98 45.02 29.97 29.91 30.0629.99 29.96 30.02 29.98Mean read time (ms): 30.073811959885386Time captured (s): 10.47Time elapsed (s): 10.777957116
linux环境的摘要统计信息,大约30秒的记录:
Mean read time (ms): 30.152922254616133Time captured (s): 30.87Time elapsed (s): 31.135111
windows环境的摘要统计信息,大约30秒记录:
Mean read time (ms): 30.020078674852652Time captured (s): 30.54Time elapsed (s): 30.901762071
我注意到,经过的时间和捕获的时间之间的差异随着linux端记录时间的增加而增加.在linux方面,单个获取时间看起来也不那么规律.
我已经尝试调整缓冲区和获取大小,但我还没有找到一个允许从线路中快速获取的组合.
什么可能导致取得缓慢?如何确定合理的提取和缓冲区大小,以便具有低延迟但快速提取以及时跟上实时? linux上是否存在可能影响此问题或我应该检查的声音配置问题?
谢谢!
最佳答案private static final int MIC_FETCH_SIZE = 480; // 0.12 seconds of data
这是一个非常小的缓冲区大小,可靠的性能.在16位单声道时,它仅代表240个声音样本.使它更像16000个样本,或者:
private static final int MIC_FETCH_SIZE = 32000; // 2 seconds of data
注意:Java Sound不保证读取数量,而是返回实际读取的字节数.关键是,允许有机会读取最多2秒的数据(如果可用).
我认为这应解决上述大多数问题.
总结以上是内存溢出为你收集整理的linux上的java声音:如何从TargetDataLine中快速捕获到足以跟上?全部内容,希望文章能够帮你解决linux上的java声音:如何从TargetDataLine中快速捕获到足以跟上?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)