AudiooPy 0.1

Module audioopy

Class sppasAudioConverter

Description

An utility to convert data formats.

Constructor

Create a sppasAudioConverter instance.

def __init__(self):
    """Create a sppasAudioConverter instance."""
    pass

Public functions

unpack_data

Turn frames into samples.

Unpack the data frames depending on their sample width.

Parameters

  • frames: (str) Audio frames
  • samples_width: (int)
  • nchannels: (int) number of channels in the frames
@staticmethod
def unpack_data(frames, samples_width, nchannels=1):
    """Turn frames into samples.

        Unpack the data frames depending on their sample width.

        :param frames: (str) Audio frames
        :param samples_width: (int)
        :param nchannels: (int) number of channels in the frames

        """
    samples_width = int(samples_width)
    if samples_width == 4:
        data = list(struct.unpack('<%ul' % (len(frames) // 4), frames))
    elif samples_width == 2:
        data = list(struct.unpack('<%uh' % (len(frames) // 2), frames))
    elif samples_width == 1:
        tmp_data = struct.unpack('%uB' % len(frames), frames)
        data = [s - 128 for s in tmp_data]
    else:
        raise SampleWidthError(samples_width)
    samples = list()
    if nchannels > 1:
        for i in range(nchannels):
            samples.append([data[j] for j in range(i, len(data), nchannels)])
    else:
        samples.append(list(data))
    return samples
samples2frames

Turn samples into frames.

Parameters

  • samples: (int[][]) samples list, first index is the index of the channel, second is the index of the sample.
  • samples_width: (int) sample width of the frames.
  • nchannels: (int) number of channels in the samples

Returns

  • (str) frames
@staticmethod
def samples2frames(samples, samples_width, nchannels=1):
    """Turn samples into frames.

        :param samples: (int[][]) samples list,
        first index is the index of the channel, second is the index of the sample.
        :param samples_width: (int) sample width of the frames.
        :param nchannels: (int) number of channels in the samples
        :return: (str) frames

        """
    samples_width = int(samples_width)
    nchannels = int(nchannels)
    if nchannels < 1:
        raise ChannelIndexError(nchannels)
    nframes = len(samples[0])
    frames = b''
    if samples_width == 4:
        for i in range(nframes):
            for j in range(nchannels):
                frames += struct.pack('<l', samples[j][i])
        return frames
    if samples_width == 2:
        for i in range(nframes):
            for j in range(nchannels):
                frames += struct.pack('<h', samples[j][i])
        return frames
    if samples_width == 1:
        for i in range(nframes):
            for j in range(nchannels):
                frames += struct.pack('<b', samples[j][i])
        return frames
    raise SampleWidthError(samples_width)
hz2mel

Return the equivalent value in a mel scale, from a frequency value.

Mel is a unit of pitch proposed by Stevens, Volkmann and Newmann in

  1. The mel scale is a scale of pitches judged by listeners to be

equal in distance one from another.

The name mel comes from the word melody to indicate that the scale

is based on pitch comparisons.

Parameters

  • value: (int|float) the value to convert

Returns

  • (int) the value in mel
@staticmethod
def hz2mel(value):
    """Return the equivalent value in a mel scale, from a frequency value.

        Mel is a unit of pitch proposed by Stevens, Volkmann and Newmann in
        1937. The mel scale is a scale of pitches judged by listeners to be
        equal in distance one from another.
        The name mel comes from the word melody to indicate that the scale
        is based on pitch comparisons.

        :param value: (int|float) the value to convert
        :return: (int) the value in mel

        """
    return 2595 * math.log10(1.0 + float(value) / 700.0)
mel2hz

Return the equivalent value in frequency, from a mel value.

Parameters

  • value: (int) the value in mel to convert

Returns

  • (int) the value in dB
@staticmethod
def mel2hz(value):
    """Return the equivalent value in frequency, from a mel value.

        :param value: (int) the value in mel to convert
        :return: (int) the value in dB

        """
    return round(700 * (10 ** (float(value) / 2595) - 1), 2)
amp2db

Return the equivalent value in a dB scale, from an amplitude value.

Decibels express a power ratio, not an amount. They tell how many times

more (positive dB) or less (negative dB) but not how much in absolute terms.

Decibels are logarithmic, not linear.

Doubling of the value leads to an increase of 6.02dB.

Parameters

  • value: (int) the amplitude value to convert

Returns

  • (float) the value in dB
@staticmethod
def amp2db(value):
    """Return the equivalent value in a dB scale, from an amplitude value.

        Decibels express a power ratio, not an amount. They tell how many times
        more (positive dB) or less (negative dB) but not how much in absolute terms.
        Decibels are logarithmic, not linear.
        Doubling of the value leads to an increase of 6.02dB.

        :param value: (int) the amplitude value to convert
        :return: (float) the value in dB

        """
    if value < 3:
        return 0.0
    return round(20.0 * math.log10(value), 2)