o
    wiR                     @   s:   d dl Zd dlZ		dddZdd Zdd	 Zd
d ZdS )    NTc                 C   sx   t | j}|dkr|dk std|t| tj} t| |}dd |D }|g| }t| |}t	|t
s:td||S )a  
  Compresses a NumPy array lossily

  Args:
    input:   A numpy.ndarray.  May be of type np.float or np.double.
    tick_power:  Determines the accuracy; the input will be compressed to integer
             multiples of 2^tick_power.
    do_regression:  If true, use regression on previous elements in the array
             (one regression coefficient per axis) to reduce the magnitudes of
             the values to compress.
  r      z.Expected number of axes to be in [1,15], got: c                 S   s   g | ]}t |d  qS )   )round).0x r   T/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/lilcom/lilcom_interface.py
<listcomp>    s    zcompress.<locals>.<listcomp>z6Something went wrong in compression, return value was )lenshape
ValueErrornpascontiguousarrayfloat32regress_arraylilcom_extensioncompress_float
isinstancebytesRuntimeError)input
tick_powerdo_regressionn_dimcoeffs
int_coeffsmetaansr   r   r	   compress   s   



r   c                 C   s  |   } dgt| j }|s|S t| d| dd }tt| jD ]\}| j| dkr/q%t| d|} | dd | dd   | dd d  d  }t	|dk rYd}n|d	k r`d	}n|d
krfd
}|||< | dd  | dd | 8  < t| d|} q%|S )a  
  Works out coefficients for linear regression on the previous sample, for
  each axis of the array.

     @param [in] input   The array to be compressed; must contain floats or doubles.
     @param [in] regression  True if we are doing regression; if false,
                         coefficients will be all zero.

  Returns a list of size len(input.shape), with either zero or regression
  coefficients in the range [-1..1] for each corresponding axis.  Each axis's
  regression coefficient will be zero if regression == False, or the input's
  size on that axis was 1, or of the estimated regression coefficient had
  absolute value less than 0.2.
  g        g#B;   r   N   g{Gz?g      g      ?)
copyr   r   r   dotreshaperangeswapaxessumabs)r   
regressionr   	tot_sumsqaxiscoeffr   r   r	   r   ,   s(   4 r   c                 C   st   t | tstdt| t| }|du rtdtj	|tj
d}t| |}|du s1|dkr8td||S )z
   Decompresses audio data compressed by compress().

   Args:
       input:    A bytes object as returned by compress()
   Return:
       On success returns a NumPy array of float; on failure
       raises an exception.
     z,Expected input to be of type `bytes`, got {}NLCould not work out shape of array from input: is not really compressed data?)dtyper   zUSomething went wrong in decompression (likely bad data): decompress_float returned {})r   r   	TypeErrorformattyper   get_float_matrix_shaper   r   emptyr   decompress_float)byte_stringr   r   retr   r   r	   
decompressV   s   


r8   c                 C   s   t | }|du rtd|S )a#  
  Decompresses audio data compressed by compress() just enough to get the
  shape it would be when decompressed.
   Args:
       input:    A bytes object as returned by compress()
   Return:
       On success returns a tuple representing the shape; on failure
       raises an exception.
  Nr.   )r   r3   r   )r6   r   r   r   r	   	get_shapeu   s   
r9   )r   T)numpyr   r   r   r   r8   r9   r   r   r   r	   <module>   s    
%*