Create audio segment from source audio based on timing information. Extracts a time-based segment from a source audio file and saves it as a separate WAV file. This is used to create individual utterance clips from longer CHiME-6 conversation recordings.
(src_dir, dst_dir, seg_dict)
| 510 | return int((h * 3600 + m * 60 + s) * 1000) |
| 511 | |
| 512 | def create_segment(src_dir, dst_dir, seg_dict): |
| 513 | """Create audio segment from source audio based on timing information. |
| 514 | |
| 515 | Extracts a time-based segment from a source audio file and saves it |
| 516 | as a separate WAV file. This is used to create individual utterance |
| 517 | clips from longer CHiME-6 conversation recordings. |
| 518 | |
| 519 | Args: |
| 520 | src_dir (str): Directory containing source audio files |
| 521 | dst_dir (str): Directory where segmented clips will be saved |
| 522 | seg_dict (dict): Dictionary containing segmentation metadata with keys: |
| 523 | - "audio_file": Source audio filename |
| 524 | - "audio_seg_file": Output segment filename |
| 525 | - "start_time": Segment start time (HH:MM:SS format) |
| 526 | - "end_time": Segment end time (HH:MM:SS format) |
| 527 | |
| 528 | Returns: |
| 529 | str: Path to the created audio segment file |
| 530 | |
| 531 | Raises: |
| 532 | FileNotFoundError: If source audio file doesn't exist |
| 533 | OSError: If unable to create destination directory or write file |
| 534 | ValueError: If timestamp format is invalid |
| 535 | |
| 536 | Example: |
| 537 | >>> seg_dict = { |
| 538 | ... "audio_file": "S02_U06.wav", |
| 539 | ... "audio_seg_file": "S02_U06_0012500_0025000.wav", |
| 540 | ... "start_time": "00:00:12.5", |
| 541 | ... "end_time": "00:00:25.0" |
| 542 | ... } |
| 543 | >>> segment_path = create_segment("audio/", "segments/", seg_dict) |
| 544 | |
| 545 | Note: |
| 546 | Creates destination directory if it doesn't exist. Uses pydub |
| 547 | for audio processing, which supports various audio formats. |
| 548 | """ |
| 549 | audio_file = os.path.join(src_dir, seg_dict["audio_file"]) |
| 550 | segment_file = os.path.join(dst_dir, seg_dict["audio_seg_file"]) |
| 551 | |
| 552 | os.makedirs(dst_dir, exist_ok=True) |
| 553 | audio = AudioSegment.from_wav(audio_file) |
| 554 | start_time = timestamp_to_ms(seg_dict["start_time"]) |
| 555 | end_time = timestamp_to_ms(seg_dict["end_time"]) |
| 556 | clip = audio[start_time:end_time] |
| 557 | clip.export(segment_file, format="wav") |
| 558 | return segment_file |
| 559 | |
| 560 | def parallel_create_segment(args): |
| 561 | """Wrapper function for parallel processing of audio segmentation. |
no test coverage detected