Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,196 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "Microphone_precompiled.h"
#include "MicrophoneSystemComponent.h"
#include "SimpleDownsample.h"
#include <AzCore/std/chrono/chrono.h>
#include <AzCore/std/parallel/atomic.h>
#include <AzCore/std/parallel/thread.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/EBus/EBus.h>
#include <AudioRingBuffer.h>
#include <IAudioInterfacesCommonData.h>
#if defined(USE_LIBSAMPLERATE)
#include <samplerate.h>
#endif // USE_LIBSAMPLERATE
#include <AzCore/Android/Utils.h>
#include <AzCore/Android/JNI/Object.h>
#include <Microphone/WAVUtil.h>
namespace Audio
{
class MicrophoneSystemEventsAndroid : public AZ::EBusTraits
{
public:
static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple;
virtual ~MicrophoneSystemEventsAndroid() = default;
virtual void HandleIncomingData(int8* data, int size) {}
};
using MicrophoneSystemEventsAndroidBus = AZ::EBus<MicrophoneSystemEventsAndroid>;
static void JNI_SendCurrentData(JNIEnv* jniEnv, jobject objectRef, jbyteArray data, jint size)
{
jbyte* bufferPtr = jniEnv->GetByteArrayElements(data, nullptr);
MicrophoneSystemEventsAndroidBus::Broadcast(&MicrophoneSystemEventsAndroid::HandleIncomingData, bufferPtr, size);
jniEnv->ReleaseByteArrayElements(data, bufferPtr, JNI_ABORT);
}
///////////////////////////////////////////////////////////////////////////////////////////////
class MicrophoneSystemComponentAndroid : public MicrophoneSystemComponent::Implementation
, public MicrophoneSystemEventsAndroidBus::Handler
{
public:
AZ_CLASS_ALLOCATOR(MicrophoneSystemComponentAndroid, AZ::SystemAllocator, 0);
bool InitializeDevice() override
{
AZ_TracePrintf("AndroidMicrophone", "Initializing Microphone device - Android!!\n");
MicrophoneSystemEventsAndroidBus::Handler::BusConnect();
m_JNIobject.RegisterStaticMethod("InitializeDevice", "()Z");
m_JNIobject.RegisterStaticMethod("ShutdownDevice", "()V");
m_JNIobject.RegisterStaticMethod("StartSession", "()Z");
m_JNIobject.RegisterStaticMethod("EndSession", "()V");
m_JNIobject.RegisterStaticMethod("IsCapturing", "()Z");
m_JNIobject.RegisterNativeMethods(
{
{"SendCurrentData", "([BI)V", (void*)JNI_SendCurrentData}
}
);
// These are the Android "guaranteed" parameters
// Note that this must match what is setup in MicrophoneSystemComponent.java
// as this is the config that's will reflect the incoming data
// and it will need to be compared to see if downsampling is required
m_config.m_sampleRate = 44100;
m_config.m_numChannels = 1;
m_config.m_bitsPerSample = 16;
m_config.m_sourceType = AudioInputSourceType::Microphone;
m_config.m_sampleType = AudioInputSampleType::Int;
m_config.SetBufferSizeFromFrameCount(512);
return m_JNIobject.InvokeStaticBooleanMethod("InitializeDevice");
}
void ShutdownDevice() override
{
m_JNIobject.InvokeStaticVoidMethod("ShutdownDevice");
MicrophoneSystemEventsAndroidBus::Handler::BusDisconnect();
}
bool StartSession() override
{
m_captureData.reset(aznew Audio::RingBuffer<AZ::s16>(4096)); // this is a good size to keep buffer filling and draining without gaps
return m_JNIobject.InvokeStaticBooleanMethod("StartSession");
}
void EndSession() override
{
m_JNIobject.InvokeStaticVoidMethod("EndSession");
if(m_captureData)
{
m_captureData.reset();
}
}
bool IsCapturing() override
{
return m_JNIobject.InvokeStaticBooleanMethod("IsCapturing");
}
SAudioInputConfig GetFormatConfig() const override
{
return m_config;
}
AZStd::size_t GetData(void** outputData, AZStd::size_t numFrames, const SAudioInputConfig& targetConfig, bool shouldDeinterleave)
{
bool changeSampleType = (targetConfig.m_sampleType != m_config.m_sampleType);
bool changeSampleRate = (targetConfig.m_sampleRate != m_config.m_sampleRate);
bool changeNumChannels = (targetConfig.m_numChannels != m_config.m_numChannels);
#if defined(USE_LIBSAMPLERATE)
return {};
#else
if (changeSampleType || changeNumChannels)
{
// Without the SRC library, any change is unsupported!
return {};
}
else if (changeSampleRate)
{
if(targetConfig.m_sampleRate > m_config.m_sampleRate)
{
AZ_Error("MacOSMicrophone", false, "Target sample rate is larger than source sample rate, this is not supported");
return {};
}
auto sourceBuffer = new AZ::s16[numFrames];
AZStd::size_t targetSize = GetDownsampleSize(numFrames, m_config.m_sampleRate, targetConfig.m_sampleRate);
auto targetBuffer = new AZ::s16[targetSize];
numFrames = m_captureData->ConsumeData(reinterpret_cast<void**>(&sourceBuffer), numFrames, m_config.m_numChannels, false);
if(numFrames > 0)
{
Downsample(sourceBuffer, numFrames, m_config.m_sampleRate, targetBuffer, targetSize, targetConfig.m_sampleRate);
numFrames = targetSize;
// swap target data to output
::memcpy(*outputData, targetBuffer, targetSize * 2); //*2 as two bytes per frame
}
delete [] sourceBuffer;
delete [] targetBuffer;
return numFrames;
}
else
{
// No change to the data from Input to Output
return m_captureData->ConsumeData(outputData, numFrames, m_config.m_numChannels, shouldDeinterleave);
}
#endif
}
void HandleIncomingData(int8* data, int size) override
{
m_captureData->AddData((int16*)data, size / 2, m_config.m_numChannels);
}
private:
AZ::Android::JNI::Object m_JNIobject {"com/amazon/lumberyard/Microphone/MicrophoneSystemComponent"};
Audio::SAudioInputConfig m_config;
AZStd::unique_ptr<Audio::RingBufferBase> m_captureData = nullptr;
};
///////////////////////////////////////////////////////////////////////////////////////////////
MicrophoneSystemComponent::Implementation* MicrophoneSystemComponent::Implementation::Create()
{
return aznew MicrophoneSystemComponentAndroid();
}
}
@@ -0,0 +1,202 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
package com.amazon.lumberyard.Microphone;
import android.media.AudioRecord;
import android.media.AudioFormat;
import android.media.MediaRecorder;
import android.util.Log;
import java.lang.Thread;
import java.lang.Runnable;
import java.lang.String;
import java.nio.ByteBuffer;
public class MicrophoneSystemComponent implements Runnable
{
private AudioRecord recorder = null;
private Thread recordingThread = null;
private int bufferSize = 0;
private int format = 0;
private int sampleRate = 0;
private int numChannels = 0;
private static final int guaranteedSampleRate = 44100;
private static final int desiredChannels = 1;
private static final int desiredBitRate = 16;
private static final int bufferSizeMultiplier = 10;
private static final int frameSize = 1024;
private static MicrophoneSystemComponent instance = null;
public static native void SendCurrentData(byte[] data, int size);
public static MicrophoneSystemComponent getInstance() {
if(instance == null)
{
instance = new MicrophoneSystemComponent();
}
return instance;
}
public static boolean InitializeDevice()
{
return getInstance().InitializeDeviceImpl(guaranteedSampleRate, desiredChannels, desiredBitRate, false);
}
public static void ShutdownDevice()
{
getInstance().ShutdownDeviceImpl();
}
public static boolean StartSession()
{
return getInstance().StartSessionImpl();
}
public static void EndSession()
{
getInstance().EndSessionImpl();
}
public static boolean IsCapturing()
{
return getInstance().IsCapturingImpl();
}
public static void ProcessData(ByteBuffer buffer, int sizeRead)
{
byte[] data = new byte[sizeRead];
buffer.get(data, 0, sizeRead);
SendCurrentData(data, sizeRead);
}
private boolean InitializeDeviceImpl(int sampleRateIn, int numChannelsIn, int bitsPerSampleIn, boolean isFloatIn)
{
sampleRate = sampleRateIn;
if(numChannelsIn == 1)
{
numChannels = AudioFormat.CHANNEL_IN_MONO;
}
else if(numChannelsIn == 2)
{
numChannels = AudioFormat.CHANNEL_IN_STEREO;
}
else
{
Log.d("LMBR", String.format("Microphone::InitializeDevice, channel count wasn't 1 or 2. Instead got %d", numChannelsIn));
}
if(isFloatIn == true)
{
format = AudioFormat.ENCODING_PCM_FLOAT;
}
else
{
if(bitsPerSampleIn == 8)
{
format = AudioFormat.ENCODING_PCM_8BIT;
}
else if(bitsPerSampleIn == 16)
{
format = AudioFormat.ENCODING_PCM_16BIT;
}
else
{
Log.d("LMBR", String.format("Microphone::InitializeDevice, bits per sample wasn't 8 or 16. Instead got %d", bitsPerSampleIn));
}
}
if(format != 0 && sampleRate != 0 && numChannels != 0)
{
// 44100 is the only guaranteed sample rate, so always use that and then downsample as needed
bufferSize = AudioRecord.getMinBufferSize(guaranteedSampleRate, numChannels, format) * bufferSizeMultiplier;
if(bufferSize != AudioRecord.ERROR_BAD_VALUE && bufferSize != AudioRecord.ERROR)
{
recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, guaranteedSampleRate, numChannels, format, bufferSize);
if(recorder != null && recorder.getState() == AudioRecord.STATE_INITIALIZED)
{
return true;
}
}
}
Log.d("LMBR", "Microphone::InitializeDevice, unable to create AudioRecord");
return false;
}
private void ShutdownDeviceImpl()
{
if(recorder != null && recorder.getState() == AudioRecord.STATE_INITIALIZED)
{
recorder.stop();
recorder.release();
}
recorder = null;
recordingThread = null;
}
private boolean StartSessionImpl()
{
if(recorder != null && recorder.getState() == AudioRecord.STATE_INITIALIZED)
{
if(IsCapturingImpl())
{
EndSessionImpl();
}
recorder.startRecording();
recordingThread = new Thread(this);
recordingThread.start();
return true;
}
return false;
}
private void EndSessionImpl()
{
if(recorder != null && recorder.getState() == AudioRecord.STATE_INITIALIZED)
{
recorder.stop();
}
}
private boolean IsCapturingImpl()
{
if(recorder != null)
{
return recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING;
}
else
{
return false;
}
}
@Override
public void run()
{
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
while(recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING)
{
ByteBuffer tempBuf = ByteBuffer.allocateDirect(frameSize);
int sizeRead = recorder.read(tempBuf, frameSize);
if(sizeRead >= 1)
{
ProcessData(tempBuf, sizeRead);
}
}
}
@Override
protected void finalize() throws Throwable
{
super.finalize();
ShutdownDeviceImpl();
}
}
@@ -0,0 +1,10 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,14 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
set(FILES
MicrophoneSystemComponent_Android.cpp
)