bbc / audiowaveform

C++ program to generate waveform data and render waveform images from audio files
https://waveform.prototyping.bbc.co.uk
GNU General Public License v3.0
1.94k stars 242 forks source link

Feature request: Ability to have much less samples #104

Open Patrykot opened 5 years ago

Patrykot commented 5 years ago

At this moment I'm not shure how to write tests for it. Feature will give you the the possibility to set maximum number of pixels on the output files. In order to achive it we need to take number of all frames (frame is block of samples, one for each channel) and devide it by desired number of pixels. We will receive then samples per pixel. But since samples per pixel is integer, higher values of desired number of pixels can get the SAME number of samples per pixel. Consider this example, based on test_file_mono.wav: It has 113519 of frames. Let's say i want to 376 samples.

samples_per_pixel1 = static_cast<int>(frames_count_ / pixels_count_ +
            ((frames_count_ % pixels_count_) > 0 ? 1 : 0)); // =302 double is 301.9122

Now, let's say i want to 377 samples.

samples_per_pixel2 = static_cast<int>(frames_count_ / pixels_count_ +
            ((frames_count_ % pixels_count_) > 0 ? 1 : 0)); // ALSO =302 !! double is 301.1114

Thus we are able to set precisly pixels count till the diferrence between these to samples_per_pixel1 - samples_per_pixel2 is greater than 1. The higher the pixels-count the more often you will encounter lower values than given the maximum.

This is kind of a remedy for issue: https://github.com/bbc/audiowaveform/issues/41

chrisn commented 5 years ago

Thanks! I'll take a look.

chrisn commented 5 years ago

But since samples per pixel is integer, higher values of desired number of pixels can get the SAME number of samples per pixel.

This is true, and means that it's not currently possible to achieve the exact length of waveform you specify, due to the rounding error. This issue affects the PNG output, where the image size is as specified on the command line, the actual waveform width may not exactly fill the image.