|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2015 - ROLI Ltd.
-
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
-
- Details of these licenses can be found at: www.gnu.org/licenses
-
- JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- ------------------------------------------------------------------------------
-
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
-
- ==============================================================================
- */
-
- #undef check
-
- const char* const openSLTypeName = "Android OpenSL";
-
- bool isOpenSLAvailable()
- {
- DynamicLibrary library;
- return library.open ("libOpenSLES.so");
- }
-
- //==============================================================================
- class OpenSLAudioIODevice : public AudioIODevice,
- private Thread
- {
- public:
- OpenSLAudioIODevice (const String& deviceName)
- : AudioIODevice (deviceName, openSLTypeName),
- Thread ("OpenSL"),
- callback (nullptr), sampleRate (0), deviceOpen (false),
- inputBuffer (2, 2), outputBuffer (2, 2)
- {
- // OpenSL has piss-poor support for determining latency, so the only way I can find to
- // get a number for this is by asking the AudioTrack/AudioRecord classes..
- AndroidAudioIODevice javaDevice (deviceName);
-
- // this is a total guess about how to calculate the latency, but seems to vaguely agree
- // with the devices I've tested.. YMMV
- inputLatency = (javaDevice.minBufferSizeIn * 2) / 3;
- outputLatency = (javaDevice.minBufferSizeOut * 2) / 3;
-
- const int64 longestLatency = jmax (inputLatency, outputLatency);
- const int64 totalLatency = inputLatency + outputLatency;
- inputLatency = (int) ((longestLatency * inputLatency) / totalLatency) & ~15;
- outputLatency = (int) ((longestLatency * outputLatency) / totalLatency) & ~15;
- }
-
- ~OpenSLAudioIODevice()
- {
- close();
- }
-
- bool openedOk() const { return engine.outputMixObject != nullptr; }
-
- StringArray getOutputChannelNames() override
- {
- StringArray s;
- s.add ("Left");
- s.add ("Right");
- return s;
- }
-
- StringArray getInputChannelNames() override
- {
- StringArray s;
- s.add ("Audio Input");
- return s;
- }
-
- Array<double> getAvailableSampleRates() override
- {
- static const double rates[] = { 8000.0, 16000.0, 32000.0, 44100.0, 48000.0 };
- Array<double> retval (rates, numElementsInArray (rates));
-
- // make sure the native sample rate is pafrt of the list
- double native = getNativeSampleRate();
- if (native != 0.0 && ! retval.contains (native))
- retval.add (native);
-
- return retval;
- }
-
- Array<int> getAvailableBufferSizes() override
- {
- // we need to offer the lowest possible buffer size which
- // is the native buffer size
- const int defaultNumMultiples = 8;
- const int nativeBufferSize = getNativeBufferSize();
-
- Array<int> retval;
- for (int i = 1; i < defaultNumMultiples; ++i)
- retval.add (i * nativeBufferSize);
-
- return retval;
- }
-
- String open (const BigInteger& inputChannels,
- const BigInteger& outputChannels,
- double requestedSampleRate,
- int bufferSize) override
- {
- close();
-
- lastError.clear();
- sampleRate = (int) requestedSampleRate;
-
- int preferredBufferSize = (bufferSize <= 0) ? getDefaultBufferSize() : bufferSize;
-
- activeOutputChans = outputChannels;
- activeOutputChans.setRange (2, activeOutputChans.getHighestBit(), false);
- numOutputChannels = activeOutputChans.countNumberOfSetBits();
-
- activeInputChans = inputChannels;
- activeInputChans.setRange (1, activeInputChans.getHighestBit(), false);
- numInputChannels = activeInputChans.countNumberOfSetBits();
-
- actualBufferSize = preferredBufferSize;
-
- inputBuffer.setSize (jmax (1, numInputChannels), actualBufferSize);
- outputBuffer.setSize (jmax (1, numOutputChannels), actualBufferSize);
- outputBuffer.clear();
-
- const int audioBuffersToEnqueue = hasLowLatencyAudioPath() ? buffersToEnqueueForLowLatency
- : buffersToEnqueueSlowAudio;
-
- DBG ("OpenSL: numInputChannels = " << numInputChannels
- << ", numOutputChannels = " << numOutputChannels
- << ", nativeBufferSize = " << getNativeBufferSize()
- << ", nativeSampleRate = " << getNativeSampleRate()
- << ", actualBufferSize = " << actualBufferSize
- << ", audioBuffersToEnqueue = " << audioBuffersToEnqueue
- << ", sampleRate = " << sampleRate);
-
- if (numInputChannels > 0)
- {
- if (! RuntimePermissions::isGranted (RuntimePermissions::recordAudio))
- {
- // If you hit this assert, you probably forgot to get RuntimePermissions::recordAudio
- // before trying to open an audio input device. This is not going to work!
- jassertfalse;
- lastError = "Error opening OpenSL input device: the app was not granted android.permission.RECORD_AUDIO";
- }
- else
- {
- recorder = engine.createRecorder (numInputChannels, sampleRate,
- audioBuffersToEnqueue, actualBufferSize);
-
- if (recorder == nullptr)
- lastError = "Error opening OpenSL input device: creating Recorder failed.";
- }
- }
-
- if (numOutputChannels > 0)
- {
- player = engine.createPlayer (numOutputChannels, sampleRate,
- audioBuffersToEnqueue, actualBufferSize);
-
- if (player == nullptr)
- lastError = "Error opening OpenSL input device: creating Player failed.";
- }
-
- // pre-fill buffers
- for (int i = 0; i < audioBuffersToEnqueue; ++i)
- processBuffers();
-
- startThread (8);
-
- deviceOpen = true;
- return lastError;
- }
-
- void close() override
- {
- stop();
- stopThread (6000);
- deviceOpen = false;
- recorder = nullptr;
- player = nullptr;
- }
-
- int getOutputLatencyInSamples() override { return outputLatency; }
- int getInputLatencyInSamples() override { return inputLatency; }
- bool isOpen() override { return deviceOpen; }
- int getCurrentBufferSizeSamples() override { return actualBufferSize; }
- int getCurrentBitDepth() override { return 16; }
- BigInteger getActiveOutputChannels() const override { return activeOutputChans; }
- BigInteger getActiveInputChannels() const override { return activeInputChans; }
- String getLastError() override { return lastError; }
- bool isPlaying() override { return callback != nullptr; }
-
- int getDefaultBufferSize() override
- {
- // Only on a Pro-Audio device will we set the lowest possible buffer size
- // by default. We need to be more conservative on other devices
- // as they may be low-latency, but still have a crappy CPU.
- return (isProAudioDevice() ? 1 : 6)
- * defaultBufferSizeIsMultipleOfNative * getNativeBufferSize();
- }
-
- double getCurrentSampleRate() override
- {
- return (sampleRate == 0.0 ? getNativeSampleRate() : sampleRate);
- }
-
- void start (AudioIODeviceCallback* newCallback) override
- {
- stop();
-
- if (deviceOpen && callback != newCallback)
- {
- if (newCallback != nullptr)
- newCallback->audioDeviceAboutToStart (this);
-
- setCallback (newCallback);
- }
- }
-
- void stop() override
- {
- if (AudioIODeviceCallback* const oldCallback = setCallback (nullptr))
- oldCallback->audioDeviceStopped();
- }
-
- bool setAudioPreprocessingEnabled (bool enable) override
- {
- return recorder != nullptr && recorder->setAudioPreprocessingEnabled (enable);
- }
-
- private:
- //==============================================================================
- CriticalSection callbackLock;
- AudioIODeviceCallback* callback;
- int actualBufferSize, sampleRate;
- int inputLatency, outputLatency;
- bool deviceOpen;
- String lastError;
- BigInteger activeOutputChans, activeInputChans;
- int numInputChannels, numOutputChannels;
- AudioSampleBuffer inputBuffer, outputBuffer;
- struct Player;
- struct Recorder;
-
- enum
- {
- // The number of buffers to enqueue needs to be at least two for the audio to use the low-latency
- // audio path (see "Performance" section in ndk/docs/Additional_library_docs/opensles/index.html)
- buffersToEnqueueForLowLatency = 2,
- buffersToEnqueueSlowAudio = 4,
- defaultBufferSizeIsMultipleOfNative = 1
- };
-
- //==============================================================================
- static String audioManagerGetProperty (const String& property)
- {
- const LocalRef<jstring> jProperty (javaString (property));
- const LocalRef<jstring> text ((jstring) android.activity.callObjectMethod (JuceAppActivity.audioManagerGetProperty,
- jProperty.get()));
- if (text.get() != 0)
- return juceString (text);
-
- return String();
- }
-
- static bool androidHasSystemFeature (const String& property)
- {
- const LocalRef<jstring> jProperty (javaString (property));
- return android.activity.callBooleanMethod (JuceAppActivity.hasSystemFeature, jProperty.get());
- }
-
- static double getNativeSampleRate()
- {
- return audioManagerGetProperty ("android.media.property.OUTPUT_SAMPLE_RATE").getDoubleValue();
- }
-
- static int getNativeBufferSize()
- {
- const int val = audioManagerGetProperty ("android.media.property.OUTPUT_FRAMES_PER_BUFFER").getIntValue();
- return val > 0 ? val : 512;
- }
-
- static bool isProAudioDevice()
- {
- return androidHasSystemFeature ("android.hardware.audio.pro");
- }
-
- static bool hasLowLatencyAudioPath()
- {
- return androidHasSystemFeature ("android.hardware.audio.low_latency");
- }
-
- //==============================================================================
- AudioIODeviceCallback* setCallback (AudioIODeviceCallback* const newCallback)
- {
- const ScopedLock sl (callbackLock);
- AudioIODeviceCallback* const oldCallback = callback;
- callback = newCallback;
- return oldCallback;
- }
-
- void processBuffers()
- {
- if (recorder != nullptr)
- recorder->readNextBlock (inputBuffer, *this);
-
- {
- const ScopedLock sl (callbackLock);
-
- if (callback != nullptr)
- callback->audioDeviceIOCallback (numInputChannels > 0 ? inputBuffer.getArrayOfReadPointers() : nullptr, numInputChannels,
- numOutputChannels > 0 ? outputBuffer.getArrayOfWritePointers() : nullptr, numOutputChannels,
- actualBufferSize);
- else
- outputBuffer.clear();
- }
-
- if (player != nullptr)
- player->writeBuffer (outputBuffer, *this);
- }
-
- void run() override
- {
- setThreadToAudioPriority ();
-
- if (recorder != nullptr) recorder->start();
- if (player != nullptr) player->start();
-
- while (! threadShouldExit())
- processBuffers();
- }
-
- void setThreadToAudioPriority ()
- {
- // see android.os.Process.THREAD_PRIORITY_AUDIO
- const int THREAD_PRIORITY_AUDIO = -16;
- jint priority = THREAD_PRIORITY_AUDIO;
-
- if (priority != android.activity.callIntMethod (JuceAppActivity.setCurrentThreadPriority, (jint) priority))
- DBG ("Unable to set audio thread priority: priority is still " << priority);
- }
-
- //==============================================================================
- struct Engine
- {
- Engine()
- : engineObject (nullptr), engineInterface (nullptr), outputMixObject (nullptr)
- {
- if (library.open ("libOpenSLES.so"))
- {
- typedef SLresult (*CreateEngineFunc) (SLObjectItf*, SLuint32, const SLEngineOption*,
- SLuint32, const SLInterfaceID*, const SLboolean*);
-
- if (CreateEngineFunc createEngine = (CreateEngineFunc) library.getFunction ("slCreateEngine"))
- {
- check (createEngine (&engineObject, 0, nullptr, 0, nullptr, nullptr));
-
- SLInterfaceID* SL_IID_ENGINE = (SLInterfaceID*) library.getFunction ("SL_IID_ENGINE");
- SL_IID_ANDROIDSIMPLEBUFFERQUEUE = (SLInterfaceID*) library.getFunction ("SL_IID_ANDROIDSIMPLEBUFFERQUEUE");
- SL_IID_PLAY = (SLInterfaceID*) library.getFunction ("SL_IID_PLAY");
- SL_IID_RECORD = (SLInterfaceID*) library.getFunction ("SL_IID_RECORD");
- SL_IID_ANDROIDCONFIGURATION = (SLInterfaceID*) library.getFunction ("SL_IID_ANDROIDCONFIGURATION");
-
- check ((*engineObject)->Realize (engineObject, SL_BOOLEAN_FALSE));
- check ((*engineObject)->GetInterface (engineObject, *SL_IID_ENGINE, &engineInterface));
-
- check ((*engineInterface)->CreateOutputMix (engineInterface, &outputMixObject, 0, nullptr, nullptr));
- check ((*outputMixObject)->Realize (outputMixObject, SL_BOOLEAN_FALSE));
- }
- }
- }
-
- ~Engine()
- {
- if (outputMixObject != nullptr) (*outputMixObject)->Destroy (outputMixObject);
- if (engineObject != nullptr) (*engineObject)->Destroy (engineObject);
- }
-
- Player* createPlayer (const int numChannels, const int sampleRate, const int numBuffers, const int bufferSize)
- {
- if (numChannels <= 0)
- return nullptr;
-
- ScopedPointer<Player> player (new Player (numChannels, sampleRate, *this, numBuffers, bufferSize));
- return player->openedOk() ? player.release() : nullptr;
- }
-
- Recorder* createRecorder (const int numChannels, const int sampleRate, const int numBuffers, const int bufferSize)
- {
- if (numChannels <= 0)
- return nullptr;
-
- ScopedPointer<Recorder> recorder (new Recorder (numChannels, sampleRate, *this, numBuffers, bufferSize));
- return recorder->openedOk() ? recorder.release() : nullptr;
- }
-
- SLObjectItf engineObject;
- SLEngineItf engineInterface;
- SLObjectItf outputMixObject;
-
- SLInterfaceID* SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
- SLInterfaceID* SL_IID_PLAY;
- SLInterfaceID* SL_IID_RECORD;
- SLInterfaceID* SL_IID_ANDROIDCONFIGURATION;
-
- private:
- DynamicLibrary library;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Engine)
- };
-
- //==============================================================================
- struct BufferList
- {
- BufferList (const int numChannels_, const int numBuffers_, const int numSamples_)
- : numChannels (numChannels_), numBuffers (numBuffers_), numSamples (numSamples_),
- bufferSpace (numChannels_ * numSamples * numBuffers), nextBlock (0)
- {
- }
-
- int16* waitForFreeBuffer (Thread& threadToCheck) noexcept
- {
- while (numBlocksOut.get() == numBuffers)
- {
- dataArrived.wait (1);
-
- if (threadToCheck.threadShouldExit())
- return nullptr;
- }
-
- return getNextBuffer();
- }
-
- int16* getNextBuffer() noexcept
- {
- if (++nextBlock == numBuffers)
- nextBlock = 0;
-
- return bufferSpace + nextBlock * numChannels * numSamples;
- }
-
- void bufferReturned() noexcept { --numBlocksOut; dataArrived.signal(); }
- void bufferSent() noexcept { ++numBlocksOut; dataArrived.signal(); }
-
- int getBufferSizeBytes() const noexcept { return numChannels * numSamples * sizeof (int16); }
-
- const int numChannels, numBuffers, numSamples;
-
- private:
- HeapBlock<int16> bufferSpace;
- int nextBlock;
- Atomic<int> numBlocksOut;
- WaitableEvent dataArrived;
- };
-
- //==============================================================================
- struct Player
- {
- Player (int numChannels, int sampleRate, Engine& engine, int playerNumBuffers, int playerBufferSize)
- : playerObject (nullptr), playerPlay (nullptr), playerBufferQueue (nullptr),
- bufferList (numChannels, playerNumBuffers, playerBufferSize)
- {
- SLDataFormat_PCM pcmFormat =
- {
- SL_DATAFORMAT_PCM,
- (SLuint32) numChannels,
- (SLuint32) (sampleRate * 1000),
- SL_PCMSAMPLEFORMAT_FIXED_16,
- SL_PCMSAMPLEFORMAT_FIXED_16,
- (numChannels == 1) ? SL_SPEAKER_FRONT_CENTER : (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT),
- SL_BYTEORDER_LITTLEENDIAN
- };
-
- SLDataLocator_AndroidSimpleBufferQueue bufferQueue = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
- static_cast<SLuint32> (bufferList.numBuffers) };
- SLDataSource audioSrc = { &bufferQueue, &pcmFormat };
-
- SLDataLocator_OutputMix outputMix = { SL_DATALOCATOR_OUTPUTMIX, engine.outputMixObject };
- SLDataSink audioSink = { &outputMix, nullptr };
-
- // (SL_IID_BUFFERQUEUE is not guaranteed to remain future-proof, so use SL_IID_ANDROIDSIMPLEBUFFERQUEUE)
- const SLInterfaceID interfaceIDs[] = { *engine.SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
- const SLboolean flags[] = { SL_BOOLEAN_TRUE };
-
- check ((*engine.engineInterface)->CreateAudioPlayer (engine.engineInterface, &playerObject, &audioSrc, &audioSink,
- 1, interfaceIDs, flags));
-
- check ((*playerObject)->Realize (playerObject, SL_BOOLEAN_FALSE));
- check ((*playerObject)->GetInterface (playerObject, *engine.SL_IID_PLAY, &playerPlay));
- check ((*playerObject)->GetInterface (playerObject, *engine.SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &playerBufferQueue));
- check ((*playerBufferQueue)->RegisterCallback (playerBufferQueue, staticCallback, this));
- }
-
- ~Player()
- {
- if (playerPlay != nullptr)
- check ((*playerPlay)->SetPlayState (playerPlay, SL_PLAYSTATE_STOPPED));
-
- if (playerBufferQueue != nullptr)
- check ((*playerBufferQueue)->Clear (playerBufferQueue));
-
- if (playerObject != nullptr)
- (*playerObject)->Destroy (playerObject);
- }
-
- bool openedOk() const noexcept { return playerBufferQueue != nullptr; }
-
- void start()
- {
- jassert (openedOk());
-
- check ((*playerPlay)->SetPlayState (playerPlay, SL_PLAYSTATE_PLAYING));
- }
-
- void writeBuffer (const AudioSampleBuffer& buffer, Thread& thread) noexcept
- {
- jassert (buffer.getNumChannels() == bufferList.numChannels);
- jassert (buffer.getNumSamples() < bufferList.numSamples * bufferList.numBuffers);
-
- int offset = 0;
- int numSamples = buffer.getNumSamples();
-
- while (numSamples > 0)
- {
- if (int16* const destBuffer = bufferList.waitForFreeBuffer (thread))
- {
- for (int i = 0; i < bufferList.numChannels; ++i)
- {
- typedef AudioData::Pointer<AudioData::Int16, AudioData::LittleEndian, AudioData::Interleaved, AudioData::NonConst> DstSampleType;
- typedef AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const> SrcSampleType;
-
- DstSampleType dstData (destBuffer + i, bufferList.numChannels);
- SrcSampleType srcData (buffer.getReadPointer (i, offset));
- dstData.convertSamples (srcData, bufferList.numSamples);
- }
-
- enqueueBuffer (destBuffer);
-
- numSamples -= bufferList.numSamples;
- offset += bufferList.numSamples;
- }
- else
- {
- break;
- }
- }
- }
-
- private:
- SLObjectItf playerObject;
- SLPlayItf playerPlay;
- SLAndroidSimpleBufferQueueItf playerBufferQueue;
-
- BufferList bufferList;
-
- void enqueueBuffer (int16* buffer) noexcept
- {
- check ((*playerBufferQueue)->Enqueue (playerBufferQueue, buffer, bufferList.getBufferSizeBytes()));
- bufferList.bufferSent();
- }
-
- static void staticCallback (SLAndroidSimpleBufferQueueItf queue, void* context) noexcept
- {
- jassert (queue == static_cast<Player*> (context)->playerBufferQueue); ignoreUnused (queue);
- static_cast<Player*> (context)->bufferList.bufferReturned();
- }
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Player)
- };
-
- //==============================================================================
- struct Recorder
- {
- Recorder (int numChannels, int sampleRate, Engine& engine, const int numBuffers, const int numSamples)
- : recorderObject (nullptr), recorderRecord (nullptr),
- recorderBufferQueue (nullptr), configObject (nullptr),
- bufferList (numChannels, numBuffers, numSamples)
- {
- SLDataFormat_PCM pcmFormat =
- {
- SL_DATAFORMAT_PCM,
- (SLuint32) numChannels,
- (SLuint32) (sampleRate * 1000), // (sample rate units are millihertz)
- SL_PCMSAMPLEFORMAT_FIXED_16,
- SL_PCMSAMPLEFORMAT_FIXED_16,
- (numChannels == 1) ? SL_SPEAKER_FRONT_CENTER : (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT),
- SL_BYTEORDER_LITTLEENDIAN
- };
-
- SLDataLocator_IODevice ioDevice = { SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, nullptr };
- SLDataSource audioSrc = { &ioDevice, nullptr };
-
- SLDataLocator_AndroidSimpleBufferQueue bufferQueue = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
- static_cast<SLuint32> (bufferList.numBuffers) };
- SLDataSink audioSink = { &bufferQueue, &pcmFormat };
-
- const SLInterfaceID interfaceIDs[] = { *engine.SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
- const SLboolean flags[] = { SL_BOOLEAN_TRUE };
-
- if (check ((*engine.engineInterface)->CreateAudioRecorder (engine.engineInterface, &recorderObject, &audioSrc,
- &audioSink, 1, interfaceIDs, flags)))
- {
- if (check ((*recorderObject)->Realize (recorderObject, SL_BOOLEAN_FALSE)))
- {
- check ((*recorderObject)->GetInterface (recorderObject, *engine.SL_IID_RECORD, &recorderRecord));
- check ((*recorderObject)->GetInterface (recorderObject, *engine.SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &recorderBufferQueue));
- // not all android versions seem to have a config object
- SLresult result = (*recorderObject)->GetInterface (recorderObject,
- *engine.SL_IID_ANDROIDCONFIGURATION, &configObject);
- if (result != SL_RESULT_SUCCESS)
- configObject = nullptr;
-
- check ((*recorderBufferQueue)->RegisterCallback (recorderBufferQueue, staticCallback, this));
- check ((*recorderRecord)->SetRecordState (recorderRecord, SL_RECORDSTATE_STOPPED));
- }
- }
- }
-
- ~Recorder()
- {
- if (recorderRecord != nullptr)
- check ((*recorderRecord)->SetRecordState (recorderRecord, SL_RECORDSTATE_STOPPED));
-
- if (recorderBufferQueue != nullptr)
- check ((*recorderBufferQueue)->Clear (recorderBufferQueue));
-
- if (recorderObject != nullptr)
- (*recorderObject)->Destroy (recorderObject);
- }
-
- bool openedOk() const noexcept { return recorderBufferQueue != nullptr; }
-
- void start()
- {
- jassert (openedOk());
- check ((*recorderRecord)->SetRecordState (recorderRecord, SL_RECORDSTATE_RECORDING));
- }
-
- void readNextBlock (AudioSampleBuffer& buffer, Thread& thread)
- {
- jassert (buffer.getNumChannels() == bufferList.numChannels);
- jassert (buffer.getNumSamples() < bufferList.numSamples * bufferList.numBuffers);
- jassert ((buffer.getNumSamples() % bufferList.numSamples) == 0);
-
- int offset = 0;
- int numSamples = buffer.getNumSamples();
-
- while (numSamples > 0)
- {
- if (int16* const srcBuffer = bufferList.waitForFreeBuffer (thread))
- {
- for (int i = 0; i < bufferList.numChannels; ++i)
- {
- typedef AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst> DstSampleType;
- typedef AudioData::Pointer<AudioData::Int16, AudioData::LittleEndian, AudioData::Interleaved, AudioData::Const> SrcSampleType;
-
- DstSampleType dstData (buffer.getWritePointer (i, offset));
- SrcSampleType srcData (srcBuffer + i, bufferList.numChannels);
- dstData.convertSamples (srcData, bufferList.numSamples);
- }
-
- enqueueBuffer (srcBuffer);
-
- numSamples -= bufferList.numSamples;
- offset += bufferList.numSamples;
- }
- else
- {
- break;
- }
- }
- }
-
- bool setAudioPreprocessingEnabled (bool enable)
- {
- SLuint32 mode = enable ? SL_ANDROID_RECORDING_PRESET_GENERIC
- : SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION;
-
- return configObject != nullptr
- && check ((*configObject)->SetConfiguration (configObject, SL_ANDROID_KEY_RECORDING_PRESET, &mode, sizeof (mode)));
- }
-
- private:
- SLObjectItf recorderObject;
- SLRecordItf recorderRecord;
- SLAndroidSimpleBufferQueueItf recorderBufferQueue;
- SLAndroidConfigurationItf configObject;
-
- BufferList bufferList;
-
- void enqueueBuffer (int16* buffer) noexcept
- {
- check ((*recorderBufferQueue)->Enqueue (recorderBufferQueue, buffer, bufferList.getBufferSizeBytes()));
- bufferList.bufferSent();
- }
-
- static void staticCallback (SLAndroidSimpleBufferQueueItf queue, void* context) noexcept
- {
- jassert (queue == static_cast<Recorder*> (context)->recorderBufferQueue); ignoreUnused (queue);
- static_cast<Recorder*> (context)->bufferList.bufferReturned();
- }
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Recorder)
- };
-
-
- //==============================================================================
- Engine engine;
-
- ScopedPointer<Player> player;
- ScopedPointer<Recorder> recorder;
-
- //==============================================================================
- static bool check (const SLresult result) noexcept
- {
- jassert (result == SL_RESULT_SUCCESS);
- return result == SL_RESULT_SUCCESS;
- }
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioIODevice)
- };
-
-
- //==============================================================================
- class OpenSLAudioDeviceType : public AudioIODeviceType
- {
- public:
- OpenSLAudioDeviceType() : AudioIODeviceType (openSLTypeName) {}
-
- //==============================================================================
- void scanForDevices() override {}
- StringArray getDeviceNames (bool wantInputNames) const override { return StringArray (openSLTypeName); }
- int getDefaultDeviceIndex (bool forInput) const override { return 0; }
- int getIndexOfDevice (AudioIODevice* device, bool asInput) const override { return device != nullptr ? 0 : -1; }
- bool hasSeparateInputsAndOutputs() const override { return false; }
-
- AudioIODevice* createDevice (const String& outputDeviceName,
- const String& inputDeviceName) override
- {
- ScopedPointer<OpenSLAudioIODevice> dev;
-
- if (outputDeviceName.isNotEmpty() || inputDeviceName.isNotEmpty())
- {
- dev = new OpenSLAudioIODevice (outputDeviceName.isNotEmpty() ? outputDeviceName
- : inputDeviceName);
- if (! dev->openedOk())
- dev = nullptr;
- }
-
- return dev.release();
- }
-
- private:
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenSLAudioDeviceType)
- };
-
-
- //==============================================================================
- AudioIODeviceType* AudioIODeviceType::createAudioIODeviceType_OpenSLES()
- {
- return isOpenSLAvailable() ? new OpenSLAudioDeviceType() : nullptr;
- }
|