�
�f0H���dZddlmZddlZejed���edd��ZdZd Zd
Z dZ
dZd
ZdZ
dZdZdZdZdZdZdZee e
eeegZGd�de��Zd�Zd�ZGd�d��ZGd�d��Zdd�ZdS) a�Stuff to parse Sun and NeXT audio files.
An audio file consists of a header followed by the data. The structure
of the header is as follows.
+---------------+
| magic word |
+---------------+
| header size |
+---------------+
| data size |
+---------------+
| encoding |
+---------------+
| sample rate |
+---------------+
| # of channels |
+---------------+
| info |
| |
+---------------+
The magic word consists of the 4 characters '.snd'. Apart from the
info field, all header fields are 4 bytes in size. They are all
32-bit unsigned integers encoded in big-endian byte order.
The header size really gives the start of the data.
The data size is the physical size of the data. From the other
parameters the number of frames can be calculated.
The encoding gives the way in which audio samples are encoded.
Possible values are listed below.
The info field currently consists of an ASCII string giving a
human-readable description of the audio file. The info field is
padded with NUL bytes to the header size.
Usage.
Reading audio files:
f = sunau.open(file, 'r')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods read(), seek(), and close().
When the setpos() and rewind() methods are not used, the seek()
method is not necessary.
This returns an instance of a class with the following public methods:
getnchannels() -- returns number of audio channels (1 for
mono, 2 for stereo)
getsampwidth() -- returns sample width in bytes
getframerate() -- returns sampling frequency
getnframes() -- returns number of audio frames
getcomptype() -- returns compression type ('NONE' or 'ULAW')
getcompname() -- returns human-readable version of
compression type ('not compressed' matches 'NONE')
getparams() -- returns a namedtuple consisting of all of the
above in the above order
getmarkers() -- returns None (for compatibility with the
aifc module)
getmark(id) -- raises an error since the mark does not
exist (for compatibility with the aifc module)
readframes(n) -- returns at most n frames of audio
rewind() -- rewind to the beginning of the audio stream
setpos(pos) -- seek to the specified position
tell() -- return the current position
close() -- close the instance (make it unusable)
The position returned by tell() and the position given to setpos()
are compatible and have nothing to do with the actual position in the
file.
The close() method is called automatically when the class instance
is destroyed.
Writing audio files:
f = sunau.open(file, 'w')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods write(), tell(), seek(), and
close().
This returns an instance of a class with the following public methods:
setnchannels(n) -- set the number of channels
setsampwidth(n) -- set the sample width
setframerate(n) -- set the frame rate
setnframes(n) -- set the number of frames
setcomptype(type, name)
-- set the compression type and the
human-readable compression type
setparams(tuple)-- set all parameters at once
tell() -- return current position in output file
writeframesraw(data)
-- write audio frames without pathing up the
file header
writeframes(data)
-- write audio frames and patch up the file header
close() -- patch up the file header and close the
output file
You should set the parameters before the first writeframesraw or
writeframes. The total number of frames does not need to be set,
but when it is set to the correct value, the header does not have to
be patched up.
It is best to first set all parameters, perhaps possibly the
compression type, and then write audio frames using writeframesraw.
When all frames have been written, either call writeframes(b'') or
close() to patch up the sizes in the header.
The close() method is called automatically when the class instance
is destroyed.
�)�
namedtupleN)��
)�remove�
_sunau_paramsz7nchannels sampwidth framerate nframes comptype compnameidns.��r���������l��c��eZdZdS)�ErrorN)�__name__�
__module__�__qualname__���,/opt/alt/python311/lib64/python3.11/sunau.pyrr�s�������Drrc��d}td��D]5}|�d��}|st�|dzt|��z}�6|S)Nrr
r�)�range�read�EOFError�ord)�file�x�i�bytes r� _read_u32r%�sS�� �A�
�1�X�X�����y�y��|�|��� ��N�
�c�E�C��I�I�����Hrc���g}td��D]:}t|d��\}}|�dt|����|}�;|�t|����dS)Nr
rr)r�divmod�insert�int�write�bytes)r!r"�datar#�d�ms r�
_write_u32r/�sl��
�D�
�1�X�X�����a��~�~���1����A�s�1�v�v����
����J�J�u�T�{�{�����rc��eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z
d �Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�Au_readc���t|��td��kr"ddl}|�|d��}d|_nd|_|�|��dS)N�r�rbTF��type�builtins�open�_opened�initfp��self�fr7s r�__init__zAu_read.__init__��X����7�7�d�2�h�h����O�O�O��
�
�a��&�&�A��D�L�L� �D�L����A�����rc�@�|jr|���dSdS�N��_file�close�r<s r�__del__zAu_read.__del__�s'���:� ��J�J�L�L�L�L�L� � rc��|SrArrEs r� __enter__zAu_read.__enter__�����rc�.�|���dSrA�rD�r<�argss r�__exit__zAu_read.__exit__�����
�
�����rc��||_d|_tt|����}|tkrtd���tt|����|_|jdkrtd���|jdkrtd���t|��|_|jtkrt|j��|_tt|����|_ |j tvrtd���|j ttfvrd|_
d |_n�|j tkrd x|_|_
nl|j t krdx|_|_
nM|j t"krd
x|_|_
n.|j t$krdx|_|_
ntd���tt|����|_tt|����|_|jstd
���|j|jz|_|jdkrF|�|jdz
��|_|j�d��\|_}}nd|_ |���|_dS#t4t6f$rd|_YdSwxYw)Nrzbad magic numberrzheader size too small�dzheader size ridiculously largezencoding not (yet) supportedr rrr
zunknown encodingzbad # of channels�r)rC� _soundposr)r%�AUDIO_FILE_MAGICr� _hdr_size�
_data_size�AUDIO_UNKNOWN_SIZE� _encoding�_simple_encodings�AUDIO_FILE_ENCODING_MULAW_8�AUDIO_FILE_ENCODING_ALAW_8�
_sampwidth�
_framesize�AUDIO_FILE_ENCODING_LINEAR_8�AUDIO_FILE_ENCODING_LINEAR_16�AUDIO_FILE_ENCODING_LINEAR_24�AUDIO_FILE_ENCODING_LINEAR_32�
_framerate�
_nchannelsr�_info� partition�tell� _data_pos�AttributeError�OSError)r<r!�magic�_s rr:zAu_read.initfp�s�����
�����I�d�O�O�$�$���$�$�$��*�+�+�+��Y�t�_�_�-�-����>�B����/�0�0�0��>�C����8�9�9�9�#�D�/�/����?�0�0�0�!�$�/�2�2�D�O��Y�t�_�_�-�-����>�!2�2�2��6�7�7�7��>�9�,�.�.�.��D�O��D�O�O�
�^�;�
;�
;�01�1�D�O�d�o�o�
�^�<�
<�
<�01�1�D�O�d�o�o�
�^�<�
<�
<�01�1�D�O�d�o�o�
�^�<�
<�
<�01�1�D�O�d�o�o��*�+�+�+��i��o�o�.�.����i��o�o�.�.����� -��+�,�,�,��/�D�O�;����>�B������4�>�B�#6�7�7�D�J�#�z�3�3�E�:�:��D�J��1�1��D�J� "�!�Y�Y�[�[�D�N�N�N����(� "� "� "�!�D�N�N�N�N� "���s� J$�$K�?Kc��|jSrA)rCrEs r�getfpz
Au_read.getfp�s
���z�rc��|jSrA)rcrEs r�getnchannelszAu_read.getnchannels��
����rc��|jSrA)r\rEs r�getsampwidthzAu_read.getsampwidth�rprc��|jSrA)rbrEs r�getframeratezAu_read.getframerate�rprc�n�|jtkrtS|jtvr|j|jzSdS)Nr)rVrWrXrYr]rEs r�
getnframeszAu_read.getnframes�s8���?�0�0�0�%�%��>�.�.�.��?�d�o�5�5��qrc�N�|jtkrdS|jtkrdSdS)N�ULAW�ALAW�NONE�rXrZr[rEs r�getcomptypezAu_read.getcomptype�s-���>�8�8�8��6�
�^�9�
9�
9��6��6rc�N�|jtkrdS|jtkrdSdS)N�CCITT G.711 u-law�CCITT G.711 A-law�not compressedr{rEs r�getcompnamezAu_read.getcompname�s0���>�8�8�8�&�&�
�^�9�
9�
9�&�&�#�#rc ��t|���|���|���|���|���|�����SrA�rrorrrtrvr|r�rEs r� getparamszAu_read.getparams�h���T�.�.�0�0�$�2C�2C�2E�2E��#�#�%�%�t���'8�'8��"�"�$�$�d�&6�&6�&8�&8�:�:� :rc��dSrArrEs r�
getmarkerszAu_read.getmarkerss���trc� �td���)Nzno marks)r)r<�ids r�getmarkzAu_read.getmarks���J���rc���|jtvr�|tkr|j���}n"|j�||jz��}|xjt|��|jzz
c_|jtkretj
��5tjdt���ddl
}ddd��n#1swxYwY|�||j��}|SdS)N�ignore��categoryr)rXrYrWrCrr]rS�lenrZ�warnings�catch_warnings�simplefilter�DeprecationWarning�audioop�ulaw2linr\)r<�nframesr,r�s r�
readframeszAu_read.readframess���>�.�.�.��,�,�,��z���(�(����z���w���'@�A�A���N�N�c�$�i�i�4�?�:�:�N�N��~�!<�<�<��,�.�.�#�#��)�(�=O�P�P�P�P�"�N�N�N�#�#�#�#�#�#�#�#�#�#�#����#�#�#�#��'�'��d�o�>�>���K��ts� C
�
C�Cc�~�|j�td���|j�|j��d|_dS)N�cannot seekr)rgrirC�seekrSrEs r�rewindzAu_read.rewind s9���>�!��-�(�(�(��
�����'�'�'�����rc��|jSrA)rSrEs rrfzAu_read.tell&�
���~�rc���|dks||���krtd���|j�td���|j�|j||jzz��||_dS)Nrzposition not in ranger�)rvrrgrirCr�r]rS)r<�poss r�setposzAu_read.setpos)sq����7�7�c�D�O�O�-�-�-�-��/�0�0�0��>�!��-�(�(�(��
������t��)>�>�?�?�?�����rc�d�|j}|r$d|_|jr|���dSdSdSrA)rCr9rD�r<r!s rrDz
Au_read.close1sG���z��� ��D�J��|�
��
�
������ � �
�
rN)rrrr>rFrHrNr:rmrorrrtrvr|r�r�r�r�r�r�rfr�rDrrrr1r1�sA������������������,"�,"�,"�\������������������$�$�$�:�:�:�
��� � � �
�
�
��������������rr1c��eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z
d �Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�Au_writec���t|��td��kr"ddl}|�|d��}d|_nd|_|�|��dS)Nr3r�wbTFr5r;s rr>zAu_write.__init__:r?rc�J�|jr|���d|_dSrArBrEs rrFzAu_write.__del__Cs$���:� ��J�J�L�L�L���
�
�
rc��|SrArrEs rrHzAu_write.__enter__HrIrc�.�|���dSrArKrLs rrNzAu_write.__exit__KrOrc��||_d|_d|_d|_d|_t
|_d|_d|_d|_ d|_
d|_dS)Nrrrx)rCrbrcr\r]rW�_nframes�_nframeswritten�_datawritten�_datalengthrd� _comptyper�s rr:zAu_write.initfpNsV����
�������������*��
� �����������
�����rc�f�|jrtd���|dvrtd���||_dS)N�0cannot change parameters after starting to write)rr r
z"only 1, 2, or 4 channels supported)r�rrc)r<� nchannelss r�setnchannelszAu_write.setnchannels[sB���� L��J�K�K�K��I�%�%��<�=�=�=�#����rc�<�|jstd���|jS)Nznumber of channels not set)rcrrEs rrozAu_write.getnchannelsb�#���� 6��4�5�5�5���rc�f�|jrtd���|dvrtd���||_dS)Nr�)rr rr
zbad sample width)r�rr\)r<� sampwidths r�setsampwidthzAu_write.setsampwidthgsB���� L��J�K�K�K��L�(�(��*�+�+�+�#����rc�<�|jstd���|jS)N�sample width not specified)rbrr\rEs rrrzAu_write.getsampwidthnr�rc�@�|jrtd���||_dS)Nr�)r�rrb)r<� framerates r�setframeratezAu_write.setframeratess(���� L��J�K�K�K�#����rc�<�|jstd���|jS)Nzframe rate not set)rbrrEs rrtzAu_write.getframeratexs#���� .��,�-�-�-���rc�j�|jrtd���|dkrtd���||_dS)Nr�rz# of frames cannot be negative)r�rr�)r<r�s r�
setnframeszAu_write.setnframes}s@���� L��J�K�K�K��Q�;�;��8�9�9�9���
�
�
rc��|jSrA�r�rEs rrvzAu_write.getnframes�����#�#rc�:�|dvr ||_dStd���)N)rzrxzunknown compression type)r�r)r<r6�names r�setcomptypezAu_write.setcomptype�s(���#�#�#�!�D�N�N�N��2�3�3�3rc��|jSrA�r�rEs rr|zAu_write.getcomptype�r�rc�:�|jdkrdS|jdkrdSdS)Nrxr~ryrr�r�rEs rr�zAu_write.getcompname�s.���>�V�#�#�&�&�
�^�v�
%�
%�&�&�#�#rc���|\}}}}}}|�|��|�|��|�|��|�|��|�||��dSrA)r�r�r�r�r�)r<�paramsr�r�r�r��comptype�compnames r� setparamszAu_write.setparams�s~��GM�D� �9�i��(�H����)�$�$�$����)�$�$�$����)�$�$�$����� � � �����8�,�,�,�,�,rc ��t|���|���|���|���|���|�����SrAr�rEs rr�zAu_write.getparams�r�rc��|jSrAr�rEs rrfz
Au_write.tell�r�rc�B�t|ttf��s"t|���d��}|���|jdkretj��5tj dt���ddl}ddd��n#1swxYwY|�||j
��}t|��|jz}|j�|��|j|z|_|jt|��z|_dS)N�Brxr�r�r)�
isinstancer+� bytearray�
memoryview�cast�_ensure_header_writtenr�r�r�r�r�r��lin2ulawr\r�r]rCr*r�r�)r<r,r�r�s r�writeframesrawzAu_write.writeframesraw�sD���$�� � 2�3�3� .��d�#�#�(�(��-�-�D��#�#�%�%�%��>�V�#�#��(�*�*�
�
��%�h�9K�L�L�L�L�����
�
�
�
�
�
�
�
�
�
�
����
�
�
�
��#�#�D�$�/�:�:�D��d�)�)�t��.���
�������#�3�g�=��� �-��D� � �9����s�1 B�B!�$B!c��|�|��|j|jks|j|jkr|���dSdSrA)r�r�r�r�r��_patchheader)r<r,s r�writeframeszAu_write.writeframes�sW�����D�!�!�!���4�=�0�0��"�d�&7�7�7���������8�7rc��|jr� |���|j|jks|j|jkr|���|j���|j}d|_|jr|� ��dSdS#|j}d|_|jr|� ��wwxYwdSrA)
rCr�r�r�r�r�r��flushr9rDr�s rrDzAu_write.close�s����:� !�
!��+�+�-�-�-��'�4�=�8�8��(�D�,=�=�=��%�%�'�'�'��
� � �"�"�"��z��!��
��<�!��J�J�L�L�L�L�L�!�!���z��!��
��<�!��J�J�L�L�L�L�!���� !� !s�A!B�,Cc���|jsX|jstd���|jstd���|jstd���|���dSdS)Nz# of channels not specifiedr�zframe rate not specified)r�rcrr\rb�
_write_headerrEs rr�zAu_write._ensure_header_written�sz���#� !��?�
;��9�:�:�:��?�
:��8�9�9�9��?�
8��6�7�7�7���� � � � � � !� !rc�\�|jdkrw|jdkrt}d|_n�|jdkrt}d|_nl|jdkrt
}d|_nR|jdkrt}d|_n8td���|jdkrt}d|_ntd���|j|j z|_t|jt��dt|j��z}|d zd
z}t|j|��|jt krt }n|j|jz} |j���|_n#t&t(f$r
d|_YnwxYwt|j|��||_t|j|��t|j|j��t|j|j ��|j�|j��|j�d|t|j��z
dz
z��dS)
Nrzrr rr
zinternal errorrxrr
i����rRr)r�r\r^r]r_r`rarrZrcr/rCrTr�rdr�rWrf�_form_length_posrhrir�rbr*)r<�encoding�header_size�lengths rr�zAu_write._write_header�s���>�V�#�#���!�#�#�7��"#������A�%�%�8��"#������A�%�%�8��"#������A�%�%�8��"#�����,�-�-�-�
�^�v�
%�
%�2�H��D�O�O��(�)�)�)��/�D�O�;����4�:�/�0�0�0��3�t�z�?�?�*��"�Q��"�,���4�:�{�+�+�+��=�.�.�.�'�F�F��]�T�_�4�F� )�$(�J�O�O�$5�$5�D�!�!����(� )� )� )�$(�D�!�!�!� )�����4�:�v�&�&�&�!����4�:�x�(�(�(��4�:�t��/�/�/��4�:�t��/�/�/��
�����$�$�$��
�����c�$�*�o�o� =�� B�C�D�D�D�D�Ds�6E�E0�/E0c���|j�td���|j�|j��t |j|j��|j|_|j�dd��dS)Nr�rr )r�rirCr�r/r�r�rEs rr�zAu_write._patchheadersl��� �(��-�(�(�(��
����-�.�.�.��4�:�t�0�1�1�1��,����
����1�����rN)rrrr>rFrHrNr:r�ror�rrr�rtr�rvr�r|r�r�r�rfr�r�rDr�r�r�rrrr�r�8s�������������
������ � � �$�$�$����
$�$�$����
$�$�$�
���
� � �$�$�$�4�4�4����$�$�$�-�-�-�:�:�:�
$�$�$�:�:�:� � � �!�!�!�$!�!�!�(E�(E�(E�T����rr�c��|�t|d��r|j}nd}|dvrt|��S|dvrt|��St d���)N�moder4)�rr4)�wr�z$mode must be 'r', 'rb', 'w', or 'wb')�hasattrr�r1r�r)r=r�s rr8r8
sd���|��1�f��� ��6�D�D��D��{����q�z�z��
�� � ���{�{���:�;�;�;rrA)�__doc__�collectionsrr��_deprecatedrrrTrZr^r_r`ra�AUDIO_FILE_ENCODING_FLOAT�AUDIO_FILE_ENCODING_DOUBLE�AUDIO_FILE_ENCODING_ADPCM_G721�AUDIO_FILE_ENCODING_ADPCM_G722� AUDIO_FILE_ENCODING_ADPCM_G723_3� AUDIO_FILE_ENCODING_ADPCM_G723_5r[rWrY� Exceptionrr%r/r1r�r8rrr�<module>r�s���g�g�R#�"�"�"�"�"��������X�g�.�.�.�.��
�?�T�V�V�
����� �� !�� !�� !������!#��!#��#%� �#%� ��� ��0�1�2�2�2�/�1�� � � � � �I� � � �
�
�
����V�V�V�V�V�V�V�V�pP�P�P�P�P�P�P�P�d<�<�<�<�<�<r |