获取音频流音量

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

AudioRecord get stream volume

问题

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

        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC
                , SAMPLE_RATE
                , AudioFormat.CHANNEL_IN_STEREO
                , AudioFormat.ENCODING_PCM_16BIT
                , intBufferSize);

        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC
                , SAMPLE_RATE
                , AudioFormat.CHANNEL_IN_STEREO
                , AudioFormat.ENCODING_PCM_16BIT
                , intBufferSize
                , AudioTrack.MODE_STREAM);

        audioTrack.setPlaybackRate(SAMPLE_RATE);

        audioRecord.startRecording();
        audioTrack.play();

        while (true){

            audioRecord.read(AudioData, 0, AudioData.length);

            for (int i = 0; i < AudioData.length; i++){
                AudioData[i] = (short) Math.min (AudioData[i], Short.MAX_VALUE);
            }

            audioTrack.setVolume(0.3f);
            audioTrack.write(AudioData, 0, AudioData.length);
        }

我如何检查输入数据的音量(分贝)?还是应该使用 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:

        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC
                , SAMPLE_RATE
                , AudioFormat.CHANNEL_IN_STEREO
                , AudioFormat.ENCODING_PCM_16BIT
                , intBufferSize);

        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC
                , SAMPLE_RATE
                , AudioFormat.CHANNEL_IN_STEREO
                , AudioFormat.ENCODING_PCM_16BIT
                , intBufferSize
                , AudioTrack.MODE_STREAM);

        audioTrack.setPlaybackRate(SAMPLE_RATE);

        audioRecord.startRecording();
        audioTrack.play();

        while (true){

            audioRecord.read(AudioData, 0, AudioData.length);

            for (int i = 0; i < AudioData.length; i++){
                AudioData[i] = (short) Math.min (AudioData[i], Short.MAX_VALUE);
            }

            audioTrack.setVolume(0.3f);
            audioTrack.write(AudioData, 0, AudioData.length);
        }

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

答案1

得分: 0

以下是翻译好的部分:

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

double[] samps = [0, -.5, 0, 0.5, 0, -.75, 0]

double max = 0;
for ( samp : samps ) {

	if ( abs(samp) > max) {
	    max = abs(samp);
	}
}

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

[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:

double[] samps = [0, -.5, 0, 0.5, 0, -.75, 0]

double max = 0;
for ( samp : samps ) {

	if ( abs(samp) > max) {
	    max = abs(samp);
	}
}

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:

[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:

确定