我该如何在Android上录制音频并改变音调?

5

我正在学习 Android 开发,想知道如何从麦克风中捕捉音频并更改音频中的声音,使其听起来更加浑厚或尖锐等。简而言之:我该如何录制并更改声音的参数?(当然是用 Java)

2个回答

5
我正在开发一个涉及音频的安卓应用程序。录制音频是容易的部分,你可以大多数情况下复制我的代码。编写音频过滤器是一个更难的任务,需要了解数字信号处理和快速傅里叶变换(FFT)的知识。
你可以先阅读这里Java中的音频处理
与此同时,这是在安卓上录制音频的代码:
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());

                }
        }

0

您可以使用Android SoundPool来改变音频文件的音调,请查看问题的答案。

当有人靠近麦克风说话时,可以使用此链接来录制音频。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接