Skip to content

Commit

Permalink
Use std::byte instead of uint8_t/void
Browse files Browse the repository at this point in the history
  • Loading branch information
dechamps committed May 26, 2024
1 parent fa35647 commit 4618a1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
24 changes: 11 additions & 13 deletions src/flexasio/FlexASIO/flexasio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,18 @@ namespace flexasio {
return result;
}

void CopyFromPortAudioBuffers(const std::vector<ASIOBufferInfo>& bufferInfos, const long doubleBufferIndex, const void* const* portAudioBuffers, const size_t bufferSizeInBytes) {
void CopyFromPortAudioBuffers(const std::vector<ASIOBufferInfo>& bufferInfos, const long doubleBufferIndex, const std::byte* const* portAudioBuffers, const size_t bufferSizeInBytes) {
for (const auto& bufferInfo : bufferInfos)
{
if (!bufferInfo.isInput) continue;
void* asioBuffer = bufferInfo.buffers[doubleBufferIndex];
memcpy(asioBuffer, portAudioBuffers[bufferInfo.channelNum], bufferSizeInBytes);
memcpy(bufferInfo.buffers[doubleBufferIndex], portAudioBuffers[bufferInfo.channelNum], bufferSizeInBytes);
}
}
void CopyToPortAudioBuffers(const std::vector<ASIOBufferInfo>& bufferInfos, const long doubleBufferIndex, void* const* portAudioBuffers, const size_t bufferSizeInBytes) {
void CopyToPortAudioBuffers(const std::vector<ASIOBufferInfo>& bufferInfos, const long doubleBufferIndex, std::byte* const* portAudioBuffers, const size_t bufferSizeInBytes) {
for (const auto& bufferInfo : bufferInfos)
{
if (bufferInfo.isInput) continue;
void* asioBuffer = bufferInfo.buffers[doubleBufferIndex];
memcpy(portAudioBuffers[bufferInfo.channelNum], asioBuffer, bufferSizeInBytes);
memcpy(portAudioBuffers[bufferInfo.channelNum], bufferInfo.buffers[doubleBufferIndex], bufferSizeInBytes);
}
}

Expand Down Expand Up @@ -733,7 +731,7 @@ namespace flexasio {
<< inputChannelCount << "/" << outputChannelCount << " (I/O) channels per buffer set, "
<< bufferSizeInFrames << " samples per channel, "
<< inputSampleSizeInBytes << "/" << outputSampleSizeInBytes << " (I/O) bytes per sample, memory range: "
<< static_cast<const void*>(buffers.data()) << "-" << static_cast<const void*>(buffers.data() + buffers.size());
<< buffers.data() << "-" << buffers.data() + buffers.size();
}

FlexASIO::PreparedState::Buffers::~Buffers() {
Expand Down Expand Up @@ -769,14 +767,14 @@ namespace flexasio {
auto& nextBuffersChannelIndex = asioBufferInfo.isInput ? nextBuffersInputChannelIndex : nextBuffersOutputChannelIndex;
const auto bufferSizeInBytes = asioBufferInfo.isInput ? buffers.GetInputBufferSizeInBytes() : buffers.GetOutputBufferSizeInBytes();

uint8_t* first_half = (buffers.*getBuffer)(0, nextBuffersChannelIndex);
uint8_t* second_half = (buffers.*getBuffer)(1, nextBuffersChannelIndex);
std::byte* first_half = (buffers.*getBuffer)(0, nextBuffersChannelIndex);
std::byte* second_half = (buffers.*getBuffer)(1, nextBuffersChannelIndex);
++nextBuffersChannelIndex;
asioBufferInfo.buffers[0] = first_half;
asioBufferInfo.buffers[1] = second_half;
Log() << "ASIO buffer #" << channelIndex << " is " << (asioBufferInfo.isInput ? "input" : "output") << " channel " << asioBufferInfo.channelNum
<< " - first half: " << static_cast<const void*>(first_half) << "-" << static_cast<const void*>(first_half + bufferSizeInBytes)
<< " - second half: " << static_cast<const void*>(second_half) << "-" << static_cast<const void*>(second_half + bufferSizeInBytes);
<< " - first half: " << first_half << "-" << first_half + bufferSizeInBytes
<< " - second half: " << second_half << "-" << second_half + bufferSizeInBytes;
bufferInfos.push_back(asioBufferInfo);
}
return bufferInfos;
Expand Down Expand Up @@ -958,8 +956,8 @@ namespace flexasio {

const auto inputSampleSizeInBytes = preparedState.buffers.inputSampleSizeInBytes;
const auto outputSampleSizeInBytes = preparedState.buffers.outputSampleSizeInBytes;
const void* const* input_samples = static_cast<const void* const*>(input);
void* const* output_samples = static_cast<void* const*>(output);
const std::byte* const* input_samples = static_cast<const std::byte* const*> (input);
std::byte* const* output_samples = static_cast<std::byte* const*>(output);

if (output_samples) {
for (int output_channel_index = 0; output_channel_index < preparedState.flexASIO.GetOutputChannelCount(); ++output_channel_index)
Expand Down
6 changes: 3 additions & 3 deletions src/flexasio/FlexASIO/flexasio.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ namespace flexasio {
{
Buffers(size_t bufferSetCount, size_t inputChannelCount, size_t outputChannelCount, size_t bufferSizeInFrames, size_t inputSampleSizeInBytes, size_t outputSampleSizeInBytes);
~Buffers();
uint8_t* GetInputBuffer(size_t bufferSetIndex, size_t channelIndex) { return buffers.data() + bufferSetIndex * GetBufferSetSizeInBytes() + channelIndex * GetInputBufferSizeInBytes(); }
uint8_t* GetOutputBuffer(size_t bufferSetIndex, size_t channelIndex) { return GetInputBuffer(bufferSetIndex, inputChannelCount) + channelIndex * GetOutputBufferSizeInBytes(); }
std::byte* GetInputBuffer(size_t bufferSetIndex, size_t channelIndex) { return buffers.data() + bufferSetIndex * GetBufferSetSizeInBytes() + channelIndex * GetInputBufferSizeInBytes(); }
std::byte* GetOutputBuffer(size_t bufferSetIndex, size_t channelIndex) { return GetInputBuffer(bufferSetIndex, inputChannelCount) + channelIndex * GetOutputBufferSizeInBytes(); }
size_t GetBufferSetSizeInBytes() const { return buffers.size() / bufferSetCount; }
size_t GetInputBufferSizeInBytes() const { if (buffers.empty()) return 0; return bufferSizeInFrames * inputSampleSizeInBytes; }
size_t GetOutputBufferSizeInBytes() const { if (buffers.empty()) return 0; return bufferSizeInFrames * outputSampleSizeInBytes; }
Expand All @@ -122,7 +122,7 @@ namespace flexasio {
// [ input channel 0 buffer 0 ] [ input channel 1 buffer 0 ] ... [ input channel N buffer 0 ] [ output channel 0 buffer 0 ] [ output channel 1 buffer 0 ] .. [ output channel N buffer 0 ]
// [ input channel 0 buffer 1 ] [ input channel 1 buffer 1 ] ... [ input channel N buffer 1 ] [ output channel 0 buffer 1 ] [ output channel 1 buffer 1 ] .. [ output channel N buffer 1 ]
// The reason why this is a giant blob is to slightly improve performance by (theroretically) improving memory locality.
std::vector<uint8_t> buffers;
std::vector<std::byte> buffers;
};

class RunningState {
Expand Down

0 comments on commit 4618a1b

Please sign in to comment.