gpt4 book ai didi

java - 如何在 Android 上录制音频并改变音调?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:39:39 26 4
gpt4 key购买 nike

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

最佳答案

我实际上正在研究 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());

}
}

关于java - 如何在 Android 上录制音频并改变音调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7163870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com