获取音频流音量

huangapple go评论94阅读模式
英文:

AudioRecord get stream volume

问题

我想读取数据然后将其发送到另一个流。首先,我想检查它是否有效。问题是,getstreamVolume 在这里不起作用,我不知道应该使用哪种方法。我有这段代码:

  1. audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC
  2. , SAMPLE_RATE
  3. , AudioFormat.CHANNEL_IN_STEREO
  4. , AudioFormat.ENCODING_PCM_16BIT
  5. , intBufferSize);
  6. audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC
  7. , SAMPLE_RATE
  8. , AudioFormat.CHANNEL_IN_STEREO
  9. , AudioFormat.ENCODING_PCM_16BIT
  10. , intBufferSize
  11. , AudioTrack.MODE_STREAM);
  12. audioTrack.setPlaybackRate(SAMPLE_RATE);
  13. audioRecord.startRecording();
  14. audioTrack.play();
  15. while (true){
  16. audioRecord.read(AudioData, 0, AudioData.length);
  17. for (int i = 0; i < AudioData.length; i++){
  18. AudioData[i] = (short) Math.min (AudioData[i], Short.MAX_VALUE);
  19. }
  20. audioTrack.setVolume(0.3f);
  21. audioTrack.write(AudioData, 0, AudioData.length);
  22. }

我如何检查输入数据的音量(分贝)?还是应该使用 Audio Manager?

英文:

I want to read data and then send it to another stream. First of all, I want to check if it works. The problem is, getstreamVolume will not work here and I don't know which method I should use. I have this code:

  1. audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC
  2. , SAMPLE_RATE
  3. , AudioFormat.CHANNEL_IN_STEREO
  4. , AudioFormat.ENCODING_PCM_16BIT
  5. , intBufferSize);
  6. audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC
  7. , SAMPLE_RATE
  8. , AudioFormat.CHANNEL_IN_STEREO
  9. , AudioFormat.ENCODING_PCM_16BIT
  10. , intBufferSize
  11. , AudioTrack.MODE_STREAM);
  12. audioTrack.setPlaybackRate(SAMPLE_RATE);
  13. audioRecord.startRecording();
  14. audioTrack.play();
  15. while (true){
  16. audioRecord.read(AudioData, 0, AudioData.length);
  17. for (int i = 0; i < AudioData.length; i++){
  18. AudioData[i] = (short) Math.min (AudioData[i], Short.MAX_VALUE);
  19. }
  20. audioTrack.setVolume(0.3f);
  21. audioTrack.write(AudioData, 0, AudioData.length);
  22. }

How can I check the volume of input data in db? Or should I use Audio Manager?

答案1

得分: 0

以下是翻译好的部分:

音频流的最大音量只能在多个样本之间进行计算;它是在流中遇到的最大绝对值:

  1. double[] samps = [0, -.5, 0, 0.5, 0, -.75, 0]
  2. double max = 0;
  3. for ( samp : samps ) {
  4. if ( abs(samp) > max) {
  5. max = abs(samp);
  6. }
  7. }

上述音频样本的最大音量将为0.75(或为-1.25dB);这意味着您可以有效地放大信号1.33倍而不会丢失任何信息;输出流将恢复到0dB并变为:

  1. [0, -0.67, 0, 0.67, 0, -1, 0]
英文:

The maximum volume of an audio stream can only be calculated across several samples; it is the maximum absolute value encountered in the stream:

  1. double[] samps = [0, -.5, 0, 0.5, 0, -.75, 0]
  2. double max = 0;
  3. for ( samp : samps ) {
  4. if ( abs(samp) > max) {
  5. max = abs(samp);
  6. }
  7. }

The maximum volume for the above audio sample would be .75, (or at -1.25dB); this means you can effectively amplify the signal by 1.33 and not lose any information; the output stream would rise back up to 0dB and become:

  1. [0, -.67, 0, .67, 0, -1, 0]

huangapple
  • 本文由 发表于 2020年8月13日 05:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63384949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定