question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Soundfile cannot read the full FLAC file it generated

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
kenowrcommented, Nov 27, 2020

@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
0reactions
MaximilianHoffmanncommented, Nov 27, 2020

A real bad workaround I found is to re-encode the file with sox…

Read more comments on GitHub >

github_iconTop Results From Across the Web

SoundFile - Read the Docs
SoundFile can open all file formats that libsndfile supports, for example WAV, FLAC, OGG and MAT files (see Known Issues below about writing...
Read more >
Why does librosa load change the flac file? - Stack Overflow
I have an original file 0.flac which I just open with librosa and then save with SoundFile as 1.flac : import soundfile as...
Read more >
How to Open, Edit, and Convert FLAC Files - Lifewire
This article explains what a FLAC file is, plus how to open one and how to convert one to a different format like...
Read more >
Flac audio bug - VEGAS Community
Not EVERY flac file will corrupt, but most will IF YOU APPLY A TOOL/PROCESS/FX to it (which is probably why you're using Sound...
Read more >
Flac filetype issue in Windows 10 and Groove Music
However, since Windows 10 started supporting .flac audio files, ... This is complete nonsense and I have no clue how this hasn't been...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found