linux上的java声音:如何从TargetDataLine中快速捕获到足以跟上?

前端之家收集整理的这篇文章主要介绍了linux上的java声音:如何从TargetDataLine中快速捕获到足以跟上?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用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中捕获一段时间(交互式确定)并记录每次在固定字节数的阻塞读取中花费的时间,然后将它们与平均值一起打印出来读取时间,已用时间和捕获的音频时间.

我的测试程序如下:

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import javax.sound.sampled.AudioFormat;
  8. import javax.sound.sampled.AudioSystem;
  9. import javax.sound.sampled.DataLine;
  10. import javax.sound.sampled.LineUnavailableException;
  11. import javax.sound.sampled.Mixer;
  12. import javax.sound.sampled.TargetDataLine;
  13. /**
  14. * This is a test of mic capture delay for given buffer and fetch settings.
  15. */
  16. public class MicCaptureDelayTest {
  17. /**
  18. * the audio format used for capturing and transmitting
  19. */
  20. private static final AudioFormat format =
  21. new AudioFormat(8000,16,1,true,true);
  22. /**
  23. * This is the target data line buffer size to request,in bytes.
  24. */
  25. private static final int MIC_BUFFER_SIZE = 1000;
  26. /**
  27. * This is the number of bytes to try to fetch from the target data line at a
  28. * time.
  29. */
  30. private static final int MIC_FETCH_SIZE = 480;
  31. /**
  32. * Searches for available mixers on the system that have a microphone.
  33. * @return a list of matching mixers
  34. */
  35. private static ListdioSystem.getMixerInfo();
  36. ListdioSystem.getMixer(mixerInfo);
  37. DataLine.Info lineInfo = new DataLine.Info(TargetDataLine.class,format);
  38. boolean isSupported = mixer.isLineSupported(lineInfo);
  39. if (isSupported) {
  40. matches.add(mixerInfo);
  41. }
  42. }
  43. return matches;
  44. }
  45. /**
  46. * This is the test recording thread.
  47. */
  48. private static class MicFetcher extends Thread {
  49. /**
  50. * This is the requested recording state.
  51. */
  52. private boolean shouldRecord = false;
  53. /**
  54. * This is the current processed recording state of the thread.
  55. */
  56. private boolean isRecording = false;
  57. /**
  58. * This is the Java audio interface line microphone data is captured from.
  59. */
  60. private TargetDataLine lineFromMic;
  61. /**
  62. * Runs the test mic capture thread body.
  63. */
  64. @Override
  65. public void run() {
  66. ListdioSystem.getMixer(matchingMixerInfo.get(0));
  67. DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);
  68. try {
  69. lineFromMic = (TargetDataLine) mixerToUse.getLine(info);
  70. lineFromMic.open(format,MIC_BUFFER_SIZE);
  71. } catch (LineUnavailableException e) {
  72. e.printStackTrace();
  73. return;
  74. }
  75. byte[] transferBuffer = new byte[MIC_FETCH_SIZE];
  76. List

当我在Linux环境中运行时,大约10秒钟的录音,输出如下:

  1. Starting.
  2. Stopping.
  3. read times (ms):
  4. 54.00 18.10 36.62 36.32 35.99
  5. 18.10 18.25 54.26 18.30 35.56
  6. 18.12 35.51 36.74 17.22 36.70
  7. 35.29 18.33 35.60 18.23 54.72
  8. 19.00 37.99 18.14 18.37 53.91
  9. 18.37 35.34 36.00 18.00 36.00
  10. 18.00 54.71 17.22 18.12 36.18
  11. 36.64 36.08 18.00 54.34 18.26
  12. 18.27 35.44 18.30 54.77 18.33
  13. 18.24 36.51 35.47 36.52 18.35
  14. 17.14 54.96 18.13 36.73 17.21
  15. 54.95 18.28 18.37 36.54 36.72
  16. 35.56 18.37 17.23 54.46 18.36
  17. 35.53 18.08 36.00 36.00 17.99
  18. 54.30 18.06 35.22 18.00 18.00
  19. 53.93 18.32 35.63 36.64 18.16
  20. 35.21 18.30 55.65 18.23 18.35
  21. 35.55 36.32 35.60 18.30 36.33
  22. 36.21 17.22 36.54 18.32 54.96
  23. 17.19 18.36 35.62 36.67 35.25
  24. 18.29 18.37 54.63 18.37 36.54
  25. 18.35 53.91 18.37 17.23 36.70
  26. 36.09 36.01 17.19 18.33 53.91
  27. 18.37 36.56 18.36 35.53 36.58
  28. 18.16 53.84 18.26 36.03 18.08
  29. 18.12 54.24 18.08 36.14 36.19
  30. 18.12 36.08 18.11 53.80 18.28
  31. 18.37 36.55 18.13 53.99 18.00
  32. 36.12 35.54 18.28 36.56 17.20
  33. 53.96 18.00 18.01 36.67 36.53
  34. 36.71 17.19 18.37 54.37 18.02
  35. 35.97 18.00 54.00 18.00 18.00
  36. 36.00 35.99 36.34 18.37 18.35
  37. 53.93 18.13 36.63 18.33 36.33
  38. 36.34 18.33 36.55 35.51 36.66
  39. 18.29 18.06 54.00 17.99 36.08
  40. 18.25 36.64 36.38 18.37 35.55
  41. 36.66 18.21 36.73 17.19 54.27
  42. 18.13 35.55 18.18 36.31 35.56
  43. 18.34 53.90 18.36 18.09 36.15
  44. 18.22 53.90 18.32 18.37 53.89
  45. 18.19 36.04 17.20 53.94 18.31
  46. 18.37 36.55 36.70 36.61 18.35
  47. 17.18 53.97 18.32 36.55 19.01
  48. 18.99 57.00 18.99 38.01 18.98
  49. 38.00 18.99 36.99 36.35 18.37
  50. 36.55 36.70 18.04 38.00 19.00
  51. 38.00 37.99 18.99 37.99 19.00
  52. 37.06 36.43 36.03 18.00 18.00
  53. 54.47 18.25 36.70 18.22 18.37
  54. 53.55 18.33 35.59 36.59 18.29
  55. 35.36 18.37 54.89 18.24 36.44
  56. 18.33 18.36 53.52 18.13 36.36
  57. 35.57 18.20 35.52 18.20 53.78
  58. 18.18 18.16 35.49 36.67 36.54
  59. 18.37 36.53 36.67 17.19 36.65
  60. 18.29 54.87 17.14 18.24 36.68
  61. 35.49 35.61 18.27 18.36 53.77
  62. 18.24 35.43 18.35 53.90 18.37
  63. 18.24 38.00 38.00 37.99 18.99
  64. 19.01 37.98 19.00 57.00 18.99
  65. 19.00 38.00 18.99 55.01 18.98
  66. 35.99 18.00 18.01 54.98 18.00
  67. 37.00 17.99 36.00 36.00 17.99
  68. 54.01 18.98 18.00 36.02 18.98
  69. 53.16 18.34 35.59 36.20 17.98
  70. 36.00 18.00 54.00 17.99 18.00
  71. 36.00 35.99 36.01 17.99 18.00
  72. 54.00 17.98 35.99 18.00 54.28
  73. Mean read time (ms): 30.210176811594206
  74. Time captured (s): 10.35
  75. Time elapsed (s): 10.466399

在我的Windows环境中类似大约10秒录制的输出如下所示:

  1. Starting.
  2. Stopping.
  3. read times (ms):
  4. 44.96 30.13 29.97 29.97 30.04
  5. 29.96 29.96 30.00 29.99 30.00
  6. 29.92 30.01 30.02 30.01 29.99
  7. 29.85 45.12 30.03 29.92 29.96
  8. 29.98 30.00 29.98 30.00 0.24
  9. 44.73 29.94 30.04 29.96 29.86
  10. 29.96 30.05 29.85 30.17 30.02
  11. 30.00 29.94 29.99 29.99 30.04
  12. 29.97 44.99 29.99 30.08 29.88
  13. 30.05 29.95 29.97 29.87 0.15
  14. 44.95 29.98 29.91 30.08 29.98
  15. 30.00 30.01 29.96 29.94 30.04
  16. 30.01 29.96 29.88 30.00 29.95
  17. 30.04 44.99 29.99 29.96 30.03
  18. 30.00 30.07 29.94 30.01 0.21
  19. 44.77 29.95 30.02 30.01 30.00
  20. 29.96 29.98 30.00 30.00 29.94
  21. 29.99 30.04 29.93 29.99 30.02
  22. 29.98 44.99 29.99 29.96 30.01
  23. 30.03 29.95 30.00 29.97 0.21
  24. 44.81 29.88 30.05 29.99 29.99
  25. 30.01 29.97 29.99 29.99 29.98
  26. 29.99 30.00 29.97 29.98 29.97
  27. 30.01 44.95 29.97 30.03 30.00
  28. 30.00 30.00 29.99 29.97 0.21
  29. 44.79 29.95 30.00 29.99 29.95
  30. 29.98 29.93 30.06 29.94 30.08
  31. 29.97 30.00 29.97 29.99 29.98
  32. 29.94 45.05 30.04 29.91 30.00
  33. 29.99 29.97 30.01 29.98 0.21
  34. 44.79 29.94 29.99 29.89 30.06
  35. 30.03 29.96 30.04 29.98 29.90
  36. 30.04 30.00 29.98 30.00 29.97
  37. 30.07 44.96 29.98 29.93 30.07
  38. 29.98 29.90 30.00 29.94 0.13
  39. 44.97 29.98 29.99 29.94 30.02
  40. 30.00 29.93 29.99 30.02 30.01
  41. 29.99 29.96 30.02 29.90 29.93
  42. 30.01 45.04 30.06 29.99 29.98
  43. 29.94 30.04 30.00 29.92 0.20
  44. 44.83 29.94 29.99 30.00 30.01
  45. 30.02 29.87 30.03 29.94 30.03
  46. 29.99 30.00 30.07 29.90 29.95
  47. 30.05 44.97 30.01 29.98 29.97
  48. 30.01 29.99 30.00 29.97 0.21
  49. 44.77 29.96 30.00 30.03 29.91
  50. 30.00 30.01 30.03 29.93 29.98
  51. 29.99 29.99 29.93 30.04 30.04
  52. 30.01 44.92 30.04 29.97 29.91
  53. 30.08 29.89 29.97 29.88 0.15
  54. 45.01 30.09 29.89 30.01 30.01
  55. 29.97 29.95 29.96 30.05 30.04
  56. 29.88 30.00 29.99 29.94 30.05
  57. 29.98 44.99 30.01 30.00 29.99
  58. 29.95 30.00 29.88 30.11 0.21
  59. 44.78 30.01 29.96 29.99 29.98
  60. 29.98 29.99 30.01 29.91 29.82
  61. 30.10 29.99 30.15 29.96 29.93
  62. 29.98 45.05 29.97 29.99 30.02
  63. 29.96 29.98 29.95 30.04 0.21
  64. 44.74 30.02 29.97 29.97 30.03
  65. 29.99 29.93 29.94 30.07 29.99
  66. 29.99 29.94 30.02 29.97 29.90
  67. 30.01 45.12 29.91 30.03 29.95
  68. 30.03 29.97 29.87 30.09 0.20
  69. 44.79 29.98 29.97 29.99 30.01
  70. 30.01 29.97 29.99 29.99 30.01
  71. 29.99 29.94 30.01 30.00 29.98
  72. 29.98 45.02 29.97 29.91 30.06
  73. 29.99 29.96 30.02 29.98
  74. Mean read time (ms): 30.073811959885386
  75. Time captured (s): 10.47
  76. Time elapsed (s): 10.777957116

Linux环境的摘要统计信息,大约30秒的记录:

  1. Mean read time (ms): 30.152922254616133
  2. Time captured (s): 30.87
  3. Time elapsed (s): 31.135111

Windows环境的摘要统计信息,大约30秒记录:

  1. Mean read time (ms): 30.020078674852652
  2. Time captured (s): 30.54
  3. Time elapsed (s): 30.901762071

我注意到,经过的时间和捕获的时间之间的差异随着Linux端记录时间的增加增加.在Linux方面,单个获取时间看起来也不那么规律.

我已经尝试调整缓冲区和获取大小,但我还没有找到一个允许从线路中快速获取的组合.

什么可能导致取得缓慢?如何确定合理的提取和缓冲区大小,以便具有低延迟但快速提取以及时跟上实时? Linux上是否存在可能影响此问题或我应该检查的声音配置问题?

谢谢!

最佳答案
  1. private static final int MIC_FETCH_SIZE = 480; // 0.12 seconds of data

这是一个非常小的缓冲区大小,可靠的性能.在16位单声道时,它仅代表240个声音样本.使它更像16000个样本,或者:

  1. private static final int MIC_FETCH_SIZE = 32000; // 2 seconds of data

注意:Java Sound不保证读取数量,而是返回实际读取的字节数.关键是,允许有机会读取最多2秒的数据(如果可用).

我认为这应解决上述大多数问题.

猜你在找的Linux相关文章