Set the filename for input or output file. Checks file extension to determine if file format is supported. For input: verifies file exists and is supported. For output: removes existing file if present. Args: base_dir: The base directory for the
(self, base_dir, filename)
| 153 | return self.device |
| 154 | |
| 155 | def _setFilename(self, base_dir, filename): |
| 156 | """ |
| 157 | Set the filename for input or output file. |
| 158 | |
| 159 | Checks file extension to determine if file format is supported. |
| 160 | For input: verifies file exists and is supported. |
| 161 | For output: removes existing file if present. |
| 162 | Args: |
| 163 | base_dir: The base directory for the file. |
| 164 | filename: The name of the file (with extension). |
| 165 | Returns: |
| 166 | filename_valid: True if the filename is valid and set, False otherwise. |
| 167 | """ |
| 168 | logger.debug(f"_setFilename: base_dir={base_dir}, filename={filename}") |
| 169 | |
| 170 | filename_valid = False |
| 171 | |
| 172 | self.filename = None |
| 173 | |
| 174 | if filename == "": |
| 175 | # Empty filename is valid (use microphone/speakers) |
| 176 | return True |
| 177 | |
| 178 | work_dir = Path(base_dir) |
| 179 | file_name = Path(filename) |
| 180 | file_path = Path("") |
| 181 | |
| 182 | # Check if file extension is supported |
| 183 | ext = file_name.suffix.lstrip('.').lower() |
| 184 | if ext not in supported_files: |
| 185 | logger.error(f"_setFilename: unsupported file extension={ext}") |
| 186 | return filename_valid |
| 187 | |
| 188 | # Check if filename is absolute path |
| 189 | if file_name.is_absolute(): |
| 190 | # Absolute path provided |
| 191 | file_path = file_name |
| 192 | logger.debug(f"_setFilename: absolute path provided, file path={file_path}") |
| 193 | else: |
| 194 | # Relative path provided, create full path |
| 195 | file_path = work_dir / file_name |
| 196 | logger.debug(f"_setFilename: relative path provided, file path={file_path}") |
| 197 | |
| 198 | # Normalize the file path |
| 199 | file_path = file_path.resolve() |
| 200 | |
| 201 | if self.mode == MODE_AUDIO_INPUT: |
| 202 | # Check if the file exists |
| 203 | if Path(file_path).exists(): |
| 204 | # File exists |
| 205 | filename_valid = True |
| 206 | else: |
| 207 | logger.error(f"_setFilename: file does not exist: {file_path}") |
| 208 | |
| 209 | if self.mode == MODE_AUDIO_OUTPUT: |
| 210 | # Check if the file exists |
| 211 | if Path(file_path).exists(): |
| 212 | # Remove existing file |