@@ -24,123 +24,87 @@ def sample_audio_data() -> NDArray[np.float32]:
2424
2525def test_save_audio_to_wav_creates_file (sample_audio_data : NDArray [np .float32 ]) -> None :
2626 """Test that save_audio_to_wav creates a WAV file."""
27- # Act
2827 wav_path = save_audio_to_wav (sample_audio_data )
2928
3029 try :
31- # Assert
3230 assert wav_path .exists (), "WAV file should exist"
3331 assert wav_path .suffix == ".wav" , "File should have .wav extension"
3432 assert wav_path .stat ().st_size > 0 , "WAV file should not be empty"
3533 finally :
36- # Cleanup
3734 if wav_path .exists ():
3835 wav_path .unlink ()
3936
4037
4138def test_save_audio_to_wav_correct_sample_rate (sample_audio_data : NDArray [np .float32 ]) -> None :
4239 """Test that saved WAV file has correct sample rate."""
43- # Act
4440 wav_path = save_audio_to_wav (sample_audio_data )
4541
4642 try :
47- # Read back the WAV file
4843 sample_rate , _ = wavfile .read (wav_path )
49-
50- # Assert
5144 assert sample_rate == SAMPLE_RATE , f"Sample rate should be { SAMPLE_RATE } Hz"
5245 finally :
53- # Cleanup
5446 if wav_path .exists ():
5547 wav_path .unlink ()
5648
5749
5850def test_save_audio_to_wav_correct_data_conversion (sample_audio_data : NDArray [np .float32 ]) -> None :
5951 """Test that audio data is correctly converted from float32 to int16."""
60- # Act
6152 wav_path = save_audio_to_wav (sample_audio_data )
6253
6354 try :
64- # Read back the WAV file
6555 _ , audio_int16 = wavfile .read (wav_path )
66-
67- # Assert
6856 assert audio_int16 .dtype == np .int16 , "Audio data should be int16"
6957
70- # Convert back to float32 for comparison
7158 audio_float_reconstructed = audio_int16 .astype (np .float32 ) / 32767.0
7259
73- # Check that values are approximately equal (allowing for conversion loss)
74- # Tolerance is relatively high because int16 has lower precision than float32
7560 np .testing .assert_allclose (
7661 audio_float_reconstructed ,
7762 sample_audio_data ,
7863 atol = 1e-4 ,
7964 err_msg = "Reconstructed audio should match original (within conversion tolerance)" ,
8065 )
8166 finally :
82- # Cleanup
8367 if wav_path .exists ():
8468 wav_path .unlink ()
8569
8670
8771def test_save_audio_to_wav_custom_sample_rate () -> None :
8872 """Test that custom sample rate is respected."""
89- # Arrange
90- custom_rate = 44100 # CD quality
91- duration = 0.5 # seconds
73+ custom_rate = 44100
74+ duration = 0.5
9275 samples = int (custom_rate * duration )
93- audio_data = np .random .randn (samples ).astype (np .float32 ) * 0.5 # Random audio
76+ audio_data = np .random .randn (samples ).astype (np .float32 ) * 0.5
9477
95- # Act
9678 wav_path = save_audio_to_wav (audio_data , sample_rate = custom_rate )
9779
9880 try :
99- # Read back
10081 sample_rate , _ = wavfile .read (wav_path )
101-
102- # Assert
10382 assert sample_rate == custom_rate , f"Sample rate should be { custom_rate } Hz"
10483 finally :
105- # Cleanup
10684 if wav_path .exists ():
10785 wav_path .unlink ()
10886
10987
11088def test_save_audio_to_wav_handles_empty_array () -> None :
11189 """Test that empty audio array raises an error or handles gracefully."""
112- # Arrange
11390 empty_audio = np .array ([], dtype = np .float32 )
114-
115- # Act & Assert
116- # scipy should handle this, but let's verify it doesn't crash
11791 wav_path = save_audio_to_wav (empty_audio )
11892
11993 try :
12094 assert wav_path .exists (), "Should create file even for empty audio"
12195 finally :
122- # Cleanup
12396 if wav_path .exists ():
12497 wav_path .unlink ()
12598
12699
127100def test_save_audio_to_wav_handles_large_values () -> None :
128101 """Test that audio values outside [-1.0, 1.0] are clipped correctly."""
129- # Arrange - audio with values outside normal range
130- audio_data = np .array ([0.5 , 1.5 , - 1.5 , 0.0 ], dtype = np .float32 ) # Some values > 1.0
131-
132- # Act
102+ audio_data = np .array ([0.5 , 1.5 , - 1.5 , 0.0 ], dtype = np .float32 )
133103 wav_path = save_audio_to_wav (audio_data )
134104
135105 try :
136- # Read back
137106 _ , audio_int16 = wavfile .read (wav_path )
138-
139- # Assert - values should be converted (may overflow/clip)
140107 assert audio_int16 .dtype == np .int16
141- # Note: Values > 1.0 will cause overflow in int16 conversion
142- # This is expected behavior - caller should ensure normalized audio
143108 finally :
144- # Cleanup
145109 if wav_path .exists ():
146110 wav_path .unlink ()
0 commit comments