Skip to content

Commit

Permalink
fix bugs in alsa handling
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir committed Jan 31, 2020
1 parent 5ad037b commit a488969
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/alsa_frames_transfer.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "alsa_frames_transfer.h"

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
Expand Down Expand Up @@ -63,7 +64,7 @@ namespace wavplayeralsa {
std::stringstream err_desc;
err_desc << "cannot query current offset in buffer (" << snd_strerror(err) << ")";
throw std::runtime_error(err_desc.str());
}
}

// this is a magic number test to remove end of file wrong reporting
if(delay < 4096) {
Expand Down Expand Up @@ -139,7 +140,7 @@ namespace wavplayeralsa {

// we want to deliver as many frames as possible.
// we can put frames_to_deliver number of frames, but the buffer can only hold frames_capacity_in_buffer_ frames
frames_to_deliver = frames_to_deliver > frames_capacity_in_buffer_ ? frames_capacity_in_buffer_ : frames_to_deliver;
frames_to_deliver = std::min(frames_to_deliver, frames_capacity_in_buffer_);
unsigned int bytes_to_deliver = frames_to_deliver * bytes_per_frame_;

// read the frames from the file. TODO: what if readRaw fails?
Expand All @@ -155,7 +156,6 @@ namespace wavplayeralsa {
return;
}


int frames_written = snd_pcm_writei(alsa_playback_handle_, buffer_for_transfer, frames_to_deliver);
if( frames_written < 0) {
err_desc << "snd_pcm_writei failed (" << snd_strerror(frames_written) << ")";
Expand All @@ -175,6 +175,9 @@ namespace wavplayeralsa {

void AlsaFramesTransfer::PcmDrainLoop(boost::system::error_code error_code, uint32_t play_seq_id) {

if(error_code)
return;

bool is_currently_playing = IsAlsaStatePlaying();

if(!is_currently_playing) {
Expand Down Expand Up @@ -210,14 +213,14 @@ namespace wavplayeralsa {
throw std::runtime_error("the player is not initialized with a valid sound file.");
}

Stop();

double position_in_seconds = (double)position_in_ms / 1000.0;
curr_position_frames_ = position_in_seconds * (double)frame_rate_;
if(curr_position_frames_ > total_frame_in_file_) {
curr_position_frames_ = total_frame_in_file_;
}
snd_file_.seek(curr_position_frames_, SEEK_SET);

Stop();
sf_count_t seek_res = snd_file_.seek(curr_position_frames_, SEEK_SET);

int err;
std::stringstream err_desc;
Expand Down Expand Up @@ -347,7 +350,7 @@ namespace wavplayeralsa {
int seconds_modulo = (number_of_ms / 1000) % 60;

bytes_per_frame_ = num_of_channels_ * bytes_per_sample_;
frames_capacity_in_buffer_ = TRANSFER_BUFFER_SIZE / bytes_per_frame_;
frames_capacity_in_buffer_ = (snd_pcm_sframes_t)(TRANSFER_BUFFER_SIZE / bytes_per_frame_);

logger_->info("finished reading audio file '{}'. "
"Frame rate: {} frames per seconds, "
Expand Down
2 changes: 1 addition & 1 deletion src/alsa_frames_transfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace wavplayeralsa {

// calculated
unsigned int bytes_per_frame_ = 1;
unsigned int frames_capacity_in_buffer_ = 0; // how many frames can be stored in a buffer with size TRANSFER_BUFFER_SIZE
snd_pcm_sframes_t frames_capacity_in_buffer_ = 0; // how many frames can be stored in a buffer with size TRANSFER_BUFFER_SIZE

private:
// alsa frames transfer stuff
Expand Down

0 comments on commit a488969

Please sign in to comment.