Skip to content

Commit

Permalink
Fixed empty sample_spec_ issue in SoX, fixed wav_source errors, added…
Browse files Browse the repository at this point in the history
… is_empty() in sample_spec, removed state checks in test
  • Loading branch information
Hrick87 committed Feb 28, 2024
1 parent e15ad04 commit 783d9c7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 84 deletions.
7 changes: 7 additions & 0 deletions src/internal_modules/roc_audio/sample_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "roc_core/macro_helpers.h"
#include "roc_core/panic.h"
#include "roc_packet/units.h"
#include "sample_spec.h"

namespace roc {
namespace audio {
Expand Down Expand Up @@ -136,6 +137,12 @@ bool SampleSpec::is_valid() const {
&& sample_rate_ != 0 && channel_set_.is_valid();
}

bool SampleSpec::is_empty() const {
return sample_fmt_ == SampleFormat_Invalid
&& pcm_fmt_ == PcmFormat_Invalid
&& sample_rate_ == 0 && channel_set_.num_channels() == 0;
}

bool SampleSpec::is_raw() const {
return sample_fmt_ == SampleFormat_Pcm && pcm_fmt_ == Sample_RawFormat;
}
Expand Down
2 changes: 2 additions & 0 deletions src/internal_modules/roc_audio/sample_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class SampleSpec {
//! Check if sample spec has non-zero rate and valid channel set.
bool is_valid() const;

bool is_empty() const;

//! Check if samples are in raw format.
//! @returns
//! true if sample_format() is SampleFormat_Pcm and pcm_format()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ SndfileSource::SndfileSource(core::IArena& arena, const Config& config)
return;
}

if (!config.sample_spec.is_empty()) {
roc_log(LogError, "sndfile source: setting io encoding not supported");
return;
}

frame_length_ = config.frame_length;
sample_spec_ = config.sample_spec;

Expand Down Expand Up @@ -231,13 +236,6 @@ bool SndfileSource::open_(const char* path) {
return false;
}

if (sample_rate_ != 0 && sample_rate_ != (size_t)file_info_.samplerate) {
roc_log(LogInfo,
"sndfile source: can't set rate: samplerate in argument is different "
"from file samplerate");
return false;
}

sample_spec_.set_sample_rate((unsigned long)file_info_.samplerate);

roc_log(LogInfo,
Expand Down
49 changes: 25 additions & 24 deletions src/internal_modules/roc_sndio/target_sox/roc_sndio/sox_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ SoxSource::SoxSource(core::IArena& arena, const Config& config)
}

frame_length_ = config.frame_length;

sample_spec_ = config.sample_spec;

if (config.sample_spec.num_channels() == 0) {
sample_spec_ =
audio::SampleSpec(44100, audio::Sample_RawFormat, audio::ChanLayout_Surround,
audio::ChanOrder_Smpte, 0x3);
} else {
sample_spec_ = config.sample_spec;
}

if (frame_length_ == 0) {
roc_log(LogError, "sox source: frame length is zero");
Expand Down Expand Up @@ -115,13 +110,13 @@ void SoxSource::pause() {
}

if (!input_) {
roc_panic("sox source: pause: non-open input file or device");
roc_panic("sox source: pause: non-open device");
}

roc_log(LogDebug, "sox source: pausing: driver=%s input=%s", driver_name_.c_str(),
input_name_.c_str());

if (!is_file_) {
if(!is_file_){
close_();
}

Expand Down Expand Up @@ -158,12 +153,17 @@ bool SoxSource::restart() {

if (is_file_ && !eof_) {
if (!seek_(0)) {
roc_panic("Reached");
roc_log(LogError,
"sox source: seek failed when restarting: driver=%s input=%s",
driver_name_.c_str(), input_name_.c_str());
return false;
}
} else {
if(is_file_){
sample_spec_.clear();
}

if (input_) {
close_();
}
Expand Down Expand Up @@ -360,23 +360,24 @@ bool SoxSource::open_() {
return false;
}

if (input_->signal.channels != sample_spec_.num_channels()) {
roc_log(LogError,
"sox source: can't open: unsupported # of channels: "
"expected=%lu actual=%lu",
(unsigned long)sample_spec_.num_channels(),
(unsigned long)input_->signal.channels);
return false;
}

is_file_ = !(input_->handler.flags & SOX_FILE_DEVICE);

if (is_file_ && sample_spec_.sample_rate() != input_->signal.rate
&& sample_spec_.sample_rate() != 0) {
roc_log(LogInfo,
"sndfile source: can't set rate: samplerate in argument is different "
"from file samplerate");
return false;
if (is_file_) {
if(!sample_spec_.is_empty()){
roc_log(LogError, "sox source: setting io encoding for files not supported");
return false;
}
sample_spec_ = sample_spec();
}
else{
if (input_->signal.channels != sample_spec_.num_channels()) {
roc_log(LogError,
"sox source: can't open: unsupported # of channels: "
"expected=%lu actual=%lu",
(unsigned long)sample_spec_.num_channels(),
(unsigned long)input_->signal.channels);
return false;
}
}

sample_spec_.set_sample_rate((unsigned long)input_->signal.rate);
Expand Down
5 changes: 3 additions & 2 deletions src/internal_modules/roc_sndio/wav_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ namespace roc {
namespace sndio {

WavSource::WavSource(core::IArena& arena, const Config& config)
: eof_(false)
: file_opened_(false)
, eof_(false)
, valid_(false) {
if (config.latency != 0) {
roc_log(LogError, "wav source: setting io latency not supported");
return;
}

if (config.sample_spec.is_valid()) {
if (!config.sample_spec.is_empty()) {
roc_log(LogError, "wav source: setting io encoding not supported");
return;
}
Expand Down
22 changes: 11 additions & 11 deletions src/tests/roc_sndio/test_backend_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ TEST_GROUP(backend_sink) {
* sink_config.sample_spec.num_channels());
}

bool supports_aiff(IBackend & backend) {
bool supports_wav(IBackend & backend) {
bool supports = false;
core::Array<DriverInfo, MaxDrivers> driver_list(arena);
backend.discover_drivers(driver_list);
for (size_t n = 0; n < driver_list.size(); n++) {
if (strcmp(driver_list[n].name, "aiff") == 0) {
if (strcmp(driver_list[n].name, "wav") == 0) {
supports = true;
break;
}
Expand All @@ -57,10 +57,10 @@ TEST(backend_sink, noop) {
for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends();
n_backend++) {
IBackend& backend = BackendMap::instance().nth_backend(n_backend);
if (!supports_aiff(backend)) {
if (!supports_wav(backend)) {
continue;
}
core::TempFile file("test.aiff");
core::TempFile file("test.wav");
IDevice* backend_device = backend.open_device(
DeviceType_Sink, DriverType_File, NULL, file.path(), sink_config, arena);
CHECK(backend_device != NULL);
Expand All @@ -73,7 +73,7 @@ TEST(backend_sink, error) {
for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends();
n_backend++) {
IBackend& backend = BackendMap::instance().nth_backend(n_backend);
if (!supports_aiff(backend)) {
if (!supports_wav(backend)) {
continue;
}
IDevice* backend_device = backend.open_device(
Expand All @@ -85,9 +85,9 @@ TEST(backend_sink, error) {
TEST(backend_sink, has_clock) {
for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends();
n_backend++) {
core::TempFile file("test.aiff");
core::TempFile file("test.wav");
IBackend& backend = BackendMap::instance().nth_backend(n_backend);
if (!supports_aiff(backend)) {
if (!supports_wav(backend)) {
continue;
}
IDevice* backend_device = backend.open_device(
Expand All @@ -103,9 +103,9 @@ TEST(backend_sink, sample_rate_auto) {
sink_config.sample_spec.set_sample_rate(0);
for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends();
n_backend++) {
core::TempFile file("test.aiff");
core::TempFile file("test.wav");
IBackend& backend = BackendMap::instance().nth_backend(n_backend);
if (!supports_aiff(backend)) {
if (!supports_wav(backend)) {
continue;
}
IDevice* backend_device = backend.open_device(
Expand All @@ -122,9 +122,9 @@ TEST(backend_sink, sample_rate_force) {
sink_config.sample_spec.set_sample_rate(SampleRate);
for (size_t n_backend = 0; n_backend < BackendMap::instance().num_backends();
n_backend++) {
core::TempFile file("test.aiff");
core::TempFile file("test.wav");
IBackend& backend = BackendMap::instance().nth_backend(n_backend);
if (!supports_aiff(backend)) {
if (!supports_wav(backend)) {
continue;
}
IDevice* backend_device = backend.open_device(
Expand Down
Loading

0 comments on commit 783d9c7

Please sign in to comment.