Skip to content

Commit

Permalink
experiments with audio record
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Mar 20, 2024
1 parent 3280833 commit 4f0bf0d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
9 changes: 6 additions & 3 deletions app/src/main/cpp/Meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,13 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {
}

// mp4 muxer test
/*
int bytesWritten = faacEncode(data, samples, audioToVideoBytes, TUNER_ARRAY_SIZE);
if (bytesWritten >= 0) {
mp4 -> write (audioToVideoBytes, bytesWritten);
}
*/

// end mp4 muxer test
/*
if (videoRecording) {
Expand Down Expand Up @@ -228,14 +231,14 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {

void Meter::start () {
engine_running = true ;
mp4 = new MP4 (lastRecordedFileName);
// mp4 = new MP4 (lastRecordedFileName);
}

void Meter::stop () {
IN
engine_running = false ;
mp4 ->aacToMP4();
delete mp4 ;
// mp4 ->aacToMP4();
// delete mp4 ;

/* we never detach
envOutput = nullptr ;
Expand Down
66 changes: 47 additions & 19 deletions app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.CamcorderProfile;
import android.media.ImageReader;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
Expand Down Expand Up @@ -105,6 +107,16 @@ long get () {
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;

// Start Audio Record
static final int AUDIO_SOURCE = MediaRecorder.AudioSource.MIC; // for raw audio, use MediaRecorder.AudioSource.UNPROCESSED, see note in MediaRecorder section
static final int SAMPLE_RATE = 48000;
static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
static final int BUFFER_SIZE_RECORDING = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
AudioRecord audioRecord;

// End Audio Record

Camera2(MainActivity mainActivity_) {
mainActivity = mainActivity_;
textureView = mainActivity_.rack.videoTexture;
Expand Down Expand Up @@ -292,8 +304,8 @@ private void prepareEncoder() {
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());
outputFormat.setInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_32BIT);
outputFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, 48000);

// Create a MediaCodec encoder, and configure it with our format. Get a Surface
// we can use for input and wrap it with a class that handles the EGL work.
Expand All @@ -309,6 +321,16 @@ private void prepareEncoder() {
throw new RuntimeException(e);
}

// audio record start
audioRecord = new AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE_RECORDING);

if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) { // check for proper initialization
Log.e(TAG, "error initializing " );
return;
}

// audio record end

mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
audioEncoder.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
Log.d(TAG, "[audio] prepareEncoder: configured format: " + outputFormat.toString());
Expand All @@ -322,12 +344,28 @@ private void prepareEncoder() {
audioEncoder.setCallback(new MediaCodec.Callback() {
@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {

byte[] data = new byte[BUFFER_SIZE_RECORDING/2]; // assign size so that bytes are read in in chunks inferior to AudioRecord internal buffer size
int read = audioRecord.read(data, 0, data.length);
ByteBuffer buffer = codec.getInputBuffer(index);
buffer.rewind();
if (read > 0)
buffer.put(data, 0, read);
else
Log.e(TAG, "[audioRecord]: read returned " + read);
long time = timestamp.get() ;
// Log.d(TAG, String.format ("[audioRecord]: %s | %d", read, time));
// buffer.rewind();
codec.queueInputBuffer(index, 0, read, time, 0);
}

@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {

ByteBuffer buffer = codec.getOutputBuffer(index);
buffer.rewind();
if (mMuxerStarted)
mMuxer.writeSampleData(audioTrackIndex, buffer, info);
// Log.d(TAG, String.format ("[audioOutput]: %d | %d", info.size, info.presentationTimeUs));
codec.releaseOutputBuffer(index, false);
}

@Override
Expand All @@ -342,8 +380,10 @@ public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaForma
audioTrackIndex, codec.getOutputFormat()));
}
});

audioEncoder.start();
mEncoder.start();
audioRecord.startRecording();

// Output filename. Ideally this would use Context.getFilesDir() rather than a
// hard-coded output directory.
Expand Down Expand Up @@ -436,24 +476,12 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu
// 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);
if (mTrackIndex == -1)
mTrackIndex = mMuxer.addTrack(newFormat);

if (audioTrackIndex == -1)
return;

MediaFormat outputFormat = new MediaFormat();
outputFormat.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
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, 2);
// outputFormat.setInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_FLOAT);
outputFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate);

outputFormat = audioEncoder.getOutputFormat();
Log.d(TAG, "[audio] onOutputBufferAvailable: format " + outputFormat.toString());
// audioTrackIndex = mMuxer.addTrack(outputFormat);
mMuxer.setOrientationHint(cameraCharacteristicsHashMap.get(cameraId).get(CameraCharacteristics.SENSOR_ORIENTATION));

// Log.d(TAG, "onOutputBufferAvailable: starting muxer");
Expand All @@ -465,7 +493,7 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu

outPutByteBuffer = codec.getOutputBuffer(index);
info.presentationTimeUs = timestamp.get();
// mMuxer.writeSampleData(mTrackIndex, outPutByteBuffer, info);
mMuxer.writeSampleData(mTrackIndex, outPutByteBuffer, info);
codec.releaseOutputBuffer(index, false);

// int bytesWritten = 0 ;
Expand Down

0 comments on commit 4f0bf0d

Please sign in to comment.