Skip to content

Commit

Permalink
gh-608 Initial implementation of sample spec parser
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Nov 29, 2023
1 parent 3c4e86c commit 8a2c333
Show file tree
Hide file tree
Showing 14 changed files with 648 additions and 56 deletions.
20 changes: 0 additions & 20 deletions src/internal_modules/roc_audio/channel_defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ const char* channel_pos_to_str(ChannelPosition pos) {
return NULL;
}

ChannelPosition channel_pos_from_str(const char* name) {
for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanPositionNames); i++) {
if (strcmp(ChanPositionNames[i].name, name) == 0) {
return ChanPositionNames[i].pos;
}
}

return ChanPos_Max;
}

const char* channel_mask_to_str(ChannelMask mask) {
for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanMaskNames); i++) {
if (ChanMaskNames[i].mask == mask) {
Expand All @@ -76,15 +66,5 @@ const char* channel_mask_to_str(ChannelMask mask) {
return NULL;
}

ChannelMask channel_mask_from_str(const char* name) {
for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanMaskNames); i++) {
if (strcmp(ChanMaskNames[i].name, name) == 0) {
return ChanMaskNames[i].mask;
}
}

return 0;
}

} // namespace audio
} // namespace roc
6 changes: 0 additions & 6 deletions src/internal_modules/roc_audio/channel_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,9 @@ const char* channel_order_to_str(ChannelOrder);
//! Get string name from channel position.
const char* channel_pos_to_str(ChannelPosition);

//! Get channel position from string name.
ChannelPosition channel_pos_from_str(const char*);

//! Get string name from channel mask.
const char* channel_mask_to_str(ChannelMask);

//! Get channel mask from string name.
ChannelMask channel_mask_from_str(const char*);

} // namespace audio
} // namespace roc

Expand Down
8 changes: 8 additions & 0 deletions src/internal_modules/roc_audio/channel_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ size_t ChannelSet::last_channel() const {
return last_chan_;
}

bool ChannelSet::is_equal(ChannelMask mask) const {
if (last_chan_ >= WordBits) {
return false;
}

return (words_[0] == mask);
}

bool ChannelSet::is_subset(ChannelMask mask) const {
if (last_chan_ >= WordBits) {
return false;
Expand Down
6 changes: 6 additions & 0 deletions src/internal_modules/roc_audio/channel_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class ChannelSet {
//! Panics if there are no enabled channels.
size_t last_channel() const;

//! Check if channel set is equal to given mask.
//! @remarks
//! The mask defines only first 32 channels. If any channels outside of 0-31
//! range are enabled in channel set, the method will fail.
bool is_equal(ChannelMask mask) const;

//! Check if channel set is sub-set of given mask, or equal to it.
//! @remarks
//! The mask defines only first 32 channels. If any channels outside of 0-31
Expand Down
11 changes: 5 additions & 6 deletions src/internal_modules/roc_audio/channel_set_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,26 @@ void format_channel_set(const ChannelSet& ch_set, core::StringBuilder& bld) {
} else {
bld.append_str(" ch=0x");

// Find last non-zero byte.
size_t last_byte = 0;

for (size_t n = 0; n < ch_set.num_bytes(); n++) {
if (ch_set.byte_at(n) != 0) {
last_byte = n;
}
}

// Format bitmask from LSB to MSB in hex.
for (size_t n = 0; n <= last_byte; n++) {
size_t n = last_byte;
do {
const uint8_t byte = ch_set.byte_at(n);

const uint8_t lo = (byte & 0xf);
const uint8_t hi = ((byte >> 4) & 0xf);

bld.append_uint(lo, 16);

if (hi != 0 || n != last_byte) {
bld.append_uint(hi, 16);
}
}
bld.append_uint(lo, 16);
} while (n-- != 0);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/internal_modules/roc_audio/sample_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ bool SampleSpec::is_valid() const {
return sample_rate_ != 0 && channel_set_.is_valid();
}

void SampleSpec::clear() {
sample_rate_ = 0;
channel_set_.clear();
}

size_t SampleSpec::sample_rate() const {
return sample_rate_;
}
Expand Down
10 changes: 10 additions & 0 deletions src/internal_modules/roc_audio/sample_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "roc_audio/channel_set.h"
#include "roc_audio/sample.h"
#include "roc_core/stddefs.h"
#include "roc_core/string_builder.h"
#include "roc_core/time.h"
#include "roc_packet/units.h"

Expand Down Expand Up @@ -57,6 +58,9 @@ class SampleSpec {
//! Check if sample spec has non-zero rate and valid channel set.
bool is_valid() const;

//! Unset all fields.
void clear();

//! Get sample rate.
//! @remarks
//! Defines sample frequency (number of samples per second).
Expand Down Expand Up @@ -148,6 +152,12 @@ class SampleSpec {
ChannelSet channel_set_;
};

//! Parse sample spec from string.
bool parse_sample_spec(const char* str, SampleSpec& result);

//! Format sample spec to string.
void format_sample_spec(const SampleSpec& sample_spec, core::StringBuilder& bld);

} // namespace audio
} // namespace roc

Expand Down
23 changes: 23 additions & 0 deletions src/internal_modules/roc_audio/sample_spec_format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "roc_audio/sample_spec.h"

namespace roc {
namespace audio {

void format_sample_spec(const SampleSpec& sample_spec, core::StringBuilder& bld) {
bld.append_str("<sspec rate=");
bld.append_uint(sample_spec.sample_rate(), 10);
bld.append_str(" chset=");
format_channel_set(sample_spec.channel_set(), bld);
bld.append_str(">");
}

} // namespace audio
} // namespace roc
Loading

0 comments on commit 8a2c333

Please sign in to comment.