Changeset 6cb85c2 in ammosreader
- Timestamp:
- 05/04/22 10:04:50 (3 years ago)
- Branches:
- AmmosSource, guix
- Children:
- d88bd02
- Parents:
- 44aebd3
- 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 2 3 import os 3 4 … … 11 12 12 13 class 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 13 19 14 20 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 """ 16 27 self.file_name = file_name 17 28 self.file = open(self.file_name, "rb") … … 20 31 self.container = AmmosContainer(self.file_name, []) 21 32 22 self.tags = []33 self.tags = {} 23 34 24 35 def rewind_to_start(self): 36 """I set the file pointer to the beginning of the file for the next operation.""" 25 37 self.file.seek(0) 26 38 27 39 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 29 47 30 48 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 """ 32 55 frames_read = 0 33 34 56 while True: 35 57 print("Reading single frame", frames_read, '...') … … 45 67 46 68 print(len(self.container.global_frames), "frames read") 69 return self.container 47 70 48 71 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) 50 79 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 55 85 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) 57 87 return current_global_frame_header 58 88 59 89 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) 62 99 63 100 # 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) 71 106 72 107 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) 83 121 84 122 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 """ 86 138 # FIXME: Describe the parameters better 87 139 … … 100 152 audio_data_header = None 101 153 102 if global_frame_header.data_header_length == 36:154 if global_frame_header.data_header_length == AmmosAudioReader.STANDARD_AUDIO_DATA_HEADER_SIZE: 103 155 print("Read standard data header") 104 156 audio_data_header = self.read_next_global_frame_body_data_header() 105 157 106 else:158 if global_frame_header.data_header_length == AmmosAudioReader.EXTENDED_AUDIO_DATA_HEADER_SIZE: 107 159 print("Read extended data header") 108 160 audio_data_header = self.read_next_global_frame_body_extended_data_header() 109 161 110 162 if audio_data_header is None: 111 print("Data header missing ")163 print("Data header missing or format unknown") 112 164 return None 113 165 … … 125 177 126 178 global_frame_header = self.read_next_global_frame_header() 179 180 print(global_frame_header) 127 181 128 182 if global_frame_header is None:
Note:
See TracChangeset
for help on using the changeset viewer.