Skip to content

Commit

Permalink
faac
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Mar 18, 2024
1 parent 9b5ee4a commit 92f7258
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/src/main/cpp/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ bool Engine::setEffectOn(bool isOn) {

meter->enable();
meter->start();
meter->faacInit(mSampleRate, bufferSizeInFrames);
meter->faacConfig();
// addPluginToRack(0, 0);
mIsEffectOn = isOn;
}
Expand All @@ -103,6 +105,7 @@ bool Engine::setEffectOn(bool isOn) {
mFullDuplexPass.stop();
closeStreams();
mIsEffectOn = isOn;
meter->faacClose();
}
}

Expand Down
26 changes: 21 additions & 5 deletions app/src/main/cpp/Meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#define TUNER_ARRAY_SIZE 4096

unsigned char * Meter::audioToVideoBytes = NULL ;
faacEncHandle Meter::faacEncHandle = nullptr;
jfloatArray Meter::jfloatArray1 ;
int Meter::jfloatArray1_index = 0 ;
Expand Down Expand Up @@ -45,7 +46,7 @@ bool Meter::enabled = false ;
float Meter::lastTotal = 0 ;
bool Meter::isInput = true;

jfloatArray pushVideoSamples = nullptr;
jbyteArray pushVideoSamples = nullptr;

JNIEnv* getEnv() {
JNIEnv *env;
Expand Down Expand Up @@ -134,7 +135,7 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {
setTuner = envOutput->GetStaticMethodID(mainActivityOutput, "setTuner",
"([FI)V");
pushToVideo = envOutput->GetStaticMethodID(mainActivityOutput, "pushToVideo",
"([FI)V");
"([BI)V");
if (setMixerMeterOutput == nullptr) {
LOGF("cannot find method!");
}
Expand All @@ -145,7 +146,8 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {

// this should never be more than this
jfloatArray1 = envOutput->NewFloatArray(TUNER_ARRAY_SIZE);
pushVideoSamples = envOutput->NewFloatArray(TUNER_ARRAY_SIZE);
pushVideoSamples = envOutput->NewByteArray(TUNER_ARRAY_SIZE);
audioToVideoBytes = (unsigned char *) malloc(sizeof(char) * TUNER_ARRAY_SIZE);
jfloatArray1_index = 0 ;
return 0 ;
} else {
Expand All @@ -160,7 +162,9 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {
}

if (videoRecording) {
envOutput->SetFloatArrayRegion(pushVideoSamples, 0, samples, data);
faacEncode(data, samples, audioToVideoBytes, TUNER_ARRAY_SIZE);
envOutput->SetByteArrayRegion(pushVideoSamples, 0, samples,
reinterpret_cast<const jbyte *>(audioToVideoBytes));
envOutput->CallStaticVoidMethod(mainActivityOutput, pushToVideo, pushVideoSamples, samples);
}
}
Expand Down Expand Up @@ -437,7 +441,8 @@ void Meter::process (int nframes, const float * data, bool isInput) {
*/
}

void Meter::faacInit (int sampleRate, unsigned long maxSamples, unsigned long maxBytes) {
void Meter::faacInit (int sampleRate, unsigned long maxSamples) {
unsigned long maxBytes = TUNER_ARRAY_SIZE ;
jack_samplerate = sampleRate ;
faacEncHandle = faacEncOpen(
sampleRate,
Expand All @@ -459,4 +464,15 @@ void Meter::faacConfig () {
faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(faacEncHandle);
config -> bitRate = 160000 ;
config->inputFormat = 4 ; // aaaaahhhhhhhhhhh
faacEncSetConfiguration(faacEncHandle, config);
}

int Meter::faacEncode (float * data, int nframes, unsigned char *outputBuffer,
unsigned int bufferSize) {
int bytesWritten = faacEncEncode(faacEncHandle, reinterpret_cast<int32_t *>(data), nframes, outputBuffer, bufferSize) ;
if (bytesWritten < 0) {
LOGE("[faac] error: %d", bytesWritten);
}

return bytesWritten;
}
6 changes: 5 additions & 1 deletion app/src/main/cpp/Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,15 @@ class Meter {
static faacEncHandle faacEncHandle;

public:
void faacInit(int sampleRate, unsigned long maxSamples, unsigned long maxBytes);
void faacInit(int sampleRate, unsigned long maxSamples);

void faacClose();

void faacConfig();

static int faacEncode(float * data, int nframes, unsigned char *outputBuffer, unsigned int bufferSize);

static unsigned char *audioToVideoBytes;
};

#endif //AMP_RACK_METER_H
23 changes: 20 additions & 3 deletions app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ private void prepareEncoder() {

// MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", sampleRate, 1);
MediaFormat outputFormat = new MediaFormat();
outputFormat.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
outputFormat.setString(MediaFormat.KEY_MIME, "audio/aac");
outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, 160000);
outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16384);
outputFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
outputFormat.setInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_FLOAT);
// outputFormat.setInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_FLOAT);
outputFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, AudioEngine.getSampleRate());

// Create a MediaCodec encoder, and configure it with our format. Get a Surface
Expand Down Expand Up @@ -418,6 +418,16 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu
// if (audioTrackIndex == -1)
// return;
//
MediaFormat outputFormat = new MediaFormat();
outputFormat.setString(MediaFormat.KEY_MIME, "audio/aac");
outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, 160000);
outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16384);
outputFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
// outputFormat.setInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_FLOAT);
outputFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, AudioEngine.getSampleRate());

audioTrackIndex = mMuxer.addTrack(outputFormat);
mMuxer.setOrientationHint(cameraCharacteristicsHashMap.get(cameraId).get(CameraCharacteristics.SENSOR_ORIENTATION));

// Log.d(TAG, "onOutputBufferAvailable: starting muxer");
Expand All @@ -431,7 +441,14 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu
info.presentationTimeUs = timestamp.get();
mMuxer.writeSampleData(mTrackIndex, outPutByteBuffer, info);
codec.releaseOutputBuffer(index, false);
mBufferInfo = info;

if (mainActivity.avBuffer.size() == 0)
return;

MainActivity.AVBuffer avBuffer = mainActivity.avBuffer.pop();
bufferInfo.set(0, avBuffer.size, info.presentationTimeUs, 0);
mMuxer.writeSampleData(audioTrackIndex, ByteBuffer.wrap(avBuffer.bytes), bufferInfo);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public class MainActivity extends AppCompatActivity implements ActivityCompat.On
boolean videoRecording = false ;
Camera2 camera2 ;
static class AVBuffer {
float [] floats ;
byte [] bytes ;
int size ;
}
public static LinkedList<AVBuffer> avBuffer = new LinkedList<>();
Expand Down Expand Up @@ -3432,18 +3432,16 @@ int getCameraSensorOrientation(CameraCharacteristics characteristics) {
return (360 - (cameraOrientation != null ? cameraOrientation : 0)) % 360;
}

public static void pushToVideo (float [] data, int nframes) {
public static void pushToVideo (byte [] data, int nframes) {
// Log.d(TAG, String.format ("%d: %f - %f", nframes, data [0], data [nframes - 1]));
if (! mainActivity.videoRecording)
return;

/*
AVBuffer buffer = new AVBuffer();
buffer.size = nframes;
buffer.floats = data.clone();
buffer.bytes = data.clone();
avBuffer.addLast(buffer);

*/
}

private static long computePresentationTimeNsec(long frameIndex, int sampleRate) {
Expand Down

0 comments on commit 92f7258

Please sign in to comment.