mackron / dr_libs

Audio decoding libraries for C/C++, each in a single source file.
Other
1.28k stars 205 forks source link

dr_wav: Distortion on output after seeking #264

Open bjorn-nesby opened 8 months ago

bjorn-nesby commented 8 months ago

It's possible for the dr_wav decoder to output distorted audio for certain files.

The problem is reproduced by downloading these test files, and compiling this program (a simple miniaudio player) to play them back.

The program will ask you to input a file, after which it will start playing the file. Once you press return, it will then seek forward half the total number of frames (you should hear nasty distorted audio as a result, so watch out!!).

I found that the distortion occurs only when the program has played back a bit of the audio, and then perform a seek in the forward direction. In order words, if the program was to start playback from halfway inside the file, the problem doesn't manifest itself. Also, the seek needs to be of a certain size (relative to the playback position) before the distortion will happen.

I've noticed that the problem affects both .aif and .wav files, but only seems to occur with 24-bit audio.

In case it's useful, I also captured 40-something bytes from the output buffer (see below), with GOOD being the correct output (captured immediately after a seek without playing audio beforehand), and BAD being the distorted output (captured immediately after a seek, but having played back some audio first).

  1. BAD is offset by 10 bytes (for better overview, I've aligned the values in the capture below). Assuming that each sample in the audio signal consist of 6-bytes (true for 24-bit audio), this could explain the distortion.
  2. Additionally, BAD contains a different value for every fourth byte.
BAD     GOOD
-----------------------

-       152
-       213
-       1
-       2
-       78
-       1
-       142
-       32
-       242
-       53
179     179
254     254
142     9       -
23      23
230     230
53      231     -
181     181
252     252
9       71      -
185     185
222     222
231     163     -
178     178
252     252
71      133     -
110     110
220     220
163     151     -
100     100
255     255
133     98      -
253     253
222     222    
151     240     -    
120     120    
4       4  
98      140     -   
179     179    
230     230    
240     45      -    
57      57   
11      11   
140     149     -
1       1  
244     244