Skip to content

Commit

Permalink
Minor audio handling APIs adjustments
Browse files Browse the repository at this point in the history
Summary:
This diff isolates minor changes to audio handling related APIs or how we use them, without changing functionality.
This diff reduces the size of upcoming diffs that contain actual functional changes that need closer attention.

Reviewed By: kiminoue7

Differential Revision: D53675816

fbshipit-source-id: ba1178b1bba510a9442c8517257dc4dcda04c713
  • Loading branch information
Georges Berenger authored and facebook-github-bot committed Feb 12, 2024
1 parent 3c4249e commit 93bed06
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sample_apps/SamplePlaybackApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class AudioStreamPlayer : public RecordFormatStreamPlayer {
// the audio data was not read yet. Use your own buffers.
utils::AudioBlock audio;
if (XR_VERIFY(audio.readBlock(record.reader, cb))) {
const auto* audioData = reinterpret_cast<const uint16_t*>(audio.rdata());
const auto* audioData = audio.data<uint16_t>();
// use audio data. In this sample code, we verify that the data matches the expected pattern
for (size_t k = 0; k < kAudioBlockSize; k++) {
XR_CHECK(audioData[k] == static_cast<int16_t>(audioBlockIndex * kAudioBlockSize + k));
Expand Down
6 changes: 2 additions & 4 deletions tools/vrsplayer/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include "AudioPlayer.h"

#include <vector>

#include <portaudio.h>

#define DEFAULT_LOG_CHANNEL "AudioPlayer"
Expand Down Expand Up @@ -74,7 +72,7 @@ bool AudioPlayer::onAudioRead(const CurrentRecord& record, size_t blkIdx, const
audio.asString());
} else if (
VideoTime::getPlaybackSpeed() <= 1 && sampleFormat_ == audio.getSampleFormat() &&
audio.getSampleCount() >= channelCount_) {
audio.getChannelCount() >= channelCount_ && audioBlock.getSampleCount() > 0) {
playbackQueue_.sendJob(std::move(audioBlock));
}
}
Expand Down Expand Up @@ -191,7 +189,7 @@ void AudioPlayer::playbackThread() {
Pa_WriteStream(paStream_, src, frameBatchSize);
} else {
// either we play fewer channels than provided, or frames are padded, we need to compact
uint8_t* dst = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(block.rdata()));
uint8_t* dst = block.data<uint8_t>();
uint32_t sample = 0;
while (dst + paFrameStride > src && sample < frameBatchSize) {
memmove(dst, src, paFrameStride); // more expensive, but safe in case of overlap
Expand Down
3 changes: 3 additions & 0 deletions vrs/RecordFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,9 @@ ContentBlock::ContentBlock(
ContentBlock::ContentBlock(AudioFormat audioFormat, uint8_t channelCount)
: contentType_{ContentType::AUDIO}, audioSpec_{audioFormat, channelCount} {}

ContentBlock::ContentBlock(const AudioContentBlockSpec& audioSpec, size_t size)
: contentType_{ContentType::AUDIO}, size_{size}, audioSpec_{audioSpec} {}

ContentBlock::ContentBlock(
AudioFormat audioFormat,
AudioSampleFormat sampleFormat,
Expand Down
5 changes: 5 additions & 0 deletions vrs/RecordFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ class AudioContentBlockSpec {
uint32_t getSampleCount() const {
return sampleFrameCount_;
}
/// Set the number of audio sample frames in the content block.
void setSampleCount(uint32_t sampleCount) {
sampleFrameCount_ = sampleCount;
}
/// Tell if the audio sample format is fully defined.
/// For instance, PCM audio data when we have enough details: sample format & channel count.
bool isSampleBlockFormatDefined() const {
Expand Down Expand Up @@ -520,6 +524,7 @@ class ContentBlock {
/// Very generic audio block description.
// NOLINTNEXTLINE(hicpp-explicit-conversions)
ContentBlock(AudioFormat audioFormat, uint8_t channelCount = 0);
ContentBlock(const AudioContentBlockSpec& audioSpec, size_t size);

/// Audio block description.
ContentBlock(
Expand Down
14 changes: 10 additions & 4 deletions vrs/utils/AudioBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include "AudioBlock.h"

#include <cstring>

#define DEFAULT_LOG_CHANNEL "AudioBlock"
#include <logging/Checks.h>
#include <logging/Log.h>
Expand Down Expand Up @@ -46,14 +44,22 @@ AudioBlock::AudioBlock(const AudioContentBlockSpec& spec, vector<uint8_t>&& fram
THROTTLED_VERIFY(nullptr, size == ContentBlock::kSizeUnknown || size == audioBytes_.size());
}

void AudioBlock::init(const AudioContentBlockSpec& spec) {
audioSpec_ = spec;
AudioBlock::AudioBlock(const AudioContentBlockSpec& spec) : audioSpec_{spec} {
allocateBytes();
}

void AudioBlock::allocateBytes() {
size_t size = audioSpec_.getBlockSize();
if (size != ContentBlock::kSizeUnknown) {
audioBytes_.resize(size);
}
}

void AudioBlock::init(const AudioContentBlockSpec& spec) {
audioSpec_ = spec;
allocateBytes();
}

void AudioBlock::init(const AudioContentBlockSpec& spec, vector<uint8_t>&& frameBytes) {
audioSpec_ = spec;
audioBytes_ = std::move(frameBytes);
Expand Down
15 changes: 14 additions & 1 deletion vrs/utils/AudioBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AudioBlock {
AudioBlock(AudioBlock&& other) noexcept = default;
AudioBlock(const AudioContentBlockSpec& spec, vector<uint8_t>&& frameBytes);
explicit AudioBlock(const AudioContentBlockSpec& spec);
explicit AudioBlock(
AudioBlock(
AudioFormat audioFormat,
AudioSampleFormat sampleFormat,
uint8_t channelCount = 0,
Expand Down Expand Up @@ -84,6 +84,9 @@ class AudioBlock {
uint32_t getSampleCount() const {
return audioSpec_.getSampleCount();
}
void setSampleCount(uint32_t sampleCount) {
audioSpec_.setSampleCount(sampleCount);
}

vector<uint8_t>& getBuffer() {
return audioBytes_;
Expand All @@ -94,6 +97,14 @@ class AudioBlock {
uint8_t* wdata() {
return audioBytes_.data();
}
template <class T>
const T* data(size_t byte_offset = 0) const {
return reinterpret_cast<const T*>(audioBytes_.data() + byte_offset);
}
template <class T>
T* data(size_t byte_offset = 0) {
return reinterpret_cast<T*>(audioBytes_.data() + byte_offset);
}
size_t size() const {
return audioBytes_.size();
}
Expand All @@ -109,6 +120,8 @@ class AudioBlock {
bool readBlock(RecordReader* reader, const ContentBlock& cb);

private:
void allocateBytes();

AudioContentBlockSpec audioSpec_;
vector<uint8_t> audioBytes_;
};
Expand Down
5 changes: 2 additions & 3 deletions vrs/utils/AudioExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int AudioExtractor::createWavFile(
format = 7;
}

uint32_t bytesPerSample = (audioBlock.getBitsPerSample() + 7) / 8;
uint32_t bytesPerSample = audioBlock.getBytesPerSample();
writeHeader(fileHeader.data() + 20, htole16(format)); // audio format, write in little endian
writeHeader(
fileHeader.data() + 22,
Expand Down Expand Up @@ -111,8 +111,7 @@ int AudioExtractor::writeWavAudioData(
const AudioContentBlockSpec& audioBlock,
const std::vector<uint8_t>& audio) {
uint32_t srcOffset = 0;
uint32_t bytesPerSampleBlock =
(audioBlock.getBitsPerSample() + 7) / 8 * audioBlock.getChannelCount();
uint32_t bytesPerSampleBlock = audioBlock.getBytesPerSample() * audioBlock.getChannelCount();
uint32_t srcStride = audioBlock.getSampleFrameStride();
uint32_t totalSamples = audioBlock.getSampleCount();
for (uint32_t i = 0; i < totalSamples; ++i) {
Expand Down

0 comments on commit 93bed06

Please sign in to comment.