如何在安卓系统中将音频从立体声转换为单声道?[英] How to convert audio from stereo to mono in Android?

本文是小编为大家收集整理的关于如何在安卓系统中将音频从立体声转换为单声道?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我使用android.provider.mediaStore.Audio.Media.external_Content_Uri意图从SD卡加载音乐文件.

Intent tmpIntent1 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(tmpIntent1, 0);

和onActivityResult

Uri mediaPath = Uri.parse(data.getData().toString());
MediaPlayer mp = MediaPlayer.create(this, mediaPath);
mp.start();

现在MediaPlayer在立体声中播放音频.是否有任何方法可以将所选音乐/音频文件或从立体声转换为应用程序本身的单声道?

我向期为 soundpool audiotrack ,但没有找到如何将mp3文件音频转换为单声道.

PowerLAMP这样的应用程序有那些立体声<- >单声道交换机,当按下时,当按下立即将输出音频转换为单声道信号并再次返回,它们如何进行?

推荐答案

您是否分别加载.wav-文件分别pcm-data?如果是这样,那么您可以轻松读取每个通道的每个样本,叠加它们并将它们除以通道的量以获得单声道信号.

如果将立体声信号以交错的签名短路的形式存储,则要计算结果的单声道信号的代码可能如下所示:

    short[] stereoSamples;//get them from somewhere

    //output array, which will contain the mono signal
    short[] monoSamples= new short[stereoSamples.length/2];
    //length of the .wav-file header-> 44 bytes
    final int HEADER_LENGTH=22;

    //additional counter
    int k=0;


    for(int i=0; i< monoSamples.length;i++){
        //skip the header andsuperpose the samples of the left and right channel
        if(k>HEADER_LENGTH){
        monoSamples[i]= (short) ((stereoSamples[i*2]+ stereoSamples[(i*2)+1])/2);
        }
        k++;
    }

我希望,我能够帮助你.

最好的问候, g_j

本文地址:https://www.itbaoku.cn/post/102570.html

问题描述

I use the android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI intent to load music files from the SD Card.

Intent tmpIntent1 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(tmpIntent1, 0);

and in onActivityResult

Uri mediaPath = Uri.parse(data.getData().toString());
MediaPlayer mp = MediaPlayer.create(this, mediaPath);
mp.start();

Now MediaPlayer plays the audio in stereo. Is there any way to convert the selected music/audio file or the output from stereo to mono in the app itself?

I looked up API for SoundPool and AudioTrack, but didn't find how to convert mp3 files audio to mono.

Apps like PowerAMP have those Stereo <-> Mono switches that when pressed immediately convert the output audio to mono signal and back again, how do they do it?

推荐答案

Do you load .wav- files respectively PCM- data? If so then you could easily read each sample of each channel, superpose them and divide them by the amount of channels to get a mono signal.

If you store your stereo signal in form of interleaved signed shorts, the code to calculate the resulting mono signal might look like this:

    short[] stereoSamples;//get them from somewhere

    //output array, which will contain the mono signal
    short[] monoSamples= new short[stereoSamples.length/2];
    //length of the .wav-file header-> 44 bytes
    final int HEADER_LENGTH=22;

    //additional counter
    int k=0;


    for(int i=0; i< monoSamples.length;i++){
        //skip the header andsuperpose the samples of the left and right channel
        if(k>HEADER_LENGTH){
        monoSamples[i]= (short) ((stereoSamples[i*2]+ stereoSamples[(i*2)+1])/2);
        }
        k++;
    }

I hope, I was able to help you.

Best regards, G_J