Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions audio-echo/app/src/main/cpp/audio_effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class AudioDelay : public AudioFormat {
size_t getDelayTime(void) const;
void setDecayWeight(float weight);
float getDecayWeight(void) const;
void process(int16_t *liveAudio, int32_t numFrames);
void process(int16_t* liveAudio, int32_t numFrames);

private:
size_t delayTime_ = 0;
float decayWeight_ = 0.5;
void *buffer_ = nullptr;
void* buffer_ = nullptr;
size_t bufCapacity_ = 0;
size_t bufSize_ = 0;
size_t curPos_ = 0;
Expand Down
44 changes: 22 additions & 22 deletions audio-echo/app/src/main/cpp/audio_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ struct EchoAudioEngine {
SLObjectItf slEngineObj_;
SLEngineItf slEngineItf_;

AudioRecorder *recorder_;
AudioPlayer *player_;
AudioQueue *freeBufQueue_; // Owner of the queue
AudioQueue *recBufQueue_; // Owner of the queue
AudioRecorder* recorder_;
AudioPlayer* player_;
AudioQueue* freeBufQueue_; // Owner of the queue
AudioQueue* recBufQueue_; // Owner of the queue

sample_buf *bufs_;
sample_buf* bufs_;
uint32_t bufCount_;
uint32_t frameCount_;
int64_t echoDelay_;
float echoDecay_;
AudioDelay *delayEffect_;
AudioDelay* delayEffect_;
};
static EchoAudioEngine engine;

bool EngineService(void *ctx, uint32_t msg, void *data);
bool EngineService(void* ctx, uint32_t msg, void* data);

JNIEXPORT void JNICALL Java_com_google_sample_echo_MainActivity_createSLEngine(
JNIEnv *env, jclass type, jint sampleRate, jint framesPerBuf,
JNIEnv* env, jclass type, jint sampleRate, jint framesPerBuf,
jlong delayInMs, jfloat decay) {
SLresult result;
memset(&engine, 0, sizeof(engine));
Expand Down Expand Up @@ -103,7 +103,7 @@ JNIEXPORT void JNICALL Java_com_google_sample_echo_MainActivity_createSLEngine(
}

JNIEXPORT jboolean JNICALL
Java_com_google_sample_echo_MainActivity_configureEcho(JNIEnv *env, jclass type,
Java_com_google_sample_echo_MainActivity_configureEcho(JNIEnv* env, jclass type,
jint delayInMs,
jfloat decay) {
engine.echoDelay_ = delayInMs;
Expand All @@ -116,7 +116,7 @@ Java_com_google_sample_echo_MainActivity_configureEcho(JNIEnv *env, jclass type,

JNIEXPORT jboolean JNICALL
Java_com_google_sample_echo_MainActivity_createSLBufferQueueAudioPlayer(
JNIEnv *env, jclass type) {
JNIEnv* env, jclass type) {
SampleFormat sampleFormat;
memset(&sampleFormat, 0, sizeof(sampleFormat));
sampleFormat.pcmFormat_ = (uint16_t)engine.bitsPerSample_;
Expand All @@ -131,22 +131,22 @@ Java_com_google_sample_echo_MainActivity_createSLBufferQueueAudioPlayer(
if (engine.player_ == nullptr) return JNI_FALSE;

engine.player_->SetBufQueue(engine.recBufQueue_, engine.freeBufQueue_);
engine.player_->RegisterCallback(EngineService, (void *)&engine);
engine.player_->RegisterCallback(EngineService, (void*)&engine);

return JNI_TRUE;
}

JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_deleteSLBufferQueueAudioPlayer(
JNIEnv *env, jclass type) {
JNIEnv* env, jclass type) {
if (engine.player_) {
delete engine.player_;
engine.player_ = nullptr;
}
}

JNIEXPORT jboolean JNICALL
Java_com_google_sample_echo_MainActivity_createAudioRecorder(JNIEnv *env,
Java_com_google_sample_echo_MainActivity_createAudioRecorder(JNIEnv* env,
jclass type) {
SampleFormat sampleFormat;
memset(&sampleFormat, 0, sizeof(sampleFormat));
Expand All @@ -161,20 +161,20 @@ Java_com_google_sample_echo_MainActivity_createAudioRecorder(JNIEnv *env,
return JNI_FALSE;
}
engine.recorder_->SetBufQueues(engine.freeBufQueue_, engine.recBufQueue_);
engine.recorder_->RegisterCallback(EngineService, (void *)&engine);
engine.recorder_->RegisterCallback(EngineService, (void*)&engine);
return JNI_TRUE;
}

JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_deleteAudioRecorder(JNIEnv *env,
Java_com_google_sample_echo_MainActivity_deleteAudioRecorder(JNIEnv* env,
jclass type) {
if (engine.recorder_) delete engine.recorder_;

engine.recorder_ = nullptr;
}

JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_startPlay(JNIEnv *env, jclass type) {
Java_com_google_sample_echo_MainActivity_startPlay(JNIEnv* env, jclass type) {
engine.frameCount_ = 0;
/*
* start player: make it into waitForData state
Expand All @@ -187,7 +187,7 @@ Java_com_google_sample_echo_MainActivity_startPlay(JNIEnv *env, jclass type) {
}

JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_stopPlay(JNIEnv *env, jclass type) {
Java_com_google_sample_echo_MainActivity_stopPlay(JNIEnv* env, jclass type) {
engine.recorder_->Stop();
engine.player_->Stop();

Expand All @@ -198,7 +198,7 @@ Java_com_google_sample_echo_MainActivity_stopPlay(JNIEnv *env, jclass type) {
}

JNIEXPORT void JNICALL Java_com_google_sample_echo_MainActivity_deleteSLEngine(
JNIEnv *env, jclass type) {
JNIEnv* env, jclass type) {
delete engine.recBufQueue_;
delete engine.freeBufQueue_;
releaseSampleBufs(engine.bufs_, engine.bufCount_);
Expand Down Expand Up @@ -236,19 +236,19 @@ uint32_t dbgEngineGetBufCount(void) {
/*
* simple message passing for player/recorder to communicate with engine
*/
bool EngineService(void *ctx, uint32_t msg, void *data) {
bool EngineService(void* ctx, uint32_t msg, void* data) {
assert(ctx == &engine);
switch (msg) {
case ENGINE_SERVICE_MSG_RETRIEVE_DUMP_BUFS: {
*(static_cast<uint32_t *>(data)) = dbgEngineGetBufCount();
*(static_cast<uint32_t*>(data)) = dbgEngineGetBufCount();
break;
}
case ENGINE_SERVICE_MSG_RECORDED_AUDIO_AVAILABLE: {
// adding audio delay effect
sample_buf *buf = static_cast<sample_buf *>(data);
sample_buf* buf = static_cast<sample_buf*>(data);
assert(engine.fastPathFramesPerBuf_ ==
buf->size_ / engine.sampleChannels_ / (engine.bitsPerSample_ / 8));
engine.delayEffect_->process(reinterpret_cast<int16_t *>(buf->buf_),
engine.delayEffect_->process(reinterpret_cast<int16_t*>(buf->buf_),
engine.fastPathFramesPerBuf_);
break;
}
Expand Down
14 changes: 7 additions & 7 deletions audio-echo/app/src/main/cpp/audio_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
* very regular, you could buffer much less audio samples between
* recorder and player, hence lower latency.
*/
void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *ctx) {
(static_cast<AudioPlayer *>(ctx))->ProcessSLCallback(bq);
void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void* ctx) {
(static_cast<AudioPlayer*>(ctx))->ProcessSLCallback(bq);
}
void AudioPlayer::ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq) {
#ifdef ENABLE_LOG
Expand All @@ -39,7 +39,7 @@ void AudioPlayer::ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq) {

// retrieve the finished device buf and put onto the free queue
// so recorder could re-use it
sample_buf *buf;
sample_buf* buf;
if (!devShadowQueue_->front(&buf)) {
/*
* This should not happen: we got a callback,
Expand Down Expand Up @@ -87,7 +87,7 @@ void AudioPlayer::ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq) {
}
}

AudioPlayer::AudioPlayer(SampleFormat *sampleFormat, SLEngineItf slEngine)
AudioPlayer::AudioPlayer(SampleFormat* sampleFormat, SLEngineItf slEngine)
: freeQueue_(nullptr),
playQueue_(nullptr),
devShadowQueue_(nullptr),
Expand Down Expand Up @@ -176,7 +176,7 @@ AudioPlayer::~AudioPlayer() {
(*playerObjectItf_)->Destroy(playerObjectItf_);
}
// Consume all non-completed audio buffers
sample_buf *buf = NULL;
sample_buf* buf = NULL;
while (devShadowQueue_->front(&buf)) {
buf->size_ = 0;
devShadowQueue_->pop();
Expand All @@ -200,7 +200,7 @@ AudioPlayer::~AudioPlayer() {
delete[] silentBuf_.buf_;
}

void AudioPlayer::SetBufQueue(AudioQueue *playQ, AudioQueue *freeQ) {
void AudioPlayer::SetBufQueue(AudioQueue* playQ, AudioQueue* freeQ) {
playQueue_ = playQ;
freeQueue_ = freeQ;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ void AudioPlayer::Stop(void) {
#endif
}

void AudioPlayer::RegisterCallback(ENGINE_CALLBACK cb, void *ctx) {
void AudioPlayer::RegisterCallback(ENGINE_CALLBACK cb, void* ctx) {
callback_ = cb;
ctx_ = ctx;
}
Expand Down
16 changes: 8 additions & 8 deletions audio-echo/app/src/main/cpp/audio_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ class AudioPlayer {
SLAndroidSimpleBufferQueueItf playBufferQueueItf_;

SampleFormat sampleInfo_;
AudioQueue *freeQueue_; // user
AudioQueue *playQueue_; // user
AudioQueue *devShadowQueue_; // owner
AudioQueue* freeQueue_; // user
AudioQueue* playQueue_; // user
AudioQueue* devShadowQueue_; // owner

ENGINE_CALLBACK callback_;
void *ctx_;
void* ctx_;
sample_buf silentBuf_;
#ifdef ENABLE_LOG
AndroidLog *logFile_;
AndroidLog* logFile_;
#endif
std::mutex stopMutex_;

public:
explicit AudioPlayer(SampleFormat *sampleFormat, SLEngineItf engine);
explicit AudioPlayer(SampleFormat* sampleFormat, SLEngineItf engine);
~AudioPlayer();
void SetBufQueue(AudioQueue *playQ, AudioQueue *freeQ);
void SetBufQueue(AudioQueue* playQ, AudioQueue* freeQ);
SLresult Start(void);
void Stop(void);
void ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq);
uint32_t dbgGetDevBufCount(void);
void RegisterCallback(ENGINE_CALLBACK cb, void *ctx);
void RegisterCallback(ENGINE_CALLBACK cb, void* ctx);
};

#endif // NATIVE_AUDIO_AUDIO_PLAYER_H
18 changes: 9 additions & 9 deletions audio-echo/app/src/main/cpp/audio_recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
* bqRecorderCallback(): called for every buffer is full;
* pass directly to handler
*/
void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *rec) {
(static_cast<AudioRecorder *>(rec))->ProcessSLCallback(bq);
void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void* rec) {
(static_cast<AudioRecorder*>(rec))->ProcessSLCallback(bq);
}

void AudioRecorder::ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq) {
#ifdef ENABLE_LOG
recLog_->logTime();
#endif
assert(bq == recBufQueueItf_);
sample_buf *dataBuf = NULL;
sample_buf* dataBuf = NULL;
devShadowQueue_->front(&dataBuf);
devShadowQueue_->pop();
dataBuf->size_ = dataBuf->cap_; // device only calls us when it is really
Expand All @@ -40,7 +40,7 @@ void AudioRecorder::ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq) {
callback_(ctx_, ENGINE_SERVICE_MSG_RECORDED_AUDIO_AVAILABLE, dataBuf);
recQueue_->push(dataBuf);

sample_buf *freeBuf;
sample_buf* freeBuf;
while (freeQueue_->front(&freeBuf) && devShadowQueue_->push(freeBuf)) {
freeQueue_->pop();
SLresult result = (*bq)->Enqueue(bq, freeBuf->buf_, freeBuf->cap_);
Expand All @@ -55,7 +55,7 @@ void AudioRecorder::ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq) {
}
}

AudioRecorder::AudioRecorder(SampleFormat *sampleFormat, SLEngineItf slEngine)
AudioRecorder::AudioRecorder(SampleFormat* sampleFormat, SLEngineItf slEngine)
: freeQueue_(nullptr),
recQueue_(nullptr),
devShadowQueue_(nullptr),
Expand Down Expand Up @@ -138,7 +138,7 @@ SLboolean AudioRecorder::Start(void) {
SLASSERT(result);

for (int i = 0; i < RECORD_DEVICE_KICKSTART_BUF_COUNT; i++) {
sample_buf *buf = NULL;
sample_buf* buf = NULL;
if (!freeQueue_->front(&buf)) {
LOGE("=====OutOfFreeBuffers @ startingRecording @ (%d)", i);
break;
Expand Down Expand Up @@ -185,7 +185,7 @@ AudioRecorder::~AudioRecorder() {
}

if (devShadowQueue_) {
sample_buf *buf = NULL;
sample_buf* buf = NULL;
while (devShadowQueue_->front(&buf)) {
devShadowQueue_->pop();
freeQueue_->push(buf);
Expand All @@ -199,13 +199,13 @@ AudioRecorder::~AudioRecorder() {
#endif
}

void AudioRecorder::SetBufQueues(AudioQueue *freeQ, AudioQueue *recQ) {
void AudioRecorder::SetBufQueues(AudioQueue* freeQ, AudioQueue* recQ) {
assert(freeQ && recQ);
freeQueue_ = freeQ;
recQueue_ = recQ;
}

void AudioRecorder::RegisterCallback(ENGINE_CALLBACK cb, void *ctx) {
void AudioRecorder::RegisterCallback(ENGINE_CALLBACK cb, void* ctx) {
callback_ = cb;
ctx_ = ctx;
}
Expand Down
16 changes: 8 additions & 8 deletions audio-echo/app/src/main/cpp/audio_recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ class AudioRecorder {
SLAndroidSimpleBufferQueueItf recBufQueueItf_;

SampleFormat sampleInfo_;
AudioQueue *freeQueue_; // user
AudioQueue *recQueue_; // user
AudioQueue *devShadowQueue_; // owner
AudioQueue* freeQueue_; // user
AudioQueue* recQueue_; // user
AudioQueue* devShadowQueue_; // owner
uint32_t audioBufCount;

ENGINE_CALLBACK callback_;
void *ctx_;
void* ctx_;

public:
explicit AudioRecorder(SampleFormat *, SLEngineItf engineEngine);
explicit AudioRecorder(SampleFormat*, SLEngineItf engineEngine);
~AudioRecorder();
SLboolean Start(void);
SLboolean Stop(void);
void SetBufQueues(AudioQueue *freeQ, AudioQueue *recQ);
void SetBufQueues(AudioQueue* freeQ, AudioQueue* recQ);
void ProcessSLCallback(SLAndroidSimpleBufferQueueItf bq);
void RegisterCallback(ENGINE_CALLBACK cb, void *ctx);
void RegisterCallback(ENGINE_CALLBACK cb, void* ctx);
int32_t dbgGetDevBufCount(void);

#ifdef ENABLE_LOG
AndroidLog *recLog_;
AndroidLog* recLog_;
#endif
};

Expand Down
18 changes: 9 additions & 9 deletions audio-echo/app/src/main/cpp/jni_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ extern "C" {
#endif

JNIEXPORT void JNICALL Java_com_google_sample_echo_MainActivity_createSLEngine(
JNIEnv *env, jclass, jint, jint, jlong delayInMs, jfloat decay);
JNIEnv* env, jclass, jint, jint, jlong delayInMs, jfloat decay);
JNIEXPORT void JNICALL Java_com_google_sample_echo_MainActivity_deleteSLEngine(
JNIEnv *env, jclass type);
JNIEnv* env, jclass type);
JNIEXPORT jboolean JNICALL
Java_com_google_sample_echo_MainActivity_createSLBufferQueueAudioPlayer(
JNIEnv *env, jclass);
JNIEnv* env, jclass);
JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_deleteSLBufferQueueAudioPlayer(
JNIEnv *env, jclass type);
JNIEnv* env, jclass type);

JNIEXPORT jboolean JNICALL
Java_com_google_sample_echo_MainActivity_createAudioRecorder(JNIEnv *env,
Java_com_google_sample_echo_MainActivity_createAudioRecorder(JNIEnv* env,
jclass type);
JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_deleteAudioRecorder(JNIEnv *env,
Java_com_google_sample_echo_MainActivity_deleteAudioRecorder(JNIEnv* env,
jclass type);
JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_startPlay(JNIEnv *env, jclass type);
Java_com_google_sample_echo_MainActivity_startPlay(JNIEnv* env, jclass type);
JNIEXPORT void JNICALL
Java_com_google_sample_echo_MainActivity_stopPlay(JNIEnv *env, jclass type);
Java_com_google_sample_echo_MainActivity_stopPlay(JNIEnv* env, jclass type);
JNIEXPORT jboolean JNICALL
Java_com_google_sample_echo_MainActivity_configureEcho(JNIEnv *env, jclass type,
Java_com_google_sample_echo_MainActivity_configureEcho(JNIEnv* env, jclass type,
jint delayInMs,
jfloat decay);
#ifdef __cplusplus
Expand Down
Loading