Skip to content

File Decoder does not properly create PCM_32 .wav files from 32-bit FLAC files #32

@nicktimpf

Description

@nicktimpf

In the FileDecoder class _write_callback method when sf.SoundFile object is created subtype is not specified. SoundFile's default subtype for the WAV format is PCM_16. This means for a 32-bit FLAC file, it is decoded properly but written as a 16-bit wave. Even though data is of type np.int32. I implemented the quick fix show below to get me by. I will leave the proper fix to someone more familiar with this project.

def _write_callback(self, data: np.ndarray, sample_rate: int, num_channels: int, num_samples: int):
    """
    Internal callback to write the decoded data to a WAV file.
    """
    if self.__output is None:
        #Need to add subtype argument to SoundFile.  If data is of type np.int32 it will be truncated by  
        #SoundFile's default subtype 'PCM_16' for the WAV format and the lower 16-bits of the sample are dropped.
        if data.dtype == np.int32:
            sub_type = 'PCM_32'
        else:
            sub_type = 'PCM_16'
        self.__output = sf.SoundFile(
            self.__output_file, mode='w', channels=num_channels,
            samplerate=sample_rate, subtype=sub_type
        )

    self.__output.write(data)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions