Skip to content

Commit

Permalink
Update float point on android speaker engine (#128)
Browse files Browse the repository at this point in the history
* update minium android version to 21 for float-point source
* Update float-point source on android speaker engine
* Refactoring with  TxEngine & WaveRenderer  & EuPIRenderer
  • Loading branch information
designe committed May 11, 2022
1 parent b10dd08 commit ff4cc7d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion euphony/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdkVersion(30)

defaultConfig {
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 30

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
2 changes: 1 addition & 1 deletion euphony/src/main/cpp/core/source/EuPIOscillator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void Euphony::EuPIOscillator::setWaveOn(bool isWaveOn) {
void Euphony::EuPIOscillator::renderAudio(float *data, int32_t numFrames) {
/* mapping frequencies data */
for(int i = 0; i < numFrames; ++i) {
data[i] = (float) (sin(mPhase) * mAmplitude);
data[i] = sin(mPhase) * mAmplitude;
mPhase += mPhaseIncrement;
if (mPhase > kTwoPi) mPhase -= kTwoPi;
}
Expand Down
2 changes: 1 addition & 1 deletion euphony/src/main/cpp/core/source/TxEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class TxEngine::TxEngineImpl : public IRestartable{
stopEuPIMode();
break;
}

mStream->requestPause();
mStream->requestFlush();
mStatus = Status::STOP;
}
}
Expand Down
18 changes: 13 additions & 5 deletions euphony/src/main/cpp/core/source/WaveRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@ void WaveRenderer::renderAudio(float *targetData, int32_t numFrames) {
const float *waveSrcData = waveSource.get();

for (int i = 0; i < numFrames; ++i) {
for (int j = 0; j < channelCount; ++j) {
targetData[(i * channelCount) + j] = waveSrcData[readFrameIndex];
const int targetDataIndex = i * channelCount;
for (int chNo = 0; chNo < channelCount; ++chNo) {
targetData[targetDataIndex + chNo] = waveSrcData[readFrameIndex];
}
if (++readFrameIndex == waveSourceSize){
readFrameIndex = 0;
if(renderTotalCount > 0) {
if(++renderIndex >= renderTotalCount)
isWaveOn.store(false);
if(renderTotalCount > 0 && ++renderIndex == renderTotalCount) {
for (int j = i + 1; j < numFrames; ++j) {
for (int chNo = 0; chNo < channelCount; ++chNo) {
targetData[j + chNo] = 0;
}
}
isWaveOn.store(false);
break;
}
}
}
} else {
renderIndex = 0;
readFrameIndex = 0;
renderSilence(targetData, numFrames * channelCount);
}
}
Expand Down
20 changes: 8 additions & 12 deletions euphony/src/main/java/co/euphony/tx/EuTxManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import co.euphony.util.EuOption;

import static android.media.AudioTrack.SUCCESS;
import static android.media.AudioTrack.WRITE_BLOCKING;

public class EuTxManager {
private EuTxNativeConnector txCore;
Expand Down Expand Up @@ -89,27 +88,24 @@ private void playWithNativeEngine(final int count) {
txCore.start();
}

short[] outShortStream;
private void playWithAndroidEngine(int count) {
float[] outStream = txCore.getGenWaveSource();
final float[] outStream = txCore.getGenWaveSource();
final int minBufferSizeBytes = AudioTrack.getMinBufferSize(Constants.SAMPLERATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_FLOAT);
final int bufferSize = outStream.length * minBufferSizeBytes;
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, Constants.SAMPLERATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_FLOAT, bufferSize, AudioTrack.MODE_STATIC);

mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, outStream.length*2, AudioTrack.MODE_STATIC);
/* A value of -1 means infinite looping, and 0 disables looping. */
if(count <= 0)
count = -1;

int result = mAudioTrack.setLoopPoints(0, outStream.length, count);
final int result = mAudioTrack.setLoopPoints(0, outStream.length, count);
if(result != SUCCESS) {
Log.i("PROCESS", "failed to loop points : " + result);
}

outShortStream = new short[outStream.length];
for(int i = 0; i < outStream.length; i++) {
outShortStream[i] = (short) (32767 * outStream[i]);
}

if(mAudioTrack != null){
try{
mAudioTrack.write(outShortStream, 0, outShortStream.length);
try {
mAudioTrack.write(outStream, 0, outStream.length, AudioTrack.WRITE_NON_BLOCKING);
mAudioTrack.play();
}
catch(IllegalStateException e)
Expand Down

0 comments on commit ff4cc7d

Please sign in to comment.