【问题标题】:How do I record audio on Android and change the pitch?如何在 Android 上录制音频并更改音高?
【发布时间】:2011-08-23 15:52:56
【问题描述】:

我正在尝试学习 Android 开发,我想知道如何从麦克风捕获音频,然后更改音频中的声音,使其听起来更厚或更清晰等。 简而言之:如何录制和更改声音的参数? (当然是在 java 中)

【问题讨论】:

    标签: java android audio attributes


    【解决方案1】:

    我实际上正在处理android app that involves audio。录制音频是很容易的部分,你可以复制我的代码。编写音频滤波器是一项艰巨的任务,需要了解数字信号处理和Fast Fourier Transform (FFT)

    您可以从阅读 audio processing in java here. 开始

    同时,这是在 android 上录制音频的代码:

    public String record() {
    
                    // please note: the emulator only supports 8 khz sampling.
                    // so in test mode, you need to change this to
                    //int frequency = 8000;
    
                    int frequency = 11025;
    
                    int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
                    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
                    File file = new File(Environment.getExternalStorageDirectory()
                                    .getAbsolutePath() + "/reverseme.pcm");
    
                    // Delete any previous recording.
                    if (file.exists())
                            file.delete();
    
                    // Create the new file.
                    try {
                            file.createNewFile();
                    } catch (IOException e) {
                            throw new IllegalStateException("Failed to create "
                                            + file.toString());
                    }
    
                    try {
                            // Create a DataOuputStream to write the audio data into the saved
                            // file.
                            OutputStream os = new FileOutputStream(file);
                            BufferedOutputStream bos = new BufferedOutputStream(os);
                            DataOutputStream dos = new DataOutputStream(bos);
    
                            // Create a new AudioRecord object to record the audio.
                            int bufferSize = 2 * AudioRecord.getMinBufferSize(frequency,
                                            channelConfiguration, audioEncoding);
                            AudioRecord audioRecord = new AudioRecord(
                                            MediaRecorder.AudioSource.MIC, frequency,
                                            channelConfiguration, audioEncoding, bufferSize);
    
                            short[] buffer = new short[bufferSize];
                            audioRecord.startRecording();
    
                            Log.e(tag, "Recording started");
    
                            long start = SystemClock.elapsedRealtime();
                            long end = start + 15000;
                            while (SystemClock.elapsedRealtime() < end) {
                                    int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
                                    for (int i = 0; i < bufferReadResult; i++)
    
                                            if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
                                                    dos.writeShort( EndianUtils.swapShort(buffer[i]));                                                
                                            } else {
                                                    dos.writeShort( buffer[i] );                                                                
                                            }
                            }
    
                            Log.e(tag, "Recording stopped");
    
                            audioRecord.stop();
                            bos.flush();
                            dos.close();
                            isRecording = false;
                            return file.getAbsolutePath();
    
                    } catch (Exception e) {
                            Log.e(tag, "Recording Failed:" + e.getMessage());
                            throw new RuntimeException("Failed to create " + e.getMessage());
    
                    }
            }
    

    【讨论】:

      【解决方案2】:

      您可以使用Android SoundPool 更改THIS 问题的音频文件检查答案的音调。

      当有人在 MIC 附近讲话时录制音频,请查看LINK

      【讨论】:

        猜你喜欢
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-30
        • 2011-12-17
        • 2016-12-02
        • 2015-04-03
        相关资源
        最近更新 更多