Exemplo n.º 1
0
 def decode_file(file_name):
     opened_data = SndFileLoaderBase.open_file(file_name)
     handle = opened_data["handle"]
     info = opened_data["info"]
     data = numpy.zeros(info.frames * info.channels, dtype=numpy.int16)
     libsndfile().sf_readf_short(handle,
                                 data.ctypes.data_as(POINTER(c_short)),
                                 info.frames)
     libsndfile().sf_close(handle)
     logging.info("Loaded " + file_name + ": " +
                  info.str_format() + ", " + str(info.frames) +
                  " samples at " + str(info.samplerate) + " Hz with " +
                  str(info.channels) + " channels")
     return {"data": data, "sampling_rate": info.samplerate,
             "samples": info.frames, "channels": info.channels,
             "name": file_name}
Exemplo n.º 2
0
    def open_file(file_name):
        info = SF_INFO()
        info.format = 0
        handle = libsndfile().sf_open(
            c_char_p(file_name.encode()), libsndfile.SFM_READ, byref(info))
        if not handle:
            raise error.BadFormatError(
                "Audio file %s does not exist or is in an unsupported format" %
                file_name)

        if info.channels > 2:
            raise error.BadFormatError("Audio file " + file_name +
                                       " has more than two channels. "
                                       "Only mono or stereo are allowed.")
        return {"handle": handle, "samples": info.frames,
                "sampling_rate": info.samplerate, "channels": info.channels,
                "info": info}
Exemplo n.º 3
0
 def close_file(opened_data):
     libsndfile().sf_close(opened_data["handle"])