-1. sometimes flips to 1. when reading FLAC format
See original GitHub issueI noticed when converting WAV PCM_24 to FLAC PCM_24 (but same goes for PCM_16) that values -1. in the wave file sometimes are flipped to 1 when reading the FLAC file. This was in a long sound file, but I managed to reproduce the issue with the code and fragment of the sound pasted below. The issue seems to be when reading the file because when I open the flac in Audacity, it is OK, no sign flip. This is on windows, pysoundfile version 0.9.0, libsnd version 1.0.27.
import numpy as np
import soundfile as sf
print(sf.__version__)
print(sf.__libsndfile_version__)
ar = np.array([[ 0.99999976, 0.90140069],
[ 0.99999988, 0.9933517 ],
[ 0.99999988, 0.99999988],
[ 0.99999976, 0.99999976],
[ 0.99999988, 0.99999976],
[ 0.99999976, 0.99999988],
[ 0.99999976, 0.99999976],
[ 0.99999988, 0.99999988],
[ 0.99999976, 0.99999988],
[ 0.99999988, 0.99999988],
[ 0.99999988, 0.99999988],
[ 0.99999976, 0.99999988],
[ 0.93490779, 0.96048272],
[ 0.58039367, 0.87977421],
[ 0.43389213, 0.85307777],
[ 0.37074029, 0.88655353],
[ 0.07136083, 0.94096255],
[-0.50475669, 0.9776206 ],
[-0.99999988, 0.99565017],
[-1. , 0.99999988]], dtype='float64')
f = sf.write('test.wav', ar, samplerate=44100, format='WAV', subtype='PCM_16')
f = sf.write('test.flac', ar, samplerate=44100, format='FLAC', subtype='PCM_16')
print('wav', sf.read('test.wav')[0][-1])
print('flac', sf.read('test.flac')[0][-1]) # -1 flipped to 1!
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
flac files mysteriously changed to .tmp - MusicBee
Flac (vorbis comment) does not have a standardised format for rating. Some software uses a scale from 1 to 5, other a scale...
Read more >Tip: Upgrading your FLAC collection to FLAC 1.3.1 - Gough Lui
From my experimentation, it seems the new FLAC 1.3.1 files are completely backward compatible from a decoding point of view – and if...
Read more >Guide to lossless audio: Everything you need to know from a ...
If you were intrigued by Apple Music getting support for lossless audio, here's everything you need to know about it and how you...
Read more >Changelog - FLAC - Xiph.org
FLAC 1.4.1 (22-Sep-2022). This release only has a few changes. ... The FLAC format document is being rewritten by the IETF CELLAR working...
Read more >274621 – audacious and lossless formats - Red Hat Bugzilla
Description of problem: Can't play lossless formats, such as flac , wav Version-Release number of selected component (if applicable): audacious-1.3.2-1.fc7 ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In the meantime, for those who want to avoid being bitten by this bug: when reading FLAC data in SoundFile, don’t use the default dtype=‘float64’ (which causes the buggy normalization to [-1,1) in the underlying libsndfile). Read it with dtype=‘int32’, which avoids normalization and the bug.
Having said that, I was just trying to verify the above in practice practive by creating 16+ hours of FLAC data from WAV PCM_24, and when comparing them. I find indeed no numeric differences when using ‘int32’, whereas ‘float64’ does lead to normalization sign-flip errors. But I do get “RuntimeError: Internal psf_fseek() failed” in one of the FLAC files when reading somewhere in the middle… whereas trying to read before or after this spot is fine. I can play the file fine in VLC or open it in Audacity.
This (or a similar) problem has also been mentioned in the
libsndfile-devel
mailing list on Jun 1, 2020, 12:29 PM. I didn’t find a mailing list archive, so I’m pasting the text here:Here’s a link to the mentioned patch: https://github.com/roebel/conda_packages/blob/master/pysndfile/flac.c.patch