cellMic: make bit_resolution and channel_size constexpr

This allows us to optimize and inline variable_byteswap
This commit is contained in:
Megamouse 2024-08-07 23:18:32 +02:00
parent 667db0f2de
commit 161edbf2f6
2 changed files with 18 additions and 10 deletions

View File

@ -241,13 +241,20 @@ bool mic_context::check_device(u32 dev_num)
// Static functions
void microphone_device::variable_byteswap(const void* src, void* dst, const u32 bytesize)
template <u32 bytesize>
inline void microphone_device::variable_byteswap(const void* src, void* dst)
{
switch (bytesize)
if constexpr (bytesize == 4)
{
case 4: *static_cast<u32*>(dst) = *static_cast<const be_t<u32>*>(src); break;
case 2: *static_cast<u16*>(dst) = *static_cast<const be_t<u16>*>(src); break;
default: break;
*static_cast<u32*>(dst) = *static_cast<const be_t<u32>*>(src);
}
else if constexpr (bytesize == 2)
{
*static_cast<u16*>(dst) = *static_cast<const be_t<u16>*>(src);
}
else
{
fmt::throw_exception("variable_byteswap with bytesize %d unimplemented", bytesize);
}
}
@ -588,7 +595,7 @@ void microphone_device::get_data(const u32 num_samples)
case microphone_handler::standard:
case microphone_handler::rocksmith:
{
const u8 channel_size = bit_resolution / 8;
constexpr u8 channel_size = bit_resolution / 8;
const usz bufsize = num_samples * sample_size;
const std::vector<u8>& buf = ::at32(devices, 0).buf;
ensure(bufsize <= buf.size());
@ -602,7 +609,7 @@ void microphone_device::get_data(const u32 num_samples)
for (u32 indchan = 0; indchan < num_channels; indchan++)
{
const u32 curindex = sample_pos + indchan * channel_size;
microphone_device::variable_byteswap(buf.data() + curindex, tmp_ptr + curindex, channel_size);
microphone_device::variable_byteswap<channel_size>(buf.data() + curindex, tmp_ptr + curindex);
}
}
break;

View File

@ -294,7 +294,7 @@ public:
bool is_opened() const { return mic_opened; }
bool is_started() const { return mic_started; }
u8 get_signal_types() const { return signal_types; }
u8 get_bit_resolution() const { return bit_resolution; }
constexpr u8 get_bit_resolution() const { return bit_resolution; }
u32 get_raw_samplingrate() const { return raw_samplingrate; }
u8 get_num_channels() const { return num_channels; }
u8 get_datatype() const
@ -321,7 +321,8 @@ public:
u32 attr_dsptype = 0;
private:
static void variable_byteswap(const void* src, void* dst, const u32 bytesize);
template <u32 bytesize>
static inline void variable_byteswap(const void* src, void* dst);
u32 capture_audio();
@ -349,13 +350,13 @@ private:
u32 raw_samplingrate = 48000;
u32 dsp_samplingrate = 48000;
u32 aux_samplingrate = 48000;
u8 bit_resolution = 16;
u8 num_channels = 2;
u8 signal_types = CELLMIC_SIGTYPE_NULL;
u32 sample_size = 0; // Determined at opening for internal use
static constexpr u8 bit_resolution = 16;
static constexpr usz inbuf_size = 400000; // Default value unknown
simple_ringbuf<inbuf_size> rbuf_raw;