Skip to content
This repository was archived by the owner on Aug 16, 2023. It is now read-only.

Commit f337960

Browse files
authored
Merge pull request #2 from dturner/master
Changing bool to std::atomic<bool>
2 parents 0558b4e + 9db579d commit f337960

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

app/src/main/cpp/Oscillator.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ void Oscillator::setSampleRate(int32_t sampleRate) {
2626
}
2727

2828
void Oscillator::setWaveOn(bool isWaveOn) {
29-
isWaveOn_ = isWaveOn;
30-
if (!isWaveOn) phase_ = 0;
29+
isWaveOn_.store(isWaveOn);
3130
}
3231

3332
void Oscillator::render(float *audioData, int32_t numFrames) {
3433

34+
// If the wave has been switched off then reset the phase to zero. Starting at a non-zero value
35+
// could result in an unwanted audible 'click'
36+
if (!isWaveOn_.load()) phase_ = 0;
37+
3538
for (int i = 0; i < numFrames; i++) {
3639

37-
if (isWaveOn_) {
40+
if (isWaveOn_.load()) {
3841

3942
// Calculates the next sample value for the sine wave.
4043
audioData[i] = (float) (sin(phase_) * AMPLITUDE);

app/src/main/cpp/Oscillator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef PART1_OSCILLATOR_H
1818
#define PART1_OSCILLATOR_H
1919

20-
20+
#include <atomic>
2121
#include <stdint.h>
2222

2323
class Oscillator {
@@ -29,7 +29,8 @@ class Oscillator {
2929
void render(float *audioData, int32_t numFrames);
3030

3131
private:
32-
bool isWaveOn_ = false;
32+
// We use an atomic bool to define isWaveOn_ because it is accessed from multiple threads.
33+
std::atomic<bool> isWaveOn_{false};
3334
double phase_ = 0.0;
3435
double phaseIncrement_ = 0.0;
3536
};

0 commit comments

Comments
 (0)