Changeset 6cb85c2 in ammosreader


Ignore:
Timestamp:
05/04/22 10:04:50 (3 years ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
AmmosSource, guix
Children:
d88bd02
Parents:
44aebd3
Message:

documentation added and numerical constants replaced

Files:
1 added
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • AmmosAudioReader.py

    r44aebd3 r6cb85c2  
    1 import math
     1"""I parse an R&S AMMOS recording."""
     2
    23import os
    34
     
    1112
    1213class AmmosAudioReader():
     14    """I read the audio data embedded in an R&S AMMOS recording."""
     15
     16    GLOBAL_HEADER_SIZE = 24  # 8 words
     17    STANDARD_AUDIO_DATA_HEADER_SIZE = 36  # 9 words
     18    EXTENDED_AUDIO_DATA_HEADER_SIZE = 44  # 11 words
    1319
    1420    def __init__(self, file_name):
    15 
     21        """
     22        I return an instance of AmmosAudioReader initialized with a given file name.
     23
     24        :param file_name: the file to read from
     25        :type file_name: str
     26        """
    1627        self.file_name = file_name
    1728        self.file = open(self.file_name, "rb")
     
    2031        self.container = AmmosContainer(self.file_name, [])
    2132
    22         self.tags = []
     33        self.tags = {}
    2334
    2435    def rewind_to_start(self):
     36        """I set the file pointer to the beginning of the file for the next operation."""
    2537        self.file.seek(0)
    2638
    2739    def add_tag(self, tag):
    28         self.tags.append(tag)
     40        """
     41        I add a tag to my tag list.
     42
     43        :param tag: The tag to add to my tag list
     44        :type tag: dict
     45        """
     46        self.tags[tag.key] = tag.value
    2947
    3048    def read_all_frames_left(self):
    31 
     49        """
     50        I read all remaining frames into my container until end of file is reached.
     51
     52        :return: a container containing all frames read
     53        :rtype: AmmosContainer
     54        """
    3255        frames_read = 0
    33 
    3456        while True:
    3557            print("Reading single frame", frames_read, '...')
     
    4567
    4668        print(len(self.container.global_frames), "frames read")
     69        return self.container
    4770
    4871    def read_next_global_frame_header(self):
    49         bytes = self.file.read(24)
     72        """
     73        I return the next global frame header read from current position in file.
     74
     75        :return: the next global frame header or None if incomplete
     76        :rtype: AmmosGlobalFrameHeader
     77        """
     78        bytes = self.file.read(AmmosAudioReader.GLOBAL_HEADER_SIZE)
    5079        print("Reading next global frame header")
    51         if ((not bytes) or (len(bytes) < 24)):
    52             print("Can not read all 24 bytes of global frame header")
    53             return None
    54 
     80        if ((not bytes) or (len(bytes) < AmmosAudioReader.GLOBAL_HEADER_SIZE)):
     81            print("Can not read all", AmmosAudioReader.GLOBAL_HEADER_SIZE, "bytes of global frame header")
     82            return None
     83
     84        # FIXME: Catch exceptions and add some asserts
    5585        current_global_frame_header = AmmosGlobalFrameHeader.from_bytes(bytes)
    56         print("Current global frame header", current_global_frame_header)
     86        # print("Current global frame header", current_global_frame_header)
    5787        return current_global_frame_header
    5888
    5989    def read_next_global_frame_body_data_header(self):
    60 
    61         bytes = self.file.read(56)
     90        """
     91        I return the next global frame body data header from current position in file.
     92
     93        :param data_header_size: the number of bytes to read
     94        :type data_header_size: int
     95        :return: the next Ammos Audio Data header or None if incomplete
     96        :rtype: AmmosAudioDataHeader
     97        """
     98        bytes = self.file.read(AmmosAudioReader.STANDARD_AUDIO_DATA_HEADER_SIZE)
    6299
    63100        # print("\nReading global frame body standard data header\n")
    64         if ((not bytes) or (len(bytes) < 56)):
    65             print("Can not read all 56 bytes of global frame body data header")
    66             return None
    67 
    68         data_header = AmmosAudioDataHeader.from_bytes(bytes)
    69         # print("Data header", data_header)
    70         return data_header
     101        if ((not bytes) or (len(bytes) < AmmosAudioReader.STANDARD_AUDIO_DATA_HEADER_SIZE)):
     102            print("Can not read all", AmmosAudioReader.STANDARD_AUDIO_DATA_HEADER_SIZE,
     103                  "bytes of global frame body data header")
     104            return None
     105        return AmmosAudioDataHeader.from_bytes(bytes)
    71106
    72107    def read_next_global_frame_body_extended_data_header(self):
    73 
    74         bytes = self.file.read(44)
    75         # print("\nReading global frame body extended data header\n")
    76 
    77         if ((not bytes) or (len(bytes) < 44)):
    78             print("Can not read all ", 44, "bytes of global frame extended data header")
    79             return None
    80         extended_data_header = AmmosExtendedAudioDataHeader.from_bytes(bytes)
    81         # print("Extended data header", extended_data_header)
    82         return extended_data_header
     108        """
     109        I return the next global frame body extended data header from current position in file.
     110
     111        :return: the next Ammos Audio Extended Data header or None if incomplete
     112        :rtype: AmmosExtendedAudioDataHeader
     113        """
     114        bytes = self.file.read(AmmosAudioReader.EXTENDED_AUDIO_DATA_HEADER_SIZE)
     115
     116        if ((not bytes) or (len(bytes) < AmmosAudioReader.EXTENDED_AUDIO_DATA_HEADER_SIZE)):
     117            print("Can not read all ", AmmosAudioReader.EXTENDED_AUDIO_DATA_HEADER_SIZE,
     118                  " bytes of global frame extended data header")
     119            return None
     120        return AmmosExtendedAudioDataHeader.from_bytes(bytes)
    83121
    84122    def read_next_audio_data_body(self, sample_count, channel_count, sample_size):
    85 
     123        """
     124        I return the next audio data read from current position in file.
     125
     126        :param sample_count: the number of samples per channel inside data body
     127        :type sample_count: int
     128
     129        :param channel_count: number of channels (e.g. mono, stereo or even more)
     130        :type channel_count: int
     131
     132        :param sample_size: sample size in bytes (1, 2 or 4 bytes)
     133        :type sample_size: int
     134
     135        :return: the next audio data or None if incomplete
     136        :rtype: bytes
     137        """
    86138        # FIXME: Describe the parameters better
    87139
     
    100152        audio_data_header = None
    101153
    102         if global_frame_header.data_header_length == 36:
     154        if global_frame_header.data_header_length == AmmosAudioReader.STANDARD_AUDIO_DATA_HEADER_SIZE:
    103155            print("Read standard data header")
    104156            audio_data_header = self.read_next_global_frame_body_data_header()
    105157
    106         else:
     158        if global_frame_header.data_header_length == AmmosAudioReader.EXTENDED_AUDIO_DATA_HEADER_SIZE:
    107159            print("Read extended data header")
    108160            audio_data_header = self.read_next_global_frame_body_extended_data_header()
    109161
    110162        if audio_data_header is None:
    111             print("Data header missing")
     163            print("Data header missing or format unknown")
    112164            return None
    113165
     
    125177
    126178        global_frame_header = self.read_next_global_frame_header()
     179
     180        print(global_frame_header)
    127181
    128182        if global_frame_header is None:
Note: See TracChangeset for help on using the changeset viewer.