我的目标是创建一个应用程序,从麦克风捕获音频然后将其传递给带通滤波器并将其乘以一些增益然后输出结果实时到扬声器
所以我在Superpowered SDK中编辑了FrequencyDomain.cpp示例以符合我的目的,我尝试了这段代码,但我听不到输出音频,
我的代码中缺少什么?
static SuperpoweredFrequencyDomain *frequencyDomain;
static float *inputBufferFloat;
// This is called periodically by the media server.
static bool audioProcessing(void * __unused clientdata, short int
*audioInputOutput, int numberOfSamples, int __unused samplerate) {
// Creating the filter.
SuperpoweredFilter *filter = new
SuperpoweredFilter(SuperpoweredFilter_Resonant_Lowpass, samplerate);
filter->setResonantParameters(1000.0f, 0.1f);
filter->enable(true);
filter->process(stereoBuffer, stereoBuffer, numberOfSamples);
SuperpoweredShortIntToFloat(audioInputOutput, inputBufferFloat, (unsigned int)numberOfSamples); // Converting the 16-bit integer samples to 32-bit floating point.
}
extern "C" JNIEXPORT void
Java_com_superpowered_frequencydomain_MainActivity_FrequencyDomain(JNIEnv *
__unused javaEnvironment, jobject __unused obj, jint samplerate, jint buffersize) {
inputBufferFloat = (float *)malloc(buffersize * sizeof(float) * 2 + 128);
SuperpoweredCPU::setSustainedPerformanceMode(true);
new SuperpoweredAndroidAudioIO(samplerate, buffersize, true, true, audioProcessing, NULL, -1, SL_ANDROID_STREAM_MEDIA, buffersize * 2); // Start audio input/output.
}
答案 0 :(得分:1)
不要在音频处理回调中创建过滤器。正如您在上面的评论中所看到的,媒体服务器(操作系统的音频堆栈)会定期调用它。
只有filter-> process()应该在音频处理回调中。
您没有听到音频输出,因为音频处理回调中的其余代码是无意义的。
麦克风输入到达" audioInputOutput"阵列。您需要首先使用SuperpoweredShortIntToFloat将其转换为浮点数据。然后,您需要使用过滤器处理该数据。最后,使用SuperpoweredFloatToShortInt将数据转换回short int。那只是3行代码。
答案 1 :(得分:0)
我设法通过编辑像这样的频率域示例来解决它
static bool audioProcessing(void *clientdata, short int *audioIO, int numberOfSamples, int __unused samplerate) {
SuperpoweredShortIntToFloat(audioIO, inputBufferFloat, (unsigned int)numberOfSamples);
return ((FrequencyDomain *)clientdata)->process(audioIO, (unsigned int)numberOfSamples);
}
FrequencyDomain::FrequencyDomain(unsigned int samplerate, unsigned int buffersize) {
audioSystem = new SuperpoweredAndroidAudioIO(samplerate, buffersize, true, true, audioProcessing, this, -1, SL_ANDROID_STREAM_MEDIA, buffersize * 2);
}
bool FrequencyDomain::process(short int *output, unsigned int numberOfSamples) {
filterHighpass->enable(true);
filterHighpass->process(inputBufferFloat, outputBufferFloatHigh, numberOfSamples);
filter = new SuperpoweredFilter(SuperpoweredFilter_Resonant_Lowpass, numberOfSamples);
filter->setResonantParameters(200.0f, 0.1f);
filter->enable(true);
filter->process(inputBufferFloat, inputBufferFloat, numberOfSamples);
SuperpoweredFloatToShortInt(inputBufferFloat, output, numberOfSamples);
return true;
}
static FrequencyDomain *example = NULL;
extern "C" JNIEXPORT void Java_com_superpowered_frequencydomain_MainActivity_FrequencyDomain(JNIEnv * __unused javaEnvironment, jobject __unused obj, jint samplerate, jint buffersize) {
inputBufferFloat = (float *)malloc(buffersize * sizeof(float) * 2 + 128);
SuperpoweredCPU::setSustainedPerformanceMode(true);
example=new FrequencyDomain((unsigned int)samplerate, (unsigned int)buffersize);
}