python-soundfile: Soundfile cannot read the full FLAC file it generated

I am generating a soundfile by appending to audioFile = sf.SoundFile(conf['pSavAudio'], mode='w', samplerate=samplerate, channels=nChan, subtype='PCM_16', format='FLAC')

,but when I try to read all frames from it: arrA=audio.read(audio.frames, dtype='int16') I get an empty array, and the extra_info is changed to

“File : ‘C:\Users\hoffmmax\Documents\Recordings\20190319_1807_TestCongruency13\20190319_1807_TestCongruency13_Audio.flac’ (utf-8 converted from ucs-2)\nLength : 48373662\nFLAC Stream Metadata\n Channels : 5\n Sample rate : 192000\n Frames : 26000000\n Bit width : 16\nVorbis Comment Metadata\nEnd\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216 channels 5\nError: pflac->remain 16777216”

I understand that this is an error from the underlying library relating to the length not being divisble by the channels, but I only interact with the soundfile bei soundfile.Soundfile.write and MATLAB/Audacity don’t complain

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 19 (6 by maintainers)

Most upvoted comments

@MaximilianHoffmann Strangely, I ran into the same problem as you when I tried to read a 7-channel .flac file, but I managed to code a crude function that works around this issue (based on the advice given in libsndfile/libsndfile#431) by reading the file chunk by chunk, so if it helps, I’ve placed it in a repo at https://github.com/kenowr/read_flac and replicated the function below:

import numpy as np
import soundfile as sf
def read_flac(file, chunk = None, **kwargs):
    ## SET CHUNK SIZE
    x, sr = sf.read(file, start = 0, stop = 0, **kwargs)
    n_channels = x.shape[1] if len(x.shape) == 2 else 1
    if n_channels in [1,2,4,8]:
        return sf.read(file, **kwargs)
    elif chunk is None:
        chunk = (2**24)//n_channels

    ## READ CHUNK BY CHUNK
    parts = []
    n_frames = 0
    i = 0
    end_reached = False
    while not end_reached:
        x, sr = sf.read(file, start = i*chunk, stop = (i+1)*chunk, **kwargs)
        if x.shape[0] != 0:
            parts.append(x)
        if x.shape[0] < chunk: 
            end_reached = True
        n_frames += x.shape[0]
        i += 1

    ## GENERATE CORRECT OUTPUT ARRAY
    x = np.zeros((n_frames, n_channels))
    start = 0
    stop = len(parts[0])
    for part in parts:
        x[start:stop,:] = part
        start = stop
        stop = min(stop + chunk,n_frames)

    return x, sr