Я пытаюсь создать базовое приложение для микшера, которое будет включать до 4 звуков одновременно.
Вот класс, который я использую для достижения этой цели:
#define log_print __android_log_print
static SuperpoweredAndroidAudioIO *audioIO;
static SuperpoweredAdvancedAudioPlayer *playerA, *playerB, *playerC, *playerD;
static float *floatBuffer;
// This is called periodically by the audio engine.
static bool audioProcessing (
void * __unused clientdata, // custom pointer
short int *audio, // buffer of interleaved samples
int numberOfFrames, // number of frames to process
int __unused samplerate // sampling rate
) {
if(playerA->process(floatBuffer, false, (unsigned int)numberOfFrames)
|| playerB->process(floatBuffer, false, (unsigned int)numberOfFrames)
|| playerC->process(floatBuffer, false, (unsigned int)numberOfFrames)
|| playerD->process(floatBuffer, false, (unsigned int)numberOfFrames)) {
SuperpoweredFloatToShortInt(floatBuffer, audio, (unsigned int)numberOfFrames);
return true;
} else {
return false;
}
}
// Called by the playerA.
static void playerEventCallbackA (
void * __unused clientData,
SuperpoweredAdvancedAudioPlayerEvent event,
void *value
) {
switch (event) {
case SuperpoweredAdvancedAudioPlayerEvent_LoadSuccess:
break;
case SuperpoweredAdvancedAudioPlayerEvent_LoadError:
log_print(ANDROID_LOG_ERROR, "Player", "Open error: %s", (char *)value);
break;
case SuperpoweredAdvancedAudioPlayerEvent_EOF:
playerA->seek(0); // loop track
break;
default:;
};
}
static void playerEventCallbackB (
void * __unused clientData,
SuperpoweredAdvancedAudioPlayerEvent event,
void *value
) {
switch (event) {
case SuperpoweredAdvancedAudioPlayerEvent_LoadSuccess:
break;
case SuperpoweredAdvancedAudioPlayerEvent_LoadError:
log_print(ANDROID_LOG_ERROR, "Player", "Open error: %s", (char *)value);
break;
case SuperpoweredAdvancedAudioPlayerEvent_EOF:
playerB->seek(0); // loop track
break;
default:;
};
}
static void playerEventCallbackC (
void * __unused clientData,
SuperpoweredAdvancedAudioPlayerEvent event,
void *value
) {
switch (event) {
case SuperpoweredAdvancedAudioPlayerEvent_LoadSuccess:
break;
case SuperpoweredAdvancedAudioPlayerEvent_LoadError:
log_print(ANDROID_LOG_ERROR, "Player", "Open error: %s", (char *)value);
break;
case SuperpoweredAdvancedAudioPlayerEvent_EOF:
playerC->seek(0); // loop track
break;
default:;
};
}
static void playerEventCallbackD (
void * __unused clientData,
SuperpoweredAdvancedAudioPlayerEvent event,
void *value
) {
switch (event) {
case SuperpoweredAdvancedAudioPlayerEvent_LoadSuccess:
break;
case SuperpoweredAdvancedAudioPlayerEvent_LoadError:
log_print(ANDROID_LOG_ERROR, "Player", "Open error: %s", (char *)value);
break;
case SuperpoweredAdvancedAudioPlayerEvent_EOF:
playerD->seek(0); // loop track
break;
default:;
};
}
// StartAudio - Start audio engine and initialize playerA.
extern "C" JNIEXPORT void
Java_com_connorschwing_CustomAudioManager_StartAudio (
JNIEnv * __unused env,
jobject __unused obj,
jint samplerate,
jint buffersize
) {
SuperpoweredInitialize(
"ExampleLicenseKey-WillExpire-OnNextUpdate",
false, // enableAudioAnalysis (using SuperpoweredAnalyzer, SuperpoweredLiveAnalyzer, SuperpoweredWaveform or SuperpoweredBandpassFilterbank)
false, // enableFFTAndFrequencyDomain (using SuperpoweredFrequencyDomain, SuperpoweredFFTComplex, SuperpoweredFFTReal or SuperpoweredPolarFFT)
false, // enableAudioTimeStretching (using SuperpoweredTimeStretching)
false, // enableAudioEffects (using any SuperpoweredFX class)
true, // enableAudioPlayerAndDecoder (using SuperpoweredAdvancedAudioPlayer or SuperpoweredDecoder)
false, // enableCryptographics (using Superpowered::RSAPublicKey, Superpowered::RSAPrivateKey, Superpowered::hasher or Superpowered::AES)
false // enableNetworking (using Superpowered::httpRequest)
);
// Allocate audio buffer.
floatBuffer = (float *)malloc(sizeof(float) * 2 * buffersize);
// Initialize playerA and pass callback function.
playerA = new SuperpoweredAdvancedAudioPlayer (
NULL, // clientData
playerEventCallbackA, // callback function
(unsigned int)samplerate, // sampling rate
0 // cachedPointCount
);
// Initialize playerA and pass callback function.
playerB = new SuperpoweredAdvancedAudioPlayer (
NULL, // clientData
playerEventCallbackA, // callback function
(unsigned int)samplerate, // sampling rate
0 // cachedPointCount
);
// Initialize playerA and pass callback function.
playerC = new SuperpoweredAdvancedAudioPlayer (
NULL, // clientData
playerEventCallbackA, // callback function
(unsigned int)samplerate, // sampling rate
0 // cachedPointCount
);
// Initialize playerA and pass callback function.
playerD = new SuperpoweredAdvancedAudioPlayer (
NULL, // clientData
playerEventCallbackA, // callback function
(unsigned int)samplerate, // sampling rate
0 // cachedPointCount
);
// Initialize audio with audio callback function.
audioIO = new SuperpoweredAndroidAudioIO (
samplerate, // sampling rate
buffersize, // buffer size
false, // enableInput
true, // enableOutput
audioProcessing, // process callback function
NULL, // clientData
-1, // inputStreamType (-1 = default)
SL_ANDROID_STREAM_MEDIA // outputStreamType (-1 = default)
);
}
// OpenFile - Open file in playerA, specifying offset and length.
extern "C" JNIEXPORT void
Java_com_connorschwing_CustomAudioManager_OpenFile (
JNIEnv *env,
jobject __unused obj,
jstring path, // path to APK file
jint channelID // which player to use
) {
log_print(ANDROID_LOG_INFO, "Player", "~~CHECKPOINT: %d", channelID);
const char *str = env->GetStringUTFChars(path, 0);
switch (channelID) {
case 0:
playerA->open(str);
log_print(ANDROID_LOG_INFO, "Player", "~~Opening: %s", str);
break;
case 1:
playerB->open(str);
log_print(ANDROID_LOG_INFO, "Player", "~~Opening: %s", str);
break;
case 2:
playerC->open(str);
break;
case 3:
playerD->open(str);
break;
default: ;
}
env->ReleaseStringUTFChars(path, str);
}
// TogglePlayback - Toggle Play/Pause state of the playerA.
extern "C" JNIEXPORT void
Java_com_connorschwing_CustomAudioManager_TogglePlayback (
JNIEnv * __unused env,
jobject __unused obj,
jint channelID,
jboolean playing
) {
switch (channelID) {
case -1:
if(playing){
log_print(ANDROID_LOG_INFO, "Player", "~~PLAYING ALL: %d", channelID);
playerA->play(true);
playerB->play(true);
playerC->play(true);
playerD->play(true);
SuperpoweredCPU::setSustainedPerformanceMode(playerA->playing); // prevent dropouts
SuperpoweredCPU::setSustainedPerformanceMode(playerB->playing); // prevent dropouts
SuperpoweredCPU::setSustainedPerformanceMode(playerC->playing); // prevent dropouts
SuperpoweredCPU::setSustainedPerformanceMode(playerD->playing); // prevent dropouts
} else {
playerA->pause();
playerA->seek(0);
playerB->pause();
playerB->seek(0);
playerC->pause();
playerC->seek(0);
playerD->pause();
playerD->seek(0);
}
break;
case 0:
if(playing){
playerA->play(false);
SuperpoweredCPU::setSustainedPerformanceMode(playerA->playing); // prevent dropouts
} else {
playerA->pause();
playerA->seek(0);
}
break;
case 1:
if(playing){
playerB->play(false);
SuperpoweredCPU::setSustainedPerformanceMode(playerB->playing); // prevent dropouts
} else playerB->pause();
break;
case 2:
if(playing){
playerC->play(false);
SuperpoweredCPU::setSustainedPerformanceMode(playerC->playing); // prevent dropouts
} else playerC->pause();
break;
case 3:
if(playing){
playerD->play(false);
SuperpoweredCPU::setSustainedPerformanceMode(playerD->playing); // prevent dropouts
} else playerD->pause();
break;
default:;
}
}
// onBackground - Put audio processing to sleep.
extern "C" JNIEXPORT void
Java_com_connorschwing_CustomAudioManager_onBackground(
JNIEnv *__unused env,
jobject __unused obj
) {
audioIO->onBackground();
}
// onForeground - Resume audio processing.
extern "C" JNIEXPORT void
Java_com_connorschwing_CustomAudioManager_onForeground(
JNIEnv *__unused env,
jobject __unused obj
) {
audioIO->onForeground();
}
// Cleanup - Free resources.
extern "C" JNIEXPORT void
Java_com_connorschwing_CustomAudioManager_Cleanup(
JNIEnv *__unused env,
jobject __unused obj
) {
delete audioIO;
delete playerA;
delete playerB;
delete playerC;
delete playerD;
free(floatBuffer);
}
Очень хорошо работает для одного трека.Я могу записать звук, открыть его в плеере, чтобы он воспроизводился и воспроизводился.Однако, если я записываю на другую дорожку и начинаю воспроизводить проигрыватель B, то из проигрывателя B не выходит звук. Я могу слышать только одну дорожку за раз.Я понимаю, что мне нужно как-то объединить этих игроков в один буфер, но я не уверен, как это сделать.Прямо сейчас он просто вызывает player-> play (false) на каждой спине к спине.
Как мне изменить этот код, чтобы разрешить одновременную работу нескольких игроков?
Решение попытки 1 Вместо использования true / false для значения bufferAdd в каждом вызове, я просто использую сам вызов для другого игрока.Эти вызовы player-> process () вернут логическое значение, которое будет установлено в качестве значения bufferAdd для следующего вызова
if((playerD->process(floatBuffer, playerC->process(floatBuffer, playerB->process(floatBuffer, playerA->process(floatBuffer, false, (unsigned int)numberOfFrames), (unsigned int)numberOfFrames), (unsigned int)numberOfFrames) , (unsigned int)numberOfFrames)))
{
SuperpoweredFloatToShortInt(floatBuffer, audio, (unsigned int)numberOfFrames);
return true;
}
попытка решения 2 Проверка состояния каждого игрока,если он обрабатывает, обработайте другой 3
if(playerA->process(floatBuffer, false, (unsigned int)numberOfFrames))
{
playerB->process(floatBuffer, true, (unsigned int)numberOfFrames);
playerC->process(floatBuffer, true, (unsigned int)numberOfFrames);
playerD->process(floatBuffer, true, (unsigned int)numberOfFrames);
SuperpoweredFloatToShortInt(floatBuffer, audio, (unsigned int)numberOfFrames);
return true;
} else if(playerB->process(floatBuffer, false, (unsigned int)numberOfFrames)) {
playerA->process(floatBuffer, true, (unsigned int) numberOfFrames);
playerC->process(floatBuffer, true, (unsigned int) numberOfFrames);
playerD->process(floatBuffer, true, (unsigned int) numberOfFrames);
SuperpoweredFloatToShortInt(floatBuffer, audio, (unsigned int) numberOfFrames);
return true;
} else if(playerC->process(floatBuffer, false, (unsigned int)numberOfFrames)) {
playerB->process(floatBuffer, true, (unsigned int) numberOfFrames);
playerA->process(floatBuffer, true, (unsigned int) numberOfFrames);
playerD->process(floatBuffer, true, (unsigned int) numberOfFrames);
SuperpoweredFloatToShortInt(floatBuffer, audio, (unsigned int) numberOfFrames);
return true;
} else if(playerD->process(floatBuffer, false, (unsigned int)numberOfFrames)) {
playerB->process(floatBuffer, true, (unsigned int) numberOfFrames);
playerC->process(floatBuffer, true, (unsigned int) numberOfFrames);
playerA->process(floatBuffer, true, (unsigned int) numberOfFrames);
SuperpoweredFloatToShortInt(floatBuffer, audio, (unsigned int) numberOfFrames);
return true;
} else return false;