| 1 |
/* |
| 2 |
* Copyright (C) 2018-2023 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 |
#pragma once |
| 27 |
|
| 28 |
#include "chip_defs.h" |
| 29 |
#include <cstdint> |
| 30 |
#include <memory> |
| 31 |
#include <mutex> |
| 32 |
|
| 33 |
namespace chip |
| 34 |
{ |
| 35 |
class AbstractResampler; |
| 36 |
class AbstractRegisterWriteLogger; |
| 37 |
|
| 38 |
class Chip |
| 39 |
{ |
| 40 |
public: |
| 41 |
// [rate] |
| 42 |
// 0 = auto-set mode (set internal chip rate) |
| 43 |
Chip(int id, int clock, int rate, int autoRate, size_t maxDuration, |
| 44 |
std::unique_ptr<AbstractResampler> resampler1, std::unique_ptr<AbstractResampler> resampler2, |
| 45 |
std::shared_ptr<AbstractRegisterWriteLogger> logger); |
| 46 |
virtual ~Chip(); |
| 47 |
|
| 48 |
void reset(); |
| 49 |
virtual void setRegister(uint32_t offset, uint8_t value) = 0; |
| 50 |
virtual uint8_t getRegister(uint32_t offset) const = 0; |
| 51 |
|
| 52 |
virtual void setRate(int rate); |
| 53 |
int getRate() const noexcept { return rate_; } |
| 54 |
|
| 55 |
int getClock() const noexcept { return clock_; } |
| 56 |
|
| 57 |
void setMaxDuration(size_t maxDuration); |
| 58 |
size_t getMaxDuration() const noexcept { return maxDuration_; } |
| 59 |
|
| 60 |
void setRegisterWriteLogger(std::shared_ptr<AbstractRegisterWriteLogger> logger = nullptr); |
| 61 |
|
| 62 |
void setMasterVolume(int percentage); |
| 63 |
|
| 64 |
virtual bool mix(int16_t* stream, size_t nSamples) = 0; |
| 65 |
|
| 66 |
protected: |
| 67 |
const int id_; |
| 68 |
std::mutex mutex_; |
| 69 |
|
| 70 |
int rate_, clock_; |
| 71 |
const int autoRate_; |
| 72 |
int internalRate_[2]; |
| 73 |
size_t maxDuration_; |
| 74 |
|
| 75 |
double masterVolumeRatio_; |
| 76 |
double busVolumeRatio_[2]; |
| 77 |
double volumeRatio_[2]; |
| 78 |
|
| 79 |
sample* buffer_[2][2]; |
| 80 |
std::unique_ptr<AbstractResampler> resampler_[2]; |
| 81 |
|
| 82 |
std::shared_ptr<AbstractRegisterWriteLogger> logger_; |
| 83 |
|
| 84 |
void initResampler(); |
| 85 |
|
| 86 |
virtual void resetSpecific() = 0; |
| 87 |
|
| 88 |
void funcSetRate(int rate) noexcept; |
| 89 |
|
| 90 |
void updateVolumeRatio(); |
| 91 |
void updateVolumeRatio(int i); |
| 92 |
}; |
| 93 |
} |