2017-12-18 02:26:06 +00:00
|
|
|
//
|
|
|
|
// Speaker.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 12/01/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2017-12-18 02:26:06 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Speaker_hpp
|
|
|
|
#define Speaker_hpp
|
|
|
|
|
2020-02-19 01:33:31 +00:00
|
|
|
#include <atomic>
|
2017-12-18 02:26:06 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Outputs {
|
|
|
|
namespace Speaker {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides a communication point for sound; machines that have a speaker provide an
|
|
|
|
audio output.
|
|
|
|
*/
|
|
|
|
class Speaker {
|
|
|
|
public:
|
|
|
|
virtual ~Speaker() {}
|
|
|
|
|
2020-02-15 18:40:19 +00:00
|
|
|
/*!
|
|
|
|
@returns The best output clock rate for the audio being supplied to this speaker, from the range given.
|
|
|
|
*/
|
2017-12-18 02:26:06 +00:00
|
|
|
virtual float get_ideal_clock_rate_in_range(float minimum, float maximum) = 0;
|
2020-02-15 18:40:19 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns @c true if the device would most ideally output stereo sound; @c false otherwise.
|
|
|
|
*/
|
2020-02-16 23:50:34 +00:00
|
|
|
virtual bool get_is_stereo() = 0;
|
2020-02-15 18:40:19 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets the actual output rate; packets provided to the delegate will conform to these
|
|
|
|
specifications regardless of the input.
|
|
|
|
*/
|
|
|
|
void set_output_rate(float cycles_per_second, int buffer_size, bool stereo) {
|
2020-01-26 18:25:23 +00:00
|
|
|
output_cycles_per_second_ = cycles_per_second;
|
|
|
|
output_buffer_size_ = buffer_size;
|
2020-02-15 18:40:19 +00:00
|
|
|
stereo_output_ = stereo;
|
2020-01-26 18:25:23 +00:00
|
|
|
compute_output_rate();
|
|
|
|
}
|
2020-02-15 18:40:19 +00:00
|
|
|
|
2020-07-31 02:50:32 +00:00
|
|
|
/*!
|
|
|
|
Takes a copy of the most recent output rate provided to @c rhs.
|
|
|
|
*/
|
|
|
|
void copy_output_rate(const Speaker &rhs) {
|
|
|
|
output_cycles_per_second_ = rhs.output_cycles_per_second_;
|
|
|
|
output_buffer_size_ = rhs.output_buffer_size_;
|
|
|
|
stereo_output_.store(rhs.stereo_output_.load(std::memory_order::memory_order_relaxed), std::memory_order::memory_order_relaxed);
|
|
|
|
compute_output_rate();
|
|
|
|
}
|
|
|
|
|
2020-03-22 02:00:47 +00:00
|
|
|
/// Sets the output volume, in the range [0, 1].
|
|
|
|
virtual void set_output_volume(float) = 0;
|
|
|
|
|
2020-02-15 18:40:19 +00:00
|
|
|
/*!
|
|
|
|
Speeds a speed multiplier for this machine, e.g. that it is currently being run at 2.0x its normal rate.
|
|
|
|
This will affect the number of input samples that are combined to produce one output sample.
|
|
|
|
*/
|
2020-01-26 18:25:23 +00:00
|
|
|
void set_input_rate_multiplier(float multiplier) {
|
|
|
|
input_rate_multiplier_ = multiplier;
|
|
|
|
compute_output_rate();
|
|
|
|
}
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2020-02-15 18:40:19 +00:00
|
|
|
/*!
|
|
|
|
@returns The number of sample sets so far delivered to the delegate.
|
|
|
|
*/
|
2020-01-20 21:08:21 +00:00
|
|
|
int completed_sample_sets() const { return completed_sample_sets_; }
|
2020-01-20 17:12:23 +00:00
|
|
|
|
2020-02-15 18:40:19 +00:00
|
|
|
/*!
|
|
|
|
Defines a receiver for audio packets.
|
|
|
|
*/
|
2017-12-18 02:26:06 +00:00
|
|
|
struct Delegate {
|
2020-02-15 18:40:19 +00:00
|
|
|
/*!
|
|
|
|
Indicates that a new audio packet is ready. If the output is stereo, samples will be interleaved with the first
|
|
|
|
being left, the second being right, etc.
|
|
|
|
*/
|
2017-12-18 02:26:06 +00:00
|
|
|
virtual void speaker_did_complete_samples(Speaker *speaker, const std::vector<int16_t> &buffer) = 0;
|
2020-02-15 18:40:19 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides the delegate with a hint that the input clock rate has changed, which provides an opportunity to
|
|
|
|
renegotiate the ideal clock rate, if desired.
|
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
virtual void speaker_did_change_input_clock([[maybe_unused]] Speaker *speaker) {}
|
2017-12-18 02:26:06 +00:00
|
|
|
};
|
2018-02-18 20:23:15 +00:00
|
|
|
virtual void set_delegate(Delegate *delegate) {
|
2020-07-31 22:20:35 +00:00
|
|
|
delegate_.store(delegate, std::memory_order::memory_order_relaxed);
|
2017-12-18 02:26:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 02:00:47 +00:00
|
|
|
|
|
|
|
// This is primarily exposed for MultiSpeaker et al; it's not for general callers.
|
2020-02-15 18:40:19 +00:00
|
|
|
virtual void set_computed_output_rate(float cycles_per_second, int buffer_size, bool stereo) = 0;
|
2020-01-26 18:25:23 +00:00
|
|
|
|
2017-12-18 02:26:06 +00:00
|
|
|
protected:
|
2020-05-30 04:37:06 +00:00
|
|
|
void did_complete_samples(Speaker *, const std::vector<int16_t> &buffer, bool is_stereo) {
|
2020-02-19 01:33:31 +00:00
|
|
|
// Test the delegate for existence again, as it may have changed.
|
2020-07-31 22:20:35 +00:00
|
|
|
const auto delegate = delegate_.load(std::memory_order::memory_order_relaxed);
|
2020-02-19 01:33:31 +00:00
|
|
|
if(!delegate) return;
|
|
|
|
|
2020-01-20 17:12:23 +00:00
|
|
|
++completed_sample_sets_;
|
2020-02-16 18:50:18 +00:00
|
|
|
|
|
|
|
// Hope for the fast path first: producer and consumer agree about
|
|
|
|
// number of channels.
|
|
|
|
if(is_stereo == stereo_output_) {
|
2020-02-19 01:33:31 +00:00
|
|
|
delegate->speaker_did_complete_samples(this, buffer);
|
2020-02-16 18:50:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Producer and consumer don't agree, so mix two channels to one, or double out one to two.
|
|
|
|
if(is_stereo) {
|
|
|
|
// Mix down.
|
|
|
|
mix_buffer_.resize(buffer.size() / 2);
|
|
|
|
for(size_t c = 0; c < mix_buffer_.size(); ++c) {
|
|
|
|
mix_buffer_[c] = (buffer[(c << 1) + 0] + buffer[(c << 1) + 1]) >> 1;
|
|
|
|
// TODO: is there an Accelerate framework solution to this?
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Double up.
|
|
|
|
mix_buffer_.resize(buffer.size() * 2);
|
|
|
|
for(size_t c = 0; c < buffer.size(); ++c) {
|
|
|
|
mix_buffer_[(c << 1) + 0] = mix_buffer_[(c << 1) + 1] = buffer[c];
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 01:33:31 +00:00
|
|
|
delegate->speaker_did_complete_samples(this, mix_buffer_);
|
2020-01-20 17:12:23 +00:00
|
|
|
}
|
2020-05-21 03:34:26 +00:00
|
|
|
std::atomic<Delegate *> delegate_{nullptr};
|
2020-01-26 18:25:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void compute_output_rate() {
|
|
|
|
// The input rate multiplier is actually used as an output rate divider,
|
|
|
|
// to confirm to the public interface of a generic speaker being output-centric.
|
2020-02-15 18:40:19 +00:00
|
|
|
set_computed_output_rate(output_cycles_per_second_ / input_rate_multiplier_, output_buffer_size_, stereo_output_);
|
2020-01-26 18:25:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 17:12:23 +00:00
|
|
|
int completed_sample_sets_ = 0;
|
2020-01-26 18:25:23 +00:00
|
|
|
float input_rate_multiplier_ = 1.0f;
|
|
|
|
float output_cycles_per_second_ = 1.0f;
|
|
|
|
int output_buffer_size_ = 1;
|
2020-05-21 03:34:26 +00:00
|
|
|
std::atomic<bool> stereo_output_{false};
|
2020-02-16 18:50:18 +00:00
|
|
|
std::vector<int16_t> mix_buffer_;
|
2017-12-18 02:26:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Speaker_hpp */
|