2016-12-03 17:41:02 +00:00
|
|
|
//
|
|
|
|
// Speaker.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 03/12/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Speaker.hpp"
|
|
|
|
|
|
|
|
using namespace Electron;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::get_samples(unsigned int number_of_samples, int16_t *target) {
|
|
|
|
if(is_enabled_) {
|
|
|
|
while(number_of_samples--) {
|
2016-12-03 17:41:02 +00:00
|
|
|
*target = (int16_t)((counter_ / (divider_+1)) * 8192);
|
|
|
|
target++;
|
|
|
|
counter_ = (counter_ + 1) % ((divider_+1) * 2);
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 17:41:02 +00:00
|
|
|
memset(target, 0, sizeof(int16_t) * number_of_samples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::skip_samples(unsigned int number_of_samples) {
|
2016-12-03 17:41:02 +00:00
|
|
|
counter_ = (counter_ + number_of_samples) % ((divider_+1) * 2);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::set_divider(uint8_t divider) {
|
2016-12-03 17:41:02 +00:00
|
|
|
enqueue([=]() {
|
|
|
|
divider_ = divider * 32 / clock_rate_divider;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Speaker::set_is_enabled(bool is_enabled) {
|
2016-12-03 17:41:02 +00:00
|
|
|
enqueue([=]() {
|
|
|
|
is_enabled_ = is_enabled;
|
|
|
|
counter_ = 0;
|
|
|
|
});
|
|
|
|
}
|