在开始录制之前我是否必须猜测样本的采样率?

发布于 2024-11-28 02:33:08 字数 1306 浏览 1 评论 0原文

这是一段尝试记录音频样本的代码:但我有一个未构造的AudioFormat对象(已传递给DataLine。信息因为我不知道采样率。

编辑

我发现仅随机放置 8000 的采样率就可以了。但这样好吗?我可以保留任何采样率值吗?

boolean lineIsStopped = false;
TargetDataLine line = null; 
AudioFormat af; // object not constructed through out
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af); // af not initialized
try {
  line = (TargetDataLine)AudioSystem.getLine(info); 
  line.open( af );      
} catch( LineUnavailableException ex ) {
   // handle the error
  }

// now we are ready for an input
// call start to start accepting data from mic
byte data[] = new byte[ line.getBufferSize() / 5 ];
 line.start(); // this statement starts delivering data into the line buffer
// start retreiving data from the line buffer
 int numBytesRead;
 int offset = 0;
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 while( ! lineIsStopped ) { // when the line is not stopped i.e is active
     numBytesRead = line.read( data , offset , data.length );
     // now save the data
     try {
        out.write(data); // writes data to this output stream !
     } catch( Exception exc) {
         System.out.println(exc);
       }
 }

在这我如何构建音频格式对象而不获取任何音频样本?

This is a code that will attempt to record an audio sample : but i've a not constructed AudioFormat object ( that has been passed to DataLine.Info) because i don't know the sample rate.

EDIT

I have seen that just randomly placing sample rate of 8000 works . But is it fine ? Can i keep any value of sample rate ?

boolean lineIsStopped = false;
TargetDataLine line = null; 
AudioFormat af; // object not constructed through out
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af); // af not initialized
try {
  line = (TargetDataLine)AudioSystem.getLine(info); 
  line.open( af );      
} catch( LineUnavailableException ex ) {
   // handle the error
  }

// now we are ready for an input
// call start to start accepting data from mic
byte data[] = new byte[ line.getBufferSize() / 5 ];
 line.start(); // this statement starts delivering data into the line buffer
// start retreiving data from the line buffer
 int numBytesRead;
 int offset = 0;
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 while( ! lineIsStopped ) { // when the line is not stopped i.e is active
     numBytesRead = line.read( data , offset , data.length );
     // now save the data
     try {
        out.write(data); // writes data to this output stream !
     } catch( Exception exc) {
         System.out.println(exc);
       }
 }

In this how can i construct audio format object without getting any audio sample ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞琼窗梦回ˉ 2024-12-05 02:33:08

阅读您的评论后,您正在用麦克风录音。在这种情况下,您需要根据您想要的麦克风质量来设置音频格式。如果你想要通话质量8k hz就可以了。如果您想要磁带质量 22khz,如果您想要 CD 质量音频 44.1khz。当然,如果您通过网络传输,那么 8khz 可能就足够了。

如果您的应用程序将其作为一个设置,这样用户就可以控制他们想要的质量,这总是一个好主意。

After reading your comments, you are recording from the mic. In which case you want to set the audio format according to the quality you want from the mic. If you want telephone quality 8k hz would be fine. If you want tape quality 22khz, and if you want CD quality audio 44.1khz. Of course, if you transmitting that over the network then 8khz is probably going to be good enough.

It's always a good idea to have this be a setting if your application so the user can control what quality they want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文