Skip to content
Browse files

o better skip mode detection. Only if someone _really_ jumps to the

  end of the file, we return some empty buffer. Otherwise, we
  diligently skip while decoding ... otherwise skipping back will
  be a hassle.
  • Loading branch information...
1 parent 453489f commit 47c2b03623d943b3a4156f83b50cec256f378558 @hzeller committed Sep 12, 2012
Showing with 18 additions and 22 deletions.
  1. +2 −8 conversion-buffer.cc
  2. +3 −2 conversion-buffer.h
  3. +13 −12 convolver.cc
View
10 conversion-buffer.cc
@@ -116,21 +116,15 @@ ssize_t ConversionBuffer::Read(char *buf, size_t size, off_t offset) {
const off_t required_min_written = offset + (offset >= header_end_ ? size : 1);
// Skipping the file looks like reading beyond what the user already
- // consumed. Right now, we have to fill the buffer up to that point, but
- // we might need to find a shortcut for that: some programs just skip to the
- // end of the file apparently - which makes us convolve the while file.
+ // consumed. Print this as diagnostic message.
if (total_written_ + 1 < offset) {
fprintf(stderr, "(skip> %ld -> %ld)", total_written_, offset);
}
// As soon as someone tries to read beyond of what we already have, we call
// our WriteToSoundfile() callback that fills more of it.
while (total_written_ < required_min_written) {
- // We skip up until 32k before the requested start-offset.
- // TODO(hzeller): remember that the skipped parts are actually not convolved
- // so if someone skips back we know that we need to re-do that.
- const bool skip_mode = total_written_ + (32 << 10) < offset;
- if (!source_->AddMoreSoundData(skip_mode))
+ if (!source_->AddMoreSoundData())
break;
}
View
5 conversion-buffer.h
@@ -33,12 +33,13 @@ class ConversionBuffer {
// Ask sf_strerror() to find out why.
// Ownership is passed to the SoundSource, receiver needs to
// sf_close() the file.
- virtual void SetOutputSoundfile(ConversionBuffer *parent, SNDFILE *sndfile) = 0;
+ virtual void SetOutputSoundfile(ConversionBuffer *parent,
+ SNDFILE *sndfile) = 0;
// This callback is called by the ConversionBuffer if it needs more data.
// Rerturns 'true' if there is more, 'false' if that was the last available
// data.
- virtual bool AddMoreSoundData(bool in_skip_mode) = 0;
+ virtual bool AddMoreSoundData() = 0;
};
// Create a conversion buffer that holds "buffer_size" bytes.
View
25 convolver.cc
@@ -118,6 +118,16 @@ class SndFileFilter :
virtual int Read(char *buf, size_t size, off_t offset) {
if (error_) return -1;
+ // If this is a skip suspiciously at the very end of the file as
+ // reported by stat, we don't do any encoding, just return garbage.
+ // Programs sometimes do this apparently.
+ // But of course only if this is really a detected skip.
+ if (output_buffer_->FileSize() < offset
+ && (int) (offset + size) == file_stat_.st_size) {
+ fprintf(stderr, "[Skip to the very end detected. Don't do filtering.]\n");
+ memset(buf, 0x00, size);
+ return size;
+ }
// The following read might block and call WriteToSoundfile() until the
// buffer is filled.
return output_buffer_->Read(buf, size, offset);
@@ -163,7 +173,7 @@ class SndFileFilter :
// the filesize as we see it grow. Some clients continuously monitor
// the size of the file to check when to stop.
fstat(filedes_, &file_stat_);
- start_estimating_size_ = 0.5 * file_stat_.st_size;
+ start_estimating_size_ = 0.4 * file_stat_.st_size;
// The flac header we get is more rich than what we can create via
// sndfile. So if we have one, just copy it.
@@ -229,7 +239,7 @@ class SndFileFilter :
out_buffer->HeaderFinished();
}
- virtual bool AddMoreSoundData(bool in_skip_mode) {
+ virtual bool AddMoreSoundData() {
if (!input_frames_left_)
return false;
if (!zita_.convproc) {
@@ -242,19 +252,10 @@ class SndFileFilter :
}
int r = sf_readf_float(snd_in_, raw_sample_buffer_, zita_.fragm);
if (r == (int) zita_.fragm) {
- fprintf(stderr, "%s", in_skip_mode ? "x" : ".");
+ fprintf(stderr, ".");
} else {
fprintf(stderr, "[%d]", r);
}
- if (in_skip_mode) {
- // If someone skips forward we don't do any encoding. That should
- // be faster, but skipping back will be dangerous: we haven't actually
- // encoded that part of the data. So we need to remember that this is
- // a no-go area. mmh.
- sf_writef_float(snd_out_, raw_sample_buffer_, r);
- input_frames_left_ -= r;
- return input_frames_left_;
- }
if (r < (int) zita_.fragm) {
// Zero out the rest of the buffer.

0 comments on commit 47c2b03

Please sign in to comment.
Something went wrong with that request. Please try again.