Skip to content

Commit

Permalink
Backend dispatcher now chooses sndfile and recv no longer segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrick87 committed Dec 5, 2023
1 parent 5a3caa6 commit d5bb9df
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 73 deletions.
8 changes: 4 additions & 4 deletions src/internal_modules/roc_sndio/backend_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ void BackendMap::register_backends_() {
pulseaudio_backend_.reset(new (pulseaudio_backend_) PulseaudioBackend);
add_backend_(pulseaudio_backend_.get());
#endif // ROC_TARGET_PULSEAUDIO
#ifdef ROC_TARGET_SOX
sox_backend_.reset(new (sox_backend_) SoxBackend);
add_backend_(sox_backend_.get());
#endif // ROC_TARGET_SOX
#ifdef ROC_TARGET_SNDFILE
sndfile_backend_.reset(new (sndfile_backend_) SndfileBackend);
add_backend_(sndfile_backend_.get());
#endif // ROC_TARGET_SNDFILE
#ifdef ROC_TARGET_SOX
sox_backend_.reset(new (sox_backend_) SoxBackend);
add_backend_(sox_backend_.get());
#endif // ROC_TARGET_SOX
}

void BackendMap::register_drivers_() {
Expand Down
16 changes: 8 additions & 8 deletions src/internal_modules/roc_sndio/backend_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#include "roc_sndio/pulseaudio_backend.h"
#endif // ROC_TARGET_PULSEAUDIO

#ifdef ROC_TARGET_SOX
#include "roc_sndio/sox_backend.h"
#endif // ROC_TARGET_SOX

#ifdef ROC_TARGET_SNDFILE
#include "roc_sndio/sndfile_backend.h"
#endif // ROC_TARGET_SNDFILE

#ifdef ROC_TARGET_SOX
#include "roc_sndio/sox_backend.h"
#endif // ROC_TARGET_SOX

namespace roc {
namespace sndio {

Expand Down Expand Up @@ -71,14 +71,14 @@ class BackendMap : public core::NonCopyable<> {
core::Optional<PulseaudioBackend> pulseaudio_backend_;
#endif // ROC_TARGET_PULSEAUDIO

#ifdef ROC_TARGET_SOX
core::Optional<SoxBackend> sox_backend_;
#endif // ROC_TARGET_SOX

#ifdef ROC_TARGET_SNDFILE
core::Optional<SndfileBackend> sndfile_backend_;
#endif // ROC_TARGET_SOX

#ifdef ROC_TARGET_SOX
core::Optional<SoxBackend> sox_backend_;
#endif // ROC_TARGET_SOX

core::Array<IBackend*, MaxBackends> backends_;
core::Array<DriverInfo, MaxDrivers> drivers_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void SndfileBackend::discover_drivers(core::Array<DriverInfo, MaxDrivers>& drive
}

const char* driver = format_info.extension;
printf("Extension: %s\nName: %s\n", driver, format_info.name);

if (!driver_list.push_back(DriverInfo(driver, DriverType_File,
DriverFlag_IsDefault
| DriverFlag_SupportsSource
Expand Down Expand Up @@ -105,6 +105,7 @@ IDevice * SndfileBackend::open_device(DeviceType device_type,
}
}

//possibly redundant?
if(!check_file_extension_support(driver)){
roc_panic("sndfile backend: file format is not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,97 @@

namespace roc {
namespace sndio {
namespace {

struct {
const char * format_cstring;
int format_enum;
} file_type_map[]={
{"aiff", SF_FORMAT_AIFF},
{"au", SF_FORMAT_AU},
{"avr", SF_FORMAT_AVR},
{"caf", SF_FORMAT_CAF},
{"htk", SF_FORMAT_HTK},
{"iff", SF_FORMAT_SVX},
{"mat", SF_FORMAT_MAT4},
{"mat", SF_FORMAT_MAT5},
{"mpc", SF_FORMAT_MPC2K},
{"paf", SF_FORMAT_PAF},
{"pvf", SF_FORMAT_PVF},
{"raw", SF_FORMAT_RAW},
{"rf64", SF_FORMAT_RF64},
{"sd2", SF_FORMAT_SD2},
{"sds", SF_FORMAT_SDS},
{"sf", SF_FORMAT_IRCAM},
{"voc", SF_FORMAT_VOC},
{"w64", SF_FORMAT_W64},
{"wav", SF_FORMAT_WAV},
{"wav", SF_FORMAT_NIST},
{"wav", SF_FORMAT_WAVEX},
{"wve", SF_FORMAT_WVE},
{"xi", SF_FORMAT_XI},
{"ogg", SF_FORMAT_OGG},
{"flac", SF_FORMAT_FLAC},
};

int map_to_sndfile(SF_INFO sfinfo, const char* driver, int &bits){
int format = 0;
for(size_t format_struct_index = 0; format_struct_index < sizeof(file_type_map) / sizeof(file_type_map[0]); format_struct_index++){
if(strcmp(file_type_map[format_struct_index].format_cstring, driver) == 0){
format = file_type_map[format_struct_index].format_enum;
break;
}
}

if(format == 0){
roc_panic("sndfile sink: Cannot map driver name to sndfile format");
}

sfinfo.format = format | sfinfo.format;

if(sf_format_check(&sfinfo))
{
bits = 32;
return sfinfo.format;
}

int temp_format = 0;
const int format_count = 2;

for(int format_attempt = 0; format_attempt < format_count; format_attempt++){
if(format == SF_FORMAT_XI){
sfinfo.channels = 1;
if(format_attempt == 0){
temp_format = format | SF_FORMAT_DPCM_16;
bits = 16;
}
else{
temp_format = format | SF_FORMAT_DPCM_8;
bits = 8;
}
}
else{
if(format_attempt == 0){
temp_format = format | SF_FORMAT_PCM_24;
bits = 24;
}
else {
temp_format = format | SF_FORMAT_PCM_16;
bits = 16;
}
}

sfinfo.format = temp_format;

if(sf_format_check(&sfinfo)){
return sfinfo.format;
}
}

roc_panic("sndfile sink: Cannot find valid subtype format for major format type");
}

}

SndfileSink::SndfileSink(core::IArena& arena, const Config& config)
: sndfile_output_(NULL)
Expand Down Expand Up @@ -43,7 +134,11 @@ SndfileSink::SndfileSink(core::IArena& arena, const Config& config)
}

memset(&sf_info_out_, 0, sizeof(sf_info_out_));


sf_info_out_.format = SF_FORMAT_PCM_32;
sf_info_out_.channels = (int)config.sample_spec.num_channels();
sf_info_out_.samplerate = (int)config.sample_spec.sample_rate();

valid_ = true;
}

Expand Down Expand Up @@ -186,24 +281,38 @@ bool SndfileSink::setup_buffer_() {

bool SndfileSink::open_(const char* driver, const char* path) {

//TODO: Implement sf_info_out_ data retrieval to pass this check
if(!sf_format_check(&sf_info_out_))
unsigned long in_rate = (unsigned long)sf_info_out_.samplerate;

if(sf_info_out_.samplerate == 0)
{
roc_panic("sndfile sink: Invalid encoding");
sf_info_out_.samplerate = 48000;
}

unsigned long out_rate = (unsigned long)sf_info_out_.samplerate;

int bits = 0;
sf_info_out_.format = map_to_sndfile(sf_info_out_, driver, bits);

sndfile_output_ = sf_open(path, SFM_WRITE, &sf_info_out_);
if (!sndfile_output_) {
roc_log(LogDebug, "sndfile sink: can't open: driver=%s path=%s", driver, path);
return false;
}

if (in_rate != 0 && in_rate != out_rate) {
roc_log(LogError,
"sox sink:"
" can't open output file or device with the required sample rate:"
" required_by_output=%lu requested_by_user=%lu",
out_rate, in_rate);
return false;
}
sample_spec_.set_sample_rate((unsigned long)sf_info_out_.samplerate);

roc_log(LogInfo,
"sndfile sink:"
" opened: samplerate=%lu ch=%lu is_file=%d",
(unsigned long)sf_info_out_.samplerate,
" opened: bits=%lu out_rate=%lu in_rate=%lu ch=%lu is_file=%d",
(unsigned long)bits, out_rate, in_rate,
(unsigned long)sf_info_out_.channels, (int)is_file_);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace roc {
namespace sndio {

//! SoX sink.
//! Sndfile sink.
//! @remarks
//! Writes samples to output file or device.
//! Supports multiple drivers for different file types and audio systems.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,6 @@

namespace roc {
namespace sndio {
namespace {

struct {
const char * format_cstring;
int format_enum;
} file_type_map[]={
{"aiff", SF_FORMAT_AIFF},
{"au", SF_FORMAT_AU},
{"avr", SF_FORMAT_AVR},
{"caf", SF_FORMAT_CAF},
{"htk", SF_FORMAT_HTK},
{"iff", SF_FORMAT_SVX},
{"mat", SF_FORMAT_MAT4},
{"mat", SF_FORMAT_MAT5},
{"mpc", SF_FORMAT_MPC2K},
{"paf", SF_FORMAT_PAF},
{"pvf", SF_FORMAT_PVF},
{"raw", SF_FORMAT_RAW},
{"rf64", SF_FORMAT_RF64},
{"sd2", SF_FORMAT_SD2},
{"sds", SF_FORMAT_SDS},
{"sf", SF_FORMAT_IRCAM},
{"voc", SF_FORMAT_VOC},
{"w64", SF_FORMAT_W64},
{"wav", SF_FORMAT_WAV},
{"wav", SF_FORMAT_NIST},
{"wav", SF_FORMAT_WAVEX},
{"wve", SF_FORMAT_WVE},
{"svx", SF_FORMAT_SVX},
{"sv", SF_FORMAT_SVX},
{"xi", SF_FORMAT_XI},

{"ogg", SF_FORMAT_OGG},
{"flac", SF_FORMAT_FLAC},
};
}

SndfileSource::SndfileSource(core::IArena& arena, const Config& config)
: driver_name_(arena)
Expand Down Expand Up @@ -81,7 +45,9 @@ SndfileSource::SndfileSource(core::IArena& arena, const Config& config)
}

memset(&sf_info_in_, 0, sizeof(sf_info_in_));

sample_rate_ = (int)config.sample_spec.sample_rate();
precision_ = 32;

valid_ = true;
}

Expand Down Expand Up @@ -364,21 +330,13 @@ bool SndfileSource::setup_buffer_() {
return true;
}

int SndfileSource::map_to_sndfile_(){
for(size_t format_struct_index = 0; format_struct_index < sizeof(file_type_map) / sizeof(file_type_map[0]); format_struct_index++){
if(strcmp(file_type_map[format_struct_index].format_cstring, driver_name_.c_str()) == 0){
return file_type_map[format_struct_index].format_enum;
}
}

roc_panic("sndfile source: Cannot map driver name to sndfile format");
}

bool SndfileSource::open_() {
if (sndfile_input_) {
roc_panic("sndfile source: already opened");
}

sf_info_in_.format = 0;

sndfile_input_ =
sf_open(input_name_.is_empty() ? NULL : input_name_.c_str(), SFM_READ, &sf_info_in_);
if (!sndfile_input_) {
Expand All @@ -396,17 +354,21 @@ bool SndfileSource::open_() {
return false;
}

sf_info_in_.format = map_to_sndfile_();
if(sample_rate_ != 0)
{
sf_info_in_.samplerate = sample_rate_;
}

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

roc_log(LogInfo,
"sndfile source:"
" format=%d in_rate=%lu "
" in_ch=%lu is_file=%d",
sf_info_in_.format,
(unsigned long)sf_info_in_.samplerate,
(unsigned long)sf_info_in_.channels, (int)is_file_);
" in_bits=%lu out_bits=%lu in_rate=%lu out_rate=%lu"
" in_ch=%lu out_ch=%lu is_file=%d",
(unsigned long)precision_,
(unsigned long)0, (unsigned long)sf_info_in_.samplerate,
(unsigned long)sample_rate_, (unsigned long)sf_info_in_.channels,
(unsigned long)0, (int)is_file_);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class SndfileSource : public ISource, private core::NonCopyable<> {

bool seek_(size_t offset);

int get_out_bits_();

core::StringBuffer driver_name_;
core::StringBuffer input_name_;

Expand All @@ -105,6 +107,8 @@ class SndfileSource : public ISource, private core::NonCopyable<> {
SNDFILE * sndfile_input_;
SF_INFO sf_info_in_;

int sample_rate_;
int precision_;
bool is_file_;
bool eof_;
bool paused_;
Expand Down

0 comments on commit d5bb9df

Please sign in to comment.