Debian Codegraph @

BambooTracker/chip/chip.cpp

raw
1 /*
2 * Copyright (C) 2018-2022 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "chip.hpp"
27 #include <utility>
28 #include "resampler.hpp"
29 #include "register_write_logger.hpp"
30
31 namespace chip
32 {
33 Chip::Chip(int id, int clock, int rate, int autoRate, size_t maxDuration,
34 std::unique_ptr<AbstractResampler> resampler1, std::unique_ptr<AbstractResampler> resampler2,
35 std::shared_ptr<AbstractRegisterWriteLogger> logger)
36 : id_(id),
37 rate_(rate), // Dummy set
38 clock_(clock),
39 autoRate_(autoRate),
40 maxDuration_(maxDuration),
41 masterVolumeRatio_(100),
42 logger_(logger)
43 {
44 resampler_[0] = std::move(resampler1);
45 resampler_[1] = std::move(resampler2);
46
47 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
48 for (auto& buf : buffer_) {
49 buf[pan] = new sample[CHIP_SMPL_BUF_SIZE_];
50 }
51 }
52 }
53
54 Chip::~Chip()
55 {
56 for (int pan = STEREO_LEFT; pan <= STEREO_RIGHT; ++pan) {
57 for (auto& buf : buffer_) {
58 delete[] buf[pan];
59 }
60 }
61 }
62
63 void Chip::initResampler()
64 {
65 for (int snd = 0; snd < 2; ++snd) {
66 resampler_[snd]->init(internalRate_[snd], rate_, maxDuration_);
67 }
68 }
69
70 void Chip::reset()
71 {
72 std::lock_guard<std::mutex> lg(mutex_);
73
74 for (int snd = 0; snd < 2; ++snd) {
75 resampler_[snd]->reset();
76 }
77
78 resetSpecific();
79 }
80
81 void Chip::setRate(int rate)
82 {
83 std::lock_guard<std::mutex> lg(mutex_);
84
85 funcSetRate(rate);
86
87 for (auto& rsmp : resampler_) {
88 rsmp->setDestributionRate(rate);
89 }
90 }
91
92 void Chip::funcSetRate(int rate) noexcept
93 {
94 rate_ = rate ? rate : autoRate_;
95 }
96
97 void Chip::updateVolumeRatio()
98 {
99 for (int i = 0; i < 2; ++i) {
100 updateVolumeRatio(i);
101 }
102 }
103
104 void Chip::updateVolumeRatio(int i)
105 {
106 volumeRatio_[i] = busVolumeRatio_[i] * masterVolumeRatio_;
107 }
108
109 void Chip::setMaxDuration(size_t maxDuration)
110 {
111 maxDuration_ = maxDuration;
112 for (int snd = 0; snd < 2; ++snd) {
113 resampler_[snd]->setMaxDuration(maxDuration);
114 }
115 }
116
117 void Chip::setRegisterWriteLogger(std::shared_ptr<AbstractRegisterWriteLogger> logger)
118 {
119 logger_ = logger;
120 }
121
122 void Chip::setMasterVolume(int percentage)
123 {
124 masterVolumeRatio_ = percentage / 100.0;
125 updateVolumeRatio();
126 }
127 }