Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinit. Audio Engine for MegaDrone on restart. #523

Merged
merged 6 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions samples/MegaDrone/src/main/cpp/Synth.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,19 @@ class Synth : public IRenderableAudio {
public:

Synth(int32_t sampleRate, int32_t channelCount) {

for (int i = 0; i < kNumOscillators; ++i) {
mOscs[i].setSampleRate(sampleRate);
mOscs[i].setFrequency(kOscBaseFrequency+(static_cast<float>(i)/kOscDivisor));
mOscs[i].setAmplitude(kOscAmplitude);

std::shared_ptr<IRenderableAudio> pOsc(&mOscs[i]);
mMixer.addTrack(pOsc);
mMixer.addTrack(&mOscs[i]);
}

if (channelCount == oboe::ChannelCount::Stereo){
mOutputStage = std::make_shared<MonoToStereo>(&mMixer);
if (channelCount == oboe::ChannelCount::Stereo) {
mOutputStage = &mConverter;
} else {
mOutputStage.reset(&mMixer);
mOutputStage = &mMixer;
}
}

// From ISynth
void setWaveOn(bool isEnabled) {
for (auto &osc : mOscs) osc.setWaveOn(isEnabled);
};
Expand All @@ -60,13 +55,14 @@ class Synth : public IRenderableAudio {
mOutputStage->renderAudio(audioData, numFrames);
};

virtual ~Synth() {}
virtual ~Synth() {
}
private:

// Rendering objects
std::array<Oscillator, kNumOscillators> mOscs;
Mixer mMixer;
std::shared_ptr<IRenderableAudio> mOutputStage;
MonoToStereo mConverter = MonoToStereo(&mMixer);
IRenderableAudio *mOutputStage; // This will point to either the mixer or converter, so it needs to be raw
};


Expand Down
12 changes: 6 additions & 6 deletions samples/MegaDrone/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "AudioEngine.h"

AudioEngine engine;
AudioEngine *engine;

std::vector<int> convertJavaArrayToVector(JNIEnv *env, jintArray intArray){

Expand All @@ -41,23 +41,23 @@ extern "C"
JNIEXPORT void JNICALL
Java_com_example_oboe_megadrone_MainActivity_startEngine(JNIEnv *env, jobject instance,
jintArray jCpuIds) {

engine = new AudioEngine;
std::vector<int> cpuIds = convertJavaArrayToVector(env, jCpuIds);
engine.start(cpuIds);
engine->start(cpuIds);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_oboe_megadrone_MainActivity_stopEngine(JNIEnv *env, jobject instance) {

engine.stop();

engine->stop();
delete engine;
}


extern "C"
JNIEXPORT void JNICALL
Java_com_example_oboe_megadrone_MainActivity_tap(JNIEnv *env, jobject instance, jboolean b) {

engine.tap(b);
engine->tap(b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ protected void onResume(){
}

protected void onPause(){
super.onPause();
stopEngine();
super.onPause();
}

private native void stopEngine();
Expand Down
8 changes: 4 additions & 4 deletions samples/RhythmGame/src/main/cpp/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ bool Game::setupAudioSources() {
LOGE("Could not load source data for clap sound");
return false;
}
mClap = std::make_shared<Player>(mClapSource);
mClap = std::make_unique<Player>(mClapSource);

// Create a data source and player for our backing track
std::shared_ptr<AAssetDataSource> backingTrackSource {
Expand All @@ -229,13 +229,13 @@ bool Game::setupAudioSources() {
LOGE("Could not load source data for backing track");
return false;
}
mBackingTrack = std::make_shared<Player>(backingTrackSource);
mBackingTrack = std::make_unique<Player>(backingTrackSource);
mBackingTrack->setPlaying(true);
mBackingTrack->setLooping(true);

// Add both players to a mixer
mMixer.addTrack(mClap);
mMixer.addTrack(mBackingTrack);
mMixer.addTrack(mClap.get());
mMixer.addTrack(mBackingTrack.get());

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions samples/RhythmGame/src/main/cpp/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class Game : public AudioStreamCallback {
private:
AAssetManager& mAssetManager;
AudioStream *mAudioStream { nullptr };
std::shared_ptr<Player> mClap;
std::shared_ptr<Player> mBackingTrack;
std::unique_ptr<Player> mClap;
std::unique_ptr<Player> mBackingTrack;
Mixer mMixer;
std::unique_ptr<float[]> mConversionBuffer { nullptr }; // For float->int16 conversion

Expand Down
5 changes: 3 additions & 2 deletions samples/shared/Mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ constexpr uint8_t kMaxTracks = 100;
* A Mixer object which sums the output from multiple tracks into a single output. The number of
* input channels on each track must match the number of output channels (default 1=mono). This can
* be changed by calling `setChannelCount`.
* The inputs to the mixer are not owned by the mixer, they should not be deleted while rendering.
*/
class Mixer : public IRenderableAudio {

Expand All @@ -45,15 +46,15 @@ class Mixer : public IRenderableAudio {
}
}

philburk marked this conversation as resolved.
Show resolved Hide resolved
void addTrack(std::shared_ptr<IRenderableAudio> renderer){
void addTrack(IRenderableAudio *renderer){
mTracks[mNextFreeTrackIndex++] = renderer;
}

void setChannelCount(int32_t channelCount){ mChannelCount = channelCount; }

private:
float mixingBuffer[kBufferSize];
std::array<std::shared_ptr<IRenderableAudio>, kMaxTracks> mTracks;
std::array<IRenderableAudio*, kMaxTracks> mTracks;
uint8_t mNextFreeTrackIndex = 0;
int32_t mChannelCount = 1; // Default to mono
};
Expand Down