Skip to content

Commit

Permalink
Seek() fixed some blank audio issues in sink, attempting the same for…
Browse files Browse the repository at this point in the history
… send
  • Loading branch information
Hrick87 committed Dec 9, 2023
1 parent f512752 commit cf8827c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ 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; //change back to 32 after testing
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();

Expand Down Expand Up @@ -240,31 +240,41 @@ bool SndfileSink::has_clock() const {

void SndfileSink::write(audio::Frame& frame) {
roc_panic_if(!valid_);

const audio::sample_t* frame_data = frame.samples();
size_t frame_size = frame.num_samples();


//roc_log(LogDebug, "Frame_size: %lu", frame_size);

audio::sample_t* buffer_data = buffer_.data();
size_t buffer_pos = 0;

while (frame_size > 0) {
for (; buffer_pos < buffer_size_ && frame_size > 0; buffer_pos++) {

buffer_data[buffer_pos] = *frame_data;
frame_data++;
frame_size--;
}

if (buffer_pos == buffer_size_) {

//roc_log(LogDebug, "sndfile sink: buffer_data and buffer_pos being passed to write_() are: %f, and %lu, also buffer_size_ is: %lu", (double)*buffer_data, buffer_pos, buffer_size_);

write_(buffer_data, buffer_pos);
buffer_pos = 0;
}
}



//roc_log(LogDebug, "sndfile sink: buffer_data and buffer_pos being passed to FINAL write_() are: %f, and %lu", (double)*buffer_data, buffer_pos);
write_(buffer_data, buffer_pos);

}

bool SndfileSink::setup_buffer_() {
buffer_size_ = sample_spec_.ns_2_samples_overall(frame_length_);
roc_log(LogInfo, "sndfile sink: buffer size is %lu", buffer_size_);
if (buffer_size_ == 0) {
roc_log(LogError, "sndfile sink: buffer size is zero");
return false;
Expand All @@ -291,6 +301,10 @@ bool SndfileSink::open_(const char* driver, const char* path) {
int bits = 0;
sf_info_out_.format = map_to_sndfile(sf_info_out_, driver, bits);

if(!sf_format_check(&sf_info_out_)){
roc_panic("sndfile sink: sf_format_check detected invalid format parameters");
}

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);
Expand All @@ -313,11 +327,12 @@ bool SndfileSink::open_(const char* driver, const char* path) {
(unsigned long)bits, out_rate, in_rate,
(unsigned long)sf_info_out_.channels, (int)is_file_);

sf_seek(sndfile_output_, 0, SEEK_SET);

return true;
}

void SndfileSink::write_(const audio::sample_t * samples, size_t n_samples) {

void SndfileSink::write_(const audio::sample_t* samples, size_t n_samples) {
if (n_samples > 0) {
if (sf_write_float(sndfile_output_, samples, (sf_count_t)n_samples) != (sf_count_t)n_samples) {
roc_log(LogError, "sndfile sink: failed to write output buffer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SndfileSink : public ISink, public core::NonCopyable<> {
private:
bool setup_buffer_();
bool open_(const char* driver, const char* path);
void write_(const audio::sample_t* samples, size_t n_samples);
void write_(const audio::sample_t * samples, size_t n_samples);
void close_();

SNDFILE * sndfile_output_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ bool SndfileSource::open_() {
(unsigned long)sample_rate_, (unsigned long)sf_info_in_.channels,
(unsigned long)0, (int)is_file_);

sf_seek(sndfile_input_, 0, SEEK_SET);

return true;
}

Expand Down

0 comments on commit cf8827c

Please sign in to comment.