|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test script to verify the segmentation fix for file size and overlap issues. |
| 4 | +""" |
| 5 | + |
| 6 | +import tempfile |
| 7 | +import os |
| 8 | +import numpy as np |
| 9 | +from pedalboard.io import AudioFile |
| 10 | +from audio import AudioSplitter |
| 11 | + |
| 12 | +def create_test_audio(duration_seconds, sample_rate=44100, output_path=None): |
| 13 | + """Create a test audio file with specified duration""" |
| 14 | + if output_path is None: |
| 15 | + output_path = tempfile.mktemp(suffix='.mp3') |
| 16 | + |
| 17 | + # Generate a simple sine wave |
| 18 | + t = np.linspace(0, duration_seconds, int(duration_seconds * sample_rate)) |
| 19 | + audio_data = np.sin(2 * np.pi * 440 * t) # 440 Hz sine wave |
| 20 | + audio_data = audio_data.reshape(1, -1) # Make it mono |
| 21 | + |
| 22 | + with AudioFile(output_path, 'w', sample_rate, 1) as f: |
| 23 | + f.write(audio_data) |
| 24 | + |
| 25 | + return output_path |
| 26 | + |
| 27 | +def test_segmentation(): |
| 28 | + """Test the segmentation logic""" |
| 29 | + print("Testing segmentation fix...") |
| 30 | + |
| 31 | + # Create a test audio file that would be large (simulate ~60MB file) |
| 32 | + # We'll create a shorter file but the logic should work the same |
| 33 | + test_duration = 3600 # 1 hour |
| 34 | + temp_audio = create_test_audio(test_duration) |
| 35 | + |
| 36 | + try: |
| 37 | + # Get file size |
| 38 | + file_size_mb = os.path.getsize(temp_audio) / (1024 * 1024) |
| 39 | + print(f"Test audio file: {file_size_mb:.2f} MB, {test_duration} seconds") |
| 40 | + |
| 41 | + # Test with AudioSplitter |
| 42 | + splitter = AudioSplitter(max_duration_minutes=25, overlap_seconds=30) |
| 43 | + |
| 44 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 45 | + segments = splitter.split_audio(temp_audio, temp_dir) |
| 46 | + |
| 47 | + print(f"\nSegmentation results:") |
| 48 | + print(f"Number of segments: {len(segments)}") |
| 49 | + |
| 50 | + # Assert we have segments |
| 51 | + assert len(segments) > 1, "Should create multiple segments for large file" |
| 52 | + |
| 53 | + for i, segment in enumerate(segments): |
| 54 | + segment_size_mb = os.path.getsize(segment['path']) / (1024 * 1024) |
| 55 | + print(f"Segment {i+1}: {segment['start_time']:.1f}s - {segment['end_time']:.1f}s, " |
| 56 | + f"Duration: {segment['duration']:.1f}s, Size: {segment_size_mb:.2f}MB") |
| 57 | + |
| 58 | + # Assert segment is under 20MB |
| 59 | + assert segment_size_mb <= 20.0, f"Segment {i+1} exceeds 20MB limit: {segment_size_mb:.2f}MB" |
| 60 | + |
| 61 | + # Check overlap |
| 62 | + print(f"\nOverlap verification:") |
| 63 | + for i in range(len(segments) - 1): |
| 64 | + current_end = segments[i]['end_time'] |
| 65 | + next_start = segments[i+1]['start_time'] |
| 66 | + overlap = current_end - next_start |
| 67 | + print(f"Segments {i+1}-{i+2}: overlap = {overlap:.1f}s") |
| 68 | + |
| 69 | + # Assert overlap is approximately 30s (allow 5s tolerance) |
| 70 | + assert 25 <= overlap <= 35, f"Unexpected overlap: {overlap:.1f}s (expected ~30s)" |
| 71 | + |
| 72 | + print(f"\nAll assertions passed! Segmentation is working correctly.") |
| 73 | + |
| 74 | + finally: |
| 75 | + # Clean up |
| 76 | + if os.path.exists(temp_audio): |
| 77 | + os.remove(temp_audio) |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + test_segmentation() |
0 commit comments