Changeset 008dcb7 in ammosreader


Ignore:
Timestamp:
05/28/23 09:21:31 (2 years ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
AmmosSource
Children:
0c6fd4c
Parents:
d2736bb
Message:

new branch to generalize input source

Location:
ammosreader
Files:
4 added
4 edited

Legend:

Unmodified
Added
Removed
  • ammosreader/AbstractAmmosReader.py

    rd2736bb r008dcb7  
    1313    """I implement a base class for specialized AmmosReaders."""
    1414
    15     def __init__(self, file_name):
     15    def __init__(self, source):
    1616        """
    1717        I am the standard constructor for Ammos Readers.
     
    1919        Additional information about the file can be added as key/value pairs in tags
    2020
    21         :param file_name: The file to read Ammos data from
    22         :type file_name: str
     21        :param source: The source to read Ammos data from
     22        :type source: AmmosSource
    2323        """
    24         self.__file_name = file_name
    25         self.__ammos_file = open(self.file_name, "rb")
    26         self.__container = AmmosContainer(self.file_name, [])
     24        self.__source = source
     25        self.__container = AmmosContainer(self.__source.name, [])
    2726        self.__tags = {}
    2827
    2928    @property
    30     def file_name(self):
    31         """I return the name of the original file."""
    32         return self.__file_name
    33 
    34     @property
    35     def ammos_file(self):
    36         """I return the file to read the data from."""
    37         return self.__ammos_file
     29    def source(self):
     30        """I return the source of this reader."""
     31        return self.__source
    3832
    3933    @property
     
    5145        assert a_key not in self.__tags
    5246        self.__tags[a_key] = a_value
    53 
    54     def rewind_to_start(self):
    55         """I set the file pointer to the beginning of the file for the next operation."""
    56         self.ammos_file.seek(0)
    5747
    5848    def read_all_frames_left(self):
     
    8575        header_size = AmmosGlobalFrameHeader.HEADER_SIZE
    8676
    87         in_bytes = self.ammos_file.read(header_size)
     77        in_bytes = self.__source.read_bytes(header_size)
    8878        logger.info("Reading next global frame header")
    8979        if ((not in_bytes) or (len(in_bytes) < header_size)):
     
    9989        return current_global_frame_header
    10090
     91    def read_bytes(self, bytes_to_read):
     92        """My descendents have to implement this."""
     93        return self.__source.read_bytes(bytes_to_read)
     94   
    10195    @abstractmethod
    10296    def read_next_global_frame_body(self, data_header_length):
  • ammosreader/AmmosAudioReader.py

    rd2736bb r008dcb7  
    2323        header_size = AmmosAudioDataHeader.HEADER_SIZE
    2424
    25         in_bytes = self.ammos_file.read(header_size)
     25        in_bytes = self.source.read_bytes(header_size)
    2626
    2727        logger.info("\nReading global frame body standard data header\n")
     
    4040        header_size = AmmosExtendedAudioDataHeader.HEADER_SIZE
    4141
    42         in_bytes = self.ammos_file.read(header_size)
     42        in_bytes = self.source.read_bytes(header_size)
    4343
    4444        if ((not in_bytes) or (len(in_bytes) < header_size)):
     
    6565        total = number_of_samples*number_of_channels*sample_size
    6666
    67         byte_string = self.ammos_file.read(total)
     67        byte_string = self.source.read_bytes(total)
    6868
    6969        if len(byte_string) != total:
     
    113113        :rtype: bytes
    114114        """
    115         return (b"".join([each.global_frame_body.data_body.pcm_for_channel(a_channel)
    116                           for each in self.container.global_frames]))
     115        return self.__container.pcm_for_channel(a_channel)
  • ammosreader/AmmosAudioSocketReader.py

    rd2736bb r008dcb7  
    103103                ammos_global_header_buffer.append(self.__get_next_data(20))
    104104                # while len(b''.join(ammos_global_header_buffer)) < 24:
    105                 #    ammos_global_header_buffer.append(self.__socket.recv(24 - len(b''.join(ammos_global_header_buffer))))
     105                #  ammos_global_header_buffer.append(self.__socket.recv(24 - len(b''.join(ammos_global_header_buffer))))
    106106
    107107                ammos_global_header = AmmosGlobalFrameHeader.from_bytes(b''.join(ammos_global_header_buffer))
  • ammosreader/AmmosContainer.py

    rd2736bb r008dcb7  
    5656                "\nEnd time  : " + str(end_time) + "\nFrequencies: " + frq)
    5757
    58     def data_only(self):
     58    def payload(self):
    5959        return [each.global_frame_body.data_body.data for each in self.__global_frames]
     60
     61    def pcm_for_channel(self, a_channel):
     62        """
     63        I return the raw pcm audio data for a given channel.
     64
     65        :param a_channel: the channel I have to extract
     66        :type a_channel: int
     67
     68        :rtype: bytes
     69        """
     70        return (b"".join([each.global_frame_body.data_body.pcm_for_channel(a_channel)
     71                          for each in self.global_frames]))
Note: See TracChangeset for help on using the changeset viewer.