|
| 1 | + |
| 2 | +#include "LabSoundTemplateNode.h" |
| 3 | +#include <LabSound/core/AudioBus.h> |
| 4 | +#include <LabSound/core/AudioContext.h> |
| 5 | +#include <LabSound/core/AudioNodeOutput.h> |
| 6 | +#include <LabSound/extended/AudioContextLock.h> |
| 7 | +#include <LabSound/extended/Registry.h> |
| 8 | + |
| 9 | +#include "concurrentqueue.h" |
| 10 | +#include <algorithm> |
| 11 | +#include <queue> |
| 12 | + |
| 13 | +using namespace lab; |
| 14 | + |
| 15 | +struct LabSoundTemplateNodeEvent |
| 16 | +{ |
| 17 | + double when; |
| 18 | + int id; |
| 19 | + |
| 20 | + bool operator<(const LabSoundTemplateNodeEvent& rhs) const |
| 21 | + { |
| 22 | + if (when > rhs.when) |
| 23 | + return true; |
| 24 | + if (when < rhs.when) |
| 25 | + return false; |
| 26 | + return id > rhs.id; |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +struct LabSoundTemplateNode::Detail |
| 33 | +{ |
| 34 | + std::priority_queue<LabSoundTemplateNodeEvent> queue; |
| 35 | + moodycamel::ConcurrentQueue<LabSoundTemplateNodeEvent> incoming; |
| 36 | + lab::AudioContext* ac = nullptr; |
| 37 | + |
| 38 | + Detail() = default; |
| 39 | + ~Detail() = default; |
| 40 | + |
| 41 | + void clearSchedules() |
| 42 | + { |
| 43 | + LabSoundTemplateNodeEvent s; |
| 44 | + while (incoming.try_dequeue(s)) {} |
| 45 | + while (!queue.empty()) |
| 46 | + queue.pop(); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +LabSoundTemplateNode::LabSoundTemplateNode(AudioContext& ac) |
| 51 | +: AudioNode(ac) |
| 52 | +, _detail(new Detail()) |
| 53 | +{ |
| 54 | + _detail->ac = ∾ |
| 55 | + addOutput(std::unique_ptr<AudioNodeOutput>(new AudioNodeOutput(this, 1))); |
| 56 | + |
| 57 | + if (s_registered) |
| 58 | + initialize(); |
| 59 | +} |
| 60 | + |
| 61 | +bool LabSoundTemplateNode::s_registered = NodeRegistry::Register(LabSoundTemplateNode::static_name(), |
| 62 | + [](AudioContext& ac)->AudioNode* { return new LabSoundTemplateNode(ac); }, |
| 63 | + [](AudioNode* n) { delete n; }); |
| 64 | + |
| 65 | +LabSoundTemplateNode::~LabSoundTemplateNode() |
| 66 | +{ |
| 67 | + _detail->clearSchedules(); |
| 68 | + uninitialize(); |
| 69 | + delete _detail; |
| 70 | +} |
| 71 | + |
| 72 | +void LabSoundTemplateNode::realtimeEvent(float when, int identifier) |
| 73 | +{ |
| 74 | + double now = _detail->ac->currentTime(); |
| 75 | + _detail->incoming.enqueue({when + now, identifier}); |
| 76 | +} |
| 77 | + |
| 78 | +void LabSoundTemplateNode::process(ContextRenderLock &r, int bufferSize) |
| 79 | +{ |
| 80 | + AudioBus * outputBus = output(0)->bus(r); |
| 81 | + |
| 82 | + if (!isInitialized()) |
| 83 | + { |
| 84 | + if (outputBus) |
| 85 | + outputBus->zero(); |
| 86 | + |
| 87 | + _detail->clearSchedules(); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // move incoming commands to the internal schedule |
| 92 | + { |
| 93 | + LabSoundTemplateNodeEvent s; |
| 94 | + while (_detail->incoming.try_dequeue(s)) |
| 95 | + { |
| 96 | + _detail->queue.push(s); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + auto& ac = *r.context(); |
| 101 | + double quantumStart = ac.currentTime(); |
| 102 | + double quantumEnd = quantumStart + (double) bufferSize / ac.sampleRate(); |
| 103 | + |
| 104 | + // any events to service now? |
| 105 | + |
| 106 | + int events = 0; |
| 107 | + outputBus->zero(); |
| 108 | + while (!_detail->queue.empty() && _detail->queue.top().when < quantumEnd) |
| 109 | + { |
| 110 | + auto& top = _detail->queue.top(); |
| 111 | + |
| 112 | + // compute the exact sample the event occurs at |
| 113 | + int offset = (top.when < quantumStart) ? 0 : static_cast<int>((top.when - quantumStart) * ac.sampleRate()); |
| 114 | + |
| 115 | + // sanity to guard against rounding problems |
| 116 | + if (offset > bufferSize - 1) |
| 117 | + offset = bufferSize - 1; |
| 118 | + |
| 119 | + // make a little popping sound |
| 120 | + float* data = outputBus->channel(0)->mutableData(); |
| 121 | + for (int i = std::max(0, offset - 4); i < std::min(offset + 4, bufferSize); ++i) |
| 122 | + data[i] = 1; |
| 123 | + |
| 124 | + _detail->queue.pop(); |
| 125 | + ++events; |
| 126 | + } |
| 127 | + |
| 128 | + if (events > 0) |
| 129 | + outputBus->clearSilentFlag(); |
| 130 | +} |
| 131 | + |
| 132 | +void LabSoundTemplateNode::reset(ContextRenderLock&) |
| 133 | +{ |
| 134 | + _detail->clearSchedules(); |
| 135 | +} |
0 commit comments