开发者

How do I record audio on Android and change the pitch?

I am trying to learn Android Development and I was wondering how I开发者_C百科 can capture audio from a microphone and then change the voice in the audio so that it sounds thicker or sharper etc. In short: How do I record and change the parameters of a sound? (In java, of course)


I'm actually working on an android app that involves audio. Recording the audio is the easy part, and you can mostly copy my code for that. Writing an audio filter is a much harder task, and requires knowledge of digital signal processing and the Fast Fourier Transform (FFT)

You could start by reading about audio processing in java here.

Meanwhile, here's the code to record audio on 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());

                }
        }


You can use Android SoundPool to chnage the pitch of audio file check answers of THIS question.

And to record audio when someone speaks near MIC check this LINK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜