Skip to content

Commit

Permalink
audio is recorded but silent
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Mar 16, 2024
1 parent 7f91fdf commit 8ddf6c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
24 changes: 16 additions & 8 deletions app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

public class Camera2 {
final String TAG = getClass().getSimpleName();
int sampleRate = 48000;
// parameters for the encoder
private static final String MIME_TYPE = "video/avc"; // H.264 Advanced Video Coding
private static final int FRAME_RATE = 30; // 15fps
Expand All @@ -60,7 +61,7 @@ public class Camera2 {
public boolean mMuxerStarted;

// allocate one of these up front so we don't need to do it every time
private MediaCodec.BufferInfo mBufferInfo;
public MediaCodec.BufferInfo mBufferInfo;

private TextureView textureView;
MainActivity mainActivity;
Expand Down Expand Up @@ -90,6 +91,9 @@ public class Camera2 {
Camera2(MainActivity mainActivity_) {
mainActivity = mainActivity_;
textureView = mainActivity_.rack.videoTexture;
sampleRate = AudioEngine.getSampleRate() ;
if (sampleRate == 0)
sampleRate = 48000 ;
}

public void openCamera() {
Expand Down Expand Up @@ -261,7 +265,7 @@ private void prepareEncoder() {
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
Log.d(TAG, "format: " + format);

MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm",AudioEngine.getSampleRate(), 1);
MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", sampleRate, 1);
outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, 160000);
outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16384);
Expand Down Expand Up @@ -322,6 +326,7 @@ private void prepareEncoder() {
*/
private void releaseEncoder() {
Log.d(TAG, "releaseEncoder: stopping encoder");
mMuxerStarted = false;
mEncoder.signalEndOfInputStream();

if (mEncoder != null) {
Expand All @@ -344,7 +349,6 @@ private void releaseEncoder() {
mMuxer.stop();
mMuxer.release();
mMuxer = null;
mMuxerStarted = false;
}

mTrackIndex = -1 ;
Expand All @@ -355,9 +359,12 @@ class EncoderCallback extends MediaCodec.Callback {
ByteBuffer outPutByteBuffer, inputByteBuffer, audioBuffer;
MainActivity.AVBuffer floatBuffer;
boolean isVideo ;
MediaCodec.BufferInfo bufferInfo ;

EncoderCallback (boolean video) {
isVideo = video;
bufferInfo = new MediaCodec.BufferInfo();

}

@Override
Expand All @@ -378,7 +385,7 @@ public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
inputByteBuffer = codec.getInputBuffer(index);
inputByteBuffer.asFloatBuffer().put(floatBuffer.floatBuffer);
presentationTimeUs = 1000000l * index / 48000;
mainActivity.camera2.audioEncoder.queueInputBuffer(index, 0, floatBuffer.size, presentationTimeUs, eos);;
mainActivity.camera2.audioEncoder.queueInputBuffer(index, 0, floatBuffer.size, mBufferInfo.presentationTimeUs, eos);;

if (eos != 0) {
audioEncoder.signalEndOfInputStream();
Expand All @@ -388,14 +395,16 @@ public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {

@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
mBufferInfo = info;
if (! mMuxerStarted) {
MediaFormat newFormat = mEncoder.getOutputFormat();
Log.d(TAG, "encoder output format changed: " + newFormat);

// now that we have the Magic Goodies, start the muxer
if (mTrackIndex == -1)
mTrackIndex = mMuxer.addTrack(newFormat);
int aIndex = audioEncoder.dequeueOutputBuffer(mBufferInfo, 5000) ;

int aIndex = audioEncoder.dequeueOutputBuffer(bufferInfo, 5000) ;
if (aIndex >= 0) {
Log.d(TAG, "onInputBufferAvailable: added audio track");
MediaFormat format = audioEncoder.getOutputFormat();
Expand All @@ -419,11 +428,10 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu
// outPutByteBuffer.get(outDate);



int aIndex = audioEncoder.dequeueOutputBuffer(mBufferInfo, 5000) ;
int aIndex = audioEncoder.dequeueOutputBuffer(bufferInfo, 5000) ;
if (aIndex > 0) {
audioBuffer = audioEncoder.getOutputBuffer(aIndex);
mMuxer.writeSampleData(audioTrackIndex, audioBuffer, mBufferInfo);
mMuxer.writeSampleData(audioTrackIndex, audioBuffer, bufferInfo);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2721,7 +2721,8 @@ static void setTuner (float [] data, int size) {
if (inputBufferId >= 0) {
ByteBuffer inputBuffer = mainActivity.camera2.audioEncoder.getInputBuffer (inputBufferId);
inputBuffer.asFloatBuffer().put(data);
presentationTimeUs = 1000000l * avEncoderIndex / 48000;
presentationTimeUs = presentationTimeUs + size / mainActivity.camera2.sampleRate;

mainActivity.camera2.audioEncoder.queueInputBuffer(inputBufferId, 0, size, presentationTimeUs, 0);;
mainActivity.camera2.audioIndex = inputBufferId ;
}
Expand All @@ -2735,6 +2736,8 @@ static void setTuner (float [] data, int size) {
mainActivity.camera2.audioEncoder.flush();
*/
} else {
presentationTimeUs = 0 ;
}

if (! mainActivity.tunerEnabled)
Expand Down

0 comments on commit 8ddf6c9

Please sign in to comment.