This repository was archived by the owner on Aug 16, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +9
-5
lines changed
Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -26,15 +26,18 @@ void Oscillator::setSampleRate(int32_t sampleRate) {
2626}
2727
2828void Oscillator::setWaveOn (bool isWaveOn) {
29- isWaveOn_ = isWaveOn;
30- if (!isWaveOn) phase_ = 0 ;
29+ isWaveOn_.store (isWaveOn);
3130}
3231
3332void 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);
Original file line number Diff line number Diff line change 1717#ifndef PART1_OSCILLATOR_H
1818#define PART1_OSCILLATOR_H
1919
20-
20+ # include < atomic >
2121#include < stdint.h>
2222
2323class Oscillator {
@@ -29,7 +29,8 @@ class Oscillator {
2929 void render (float *audioData, int32_t numFrames);
3030
3131private:
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};
You can’t perform that action at this time.
0 commit comments