-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_audio.py
1111 lines (807 loc) · 35.7 KB
/
test_audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pytest
from pathlib import Path
import io
import numpy as np
import random
import math
import numpy.testing as npt
import pytz
import datetime
from opensoundscape.audio import Audio, AudioOutOfBoundsError, load_channels_as_audio
from opensoundscape import audio
import opensoundscape
@pytest.fixture()
def metadata_wav_str():
return "tests/audio/metadata.wav"
@pytest.fixture()
def new_metadata_wav_str(request):
path = "tests/audio/new_metadata.wav"
def fin():
Path("tests/audio/new_metadata.wav").unlink()
request.addfinalizer(fin)
return path
@pytest.fixture()
def onemin_wav_str():
return "tests/audio/1min.wav"
@pytest.fixture()
def empty_wav_str():
return "tests/audio/empty_2c.wav"
@pytest.fixture()
def veryshort_wav_str():
return "tests/audio/veryshort.wav"
@pytest.fixture()
def short_no_metadata_wav_str():
return "tests/audio/short_no_metadata.wav"
@pytest.fixture()
def veryshort_wav_audio(veryshort_wav_str):
return Audio.from_file(veryshort_wav_str)
@pytest.fixture()
def silence_10s_mp3_str():
return "tests/audio/silence_10s.mp3"
@pytest.fixture()
def not_a_file_str():
return "tests/audio/not_a_file.wav"
@pytest.fixture()
def out_path():
return "tests/audio/audio_out"
@pytest.fixture()
def veryshort_wav_pathlib(veryshort_wav_str):
return Path(veryshort_wav_str)
@pytest.fixture()
def veryshort_wav_bytesio(veryshort_wav_str):
with open(veryshort_wav_str, "rb") as f:
return io.BytesIO(f.read())
@pytest.fixture()
def silence_10s_mp3_pathlib(silence_10s_mp3_str):
return Path(silence_10s_mp3_str)
@pytest.fixture()
def multichannel_audio():
return audio.MultiChannelAudio(
samples=np.array([np.ones(10), np.ones(10)]), sample_rate=10
)
@pytest.fixture()
def tmp_dir(request):
path = Path("tests/audio_out")
if not path.exists():
path.mkdir()
def fin():
path.rmdir()
request.addfinalizer(fin)
return path
@pytest.fixture()
def saved_wav(request, tmp_dir):
path = Path(f"{tmp_dir}/saved.wav")
def fin():
path.unlink()
request.addfinalizer(fin)
return path
@pytest.fixture()
def saved_mp3(request, tmp_dir):
path = Path(f"{tmp_dir}/saved.mp3")
def fin():
if path.exists():
path.unlink()
request.addfinalizer(fin)
return path
@pytest.fixture()
def stereo_wav_str():
return "tests/audio/stereo.wav"
@pytest.fixture()
def silent_wav_str():
return "tests/audio/silence_10s.mp3"
@pytest.fixture()
def convolved_wav_str(out_path, request):
path = Path(f"{out_path}/convolved.wav")
def fin():
path.unlink()
request.addfinalizer(fin)
return path
@pytest.fixture()
def veryshort_audio(veryshort_wav_str):
return Audio.from_file(veryshort_wav_str)
@pytest.fixture()
def LOCA_array_3_str():
return "tests/audio/LOCA_2021_09_24_652_3.wav"
@pytest.fixture()
def LOCA_array_6_str():
return "tests/audio/LOCA_2021_09_24_652_6.wav"
def test_init_with_list():
a = Audio([0] * 10, sample_rate=10)
assert len(a.samples) == 10
def test_init_with_nparray():
a = Audio(np.zeros(10), sample_rate=10)
assert len(a.samples) == 10
def test_load_channels_as_audio(stereo_wav_str):
s = load_channels_as_audio(stereo_wav_str)
assert max(s[0].samples) == 0 # channel 1 of stereo.wav is all 0
assert max(s[1].samples) == 1 # channel 2 of stereo.wav is all 1
assert len(s) == 2
assert type(s[0]) == Audio
assert s[0].metadata["channel"] == "1 of 2"
assert s[0].metadata["channels"] == 1
def test_load_channels_as_audio_from_mono(veryshort_wav_str):
s = load_channels_as_audio(veryshort_wav_str)
assert len(s) == 1
assert type(s[0]) == Audio
def test_duration(veryshort_wav_audio):
assert math.isclose(veryshort_wav_audio.duration, 0.14208616780045352, abs_tol=1e-7)
def test_normalize(veryshort_wav_audio):
assert math.isclose(
max(abs(veryshort_wav_audio.normalize(peak_level=0.5).samples)),
0.5,
abs_tol=1e-4,
)
def test_normalize_default(veryshort_wav_audio):
assert math.isclose(
max(abs(veryshort_wav_audio.normalize().samples)),
1.0,
abs_tol=1e-4,
)
def test_normalize_dBFS(veryshort_wav_audio):
assert math.isclose(
max(abs(veryshort_wav_audio.normalize(peak_dBFS=-3).samples)),
0.707946,
abs_tol=1e-4,
)
def test_apply_gain():
a = Audio([1, -1, 0], sample_rate=10).apply_gain(dB=-20)
assert math.isclose(a.samples.max(), 0.1, abs_tol=1e-6)
assert math.isclose(a.samples.min(), -0.1, abs_tol=1e-6)
def test_gain_clips():
a = Audio([0.5, -0.5, 0], sample_rate=10).apply_gain(dB=10)
assert math.isclose(a.samples.max(), 1, abs_tol=1e-6)
assert math.isclose(a.samples.min(), -1, abs_tol=1e-6)
def test_normalize_by_db(veryshort_wav_audio):
assert math.isclose(
max(abs(veryshort_wav_audio.normalize(peak_dBFS=0).samples)), 1, abs_tol=1e-4
)
def test_normalize_doesnt_allow_both_arguments(veryshort_wav_audio):
with pytest.raises(ValueError):
veryshort_wav_audio.normalize(peak_dBFS=0, peak_level=0.2)
def test_load_incorrect_timestamp(onemin_wav_str):
with pytest.raises(AssertionError):
timestamp = "NotATimestamp"
s = Audio.from_file(onemin_wav_str, start_timestamp=timestamp)
def test_load_timestamp_no_metadata_raises(short_no_metadata_wav_str):
"""attempts to parse metadata to get datetime, fails because file doesn't have metadata"""
with pytest.raises(AssertionError): # file doesn't have audiomoth metadata
local_timestamp = datetime.datetime(2018, 4, 5, 9, 32, 0)
local_timezone = pytz.timezone("US/Eastern")
timestamp = local_timezone.localize(local_timestamp)
s = Audio.from_file(short_no_metadata_wav_str, start_timestamp=timestamp)
def test_load_timestamp_after_end_of_recording(metadata_wav_str):
with pytest.raises(AudioOutOfBoundsError):
local_timestamp = datetime.datetime(
2021, 4, 4, 0, 0, 0
) # 1 year after recording
local_timezone = pytz.timezone("US/Eastern")
timestamp = local_timezone.localize(local_timestamp)
s = Audio.from_file(
metadata_wav_str, start_timestamp=timestamp, out_of_bounds_mode="raise"
)
def test_load_timestamp_before_recording(metadata_wav_str):
with pytest.raises(AudioOutOfBoundsError):
local_timestamp = datetime.datetime(
2018, 4, 4, 0, 0, 0
) # 1 year before recording
local_timezone = pytz.timezone("UTC")
timestamp = local_timezone.localize(local_timestamp)
s = Audio.from_file(
metadata_wav_str, start_timestamp=timestamp, out_of_bounds_mode="raise"
)
def test_load_timestamp_before_warnmode(metadata_wav_str):
with pytest.warns(UserWarning):
correct_ts = Audio.from_file(metadata_wav_str).metadata["recording_start_time"]
local_timestamp = datetime.datetime(
2018, 4, 4, 0, 0, 0
) # 1 year before recording
local_timezone = pytz.timezone("UTC")
timestamp = local_timezone.localize(local_timestamp)
s = Audio.from_file(
metadata_wav_str, start_timestamp=timestamp, out_of_bounds_mode="warn"
)
# Assert the start time is the correct, original timestamp and has not been changed
assert s.metadata["recording_start_time"] == correct_ts
def test_retain_metadata_soundfile(metadata_wav_str, new_metadata_wav_str):
a = Audio.from_file(metadata_wav_str)
a.save(new_metadata_wav_str, metadata_format="soundfile")
new_a = Audio.from_file(new_metadata_wav_str)
# file size may differ slightly, other fields should be the same
assert new_a.metadata == a.metadata
def test_save_with_empty_metadata_field(metadata_wav_str, new_metadata_wav_str):
a = Audio.from_file(metadata_wav_str)
a.metadata = {"date": "", "artist": "me"}
# should write new file with ' ' as date rather than raising an error
a.save(new_metadata_wav_str, metadata_format="soundfile")
new_a = Audio.from_file(new_metadata_wav_str)
assert new_a.metadata["date"] == " "
assert new_a.metadata["artist"] == "me"
def test_save_load_opso_metadata(metadata_wav_str, new_metadata_wav_str):
# add more tests if more versions are added
a = Audio.from_file(metadata_wav_str)
a.save(new_metadata_wav_str, metadata_format="opso")
new_md = Audio.from_file(new_metadata_wav_str).metadata
assert new_md["opensoundscape_version"] == opensoundscape.__version__
assert new_md["opso_metadata_version"] == "v0.1" # current default version
# file size may differ slightly, other fields should be the same
# all other keys/values should be equivalent:
del new_md["opensoundscape_version"]
del new_md["opso_metadata_version"]
assert new_md == a.metadata
def test_update_metadata(metadata_wav_str, new_metadata_wav_str):
a = Audio.from_file(metadata_wav_str)
a.metadata["artist"] = "newartist"
a.save(new_metadata_wav_str)
assert Audio.from_file(new_metadata_wav_str).metadata["artist"] == "newartist"
def test_load_empty_wav(empty_wav_str):
with pytest.raises(AudioOutOfBoundsError):
s = Audio.from_file(empty_wav_str, out_of_bounds_mode="raise")
def test_load_duration_too_long(veryshort_wav_str):
with pytest.raises(AudioOutOfBoundsError):
s = Audio.from_file(veryshort_wav_str, duration=5, out_of_bounds_mode="raise")
def test_load_veryshort_wav_str_44100(veryshort_wav_str):
s = Audio.from_file(veryshort_wav_str)
assert s.samples.shape == (6266,)
def test_load_veryshort_wav_str(veryshort_wav_str):
s = Audio.from_file(veryshort_wav_str, sample_rate=22050)
assert s.samples.shape == (3133,)
def test_load_veryshort_wav_pathlib(veryshort_wav_pathlib):
s = Audio.from_file(veryshort_wav_pathlib, sample_rate=22050)
assert s.samples.shape == (3133,)
def test_load_veryshort_wav_bytesio(veryshort_wav_bytesio):
s = Audio.from_bytesio(veryshort_wav_bytesio, sample_rate=22050)
assert s.samples.shape == (3133,)
def test_load_pathlib_and_bytesio_are_almost_equal(
veryshort_wav_pathlib, veryshort_wav_bytesio
):
s_pathlib = Audio.from_file(veryshort_wav_pathlib)
s_bytesio = Audio.from_bytesio(veryshort_wav_bytesio)
np.testing.assert_allclose(s_pathlib.samples, s_bytesio.samples, atol=1e-7)
def test_load_not_a_file_asserts_not_a_file(not_a_file_str):
with pytest.raises(FileNotFoundError):
Audio.from_file(not_a_file_str)
def test_load_metadata(veryshort_wav_str):
m_dict = audio.parse_metadata(veryshort_wav_str)
assert m_dict["samplerate"] == 44100
a = Audio.from_file(veryshort_wav_str)
assert a.metadata["samplerate"] == 44100
def test_load_metadata_int_offset(metadata_wav_str):
# addresses issue #928
Audio.from_file(metadata_wav_str, offset=np.int32(3), duration=0.1)
Audio.from_file(metadata_wav_str, offset=np.float32(3), duration=0.1)
# currently don't know how to create a file with bad / no metadata
# def test_load_metadata_warning(path_with_no_metadata):
# with pytest.raises(UserWarning)
# a=Audio.from_file(path_with_no_metadata)
def test_calculate_rms(veryshort_wav_audio):
assert math.isclose(veryshort_wav_audio.rms, 0.0871002, abs_tol=1e-7)
def test_calculate_dBFS(veryshort_wav_audio):
assert math.isclose(veryshort_wav_audio.dBFS, -18.189316963185377, abs_tol=1e-5)
def test_property_trim_length_is_correct(silence_10s_mp3_str):
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
duration = audio.duration
for _ in range(100):
[first, second] = sorted(
[random.uniform(0, duration), random.uniform(0, duration)]
)
assert math.isclose(
audio.trim(first, second).duration, second - first, abs_tol=1e-4
)
def test_trim_updates_metadata(metadata_wav_str):
a = Audio.from_file(metadata_wav_str)
a2 = a.trim(1, 2)
assert a2.metadata["recording_start_time"] == a.metadata[
"recording_start_time"
] + datetime.timedelta(seconds=1)
assert a2.metadata["duration"] == 1
def test_trim_with_timestamps_metadata(metadata_wav_str):
a = Audio.from_file(metadata_wav_str)
start_time = a.metadata["recording_start_time"]
a2 = a.trim_with_timestamps(
start_time + datetime.timedelta(seconds=1),
start_time + datetime.timedelta(seconds=2),
)
assert a2.metadata["recording_start_time"] == start_time + datetime.timedelta(
seconds=1
)
assert a2.metadata["duration"] == 1
assert a2.duration == 1
# repeat with `duration` argument:
a3 = a.trim_with_timestamps(start_time + datetime.timedelta(seconds=1), duration=1)
assert a3.metadata["recording_start_time"] == start_time + datetime.timedelta(
seconds=1
)
assert a3.metadata["duration"] == 1
assert a3.duration == 1
def test_trim_with_timestamps_raises_error_if_tz_is_not_localized(metadata_wav_str):
# check that it raises error if tz is not localized
a = Audio.from_file(metadata_wav_str)
with pytest.raises(AssertionError):
a.trim_with_timestamps(
datetime.datetime.now(), duration=1, out_of_bounds_mode="ignore"
)
def test_trim_timestamps_raises_out_of_bounds_error(metadata_wav_str):
# raises error if requested time is out of bounds and out_of_bounds_error is True
a = Audio.from_file(metadata_wav_str)
start_time = a.metadata["recording_start_time"]
with pytest.raises(AudioOutOfBoundsError):
# start too early
a.trim_with_timestamps(
start_time - datetime.timedelta(seconds=3),
duration=1,
out_of_bounds_mode="raise",
)
with pytest.raises(AudioOutOfBoundsError):
# end too late
a.trim_with_timestamps(start_time, duration=1000, out_of_bounds_mode="raise")
# no error if out_of_bounds_error is False
a.trim_with_timestamps(
start_time - datetime.timedelta(seconds=3),
duration=1000,
out_of_bounds_mode="ignore",
)
def test_trim_from_negative_time(silence_10s_mp3_str):
"""correct behavior is to trim from time zero"""
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
a = audio.trim(-1, 5)
assert math.isclose(a.duration, 5, abs_tol=1e-5)
with pytest.warns(UserWarning):
audio.trim(-1, 5, out_of_bounds_mode="warn")
with pytest.raises(AudioOutOfBoundsError):
audio.trim(-1, 5, out_of_bounds_mode="raise")
def test_trim_samples(silence_10s_mp3_str):
"""correct behavior is to trim from time zero"""
audio = Audio.from_file(silence_10s_mp3_str)
assert len(audio.trim_samples(0, 10).samples) == 10
assert len(audio.trim_samples(200, 210).samples) == 10
assert len(audio.trim_samples(-10, 10).samples) == 10
assert len(audio.trim_samples(10, 10).samples) == 0
with pytest.raises(AssertionError):
# cannot pass start index > end index
audio.trim_samples(20, 10)
def test_trim_past_end_of_clip(silence_10s_mp3_str):
"""correct behavior is to trim to the end of the clip"""
a = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
audio = a.trim(9, 11)
assert math.isclose(audio.duration, 1, abs_tol=1e-5)
with pytest.warns(UserWarning):
a.trim(9, 11, out_of_bounds_mode="warn")
with pytest.raises(AudioOutOfBoundsError):
a.trim(9, 11, out_of_bounds_mode="raise")
def test_trim_with_datetime(silence_10s_mp3_str):
a = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
a.metadata["recording_start_time"] = datetime.datetime(
2022, 1, 1, 0, 0, 0, tzinfo=pytz.utc
)
start = datetime.datetime(2022, 1, 1, 0, 0, 1, tzinfo=pytz.utc)
end = datetime.datetime(2022, 1, 1, 0, 0, 2, tzinfo=pytz.utc)
audio = a.trim_with_timestamps(start, end)
assert math.isclose(audio.duration, 1, abs_tol=1e-5)
def test_resample_veryshort_wav(veryshort_wav_str):
"""Note: default resample type changed from kaiser_Fast to soxr_hq
this change mirrors default change in librosa, and means we need to
require librosa>=0.10.0 or add soxr as a dependency. See issue:
https://github.com/kitzeslab/opensoundscape/issues/674
I've chosen to add librosa>=0.10.0 as a dependency.
"""
audio = Audio.from_file(veryshort_wav_str)
dur = audio.duration
resampled_audio = audio.resample(22050)
assert np.isclose(resampled_audio.duration, dur, 1e-6)
assert resampled_audio.samples.shape == (3133,)
assert resampled_audio.sample_rate == 22050
def test_spawn(veryshort_wav_audio):
"""spawn method creates copy of Audio object with any kwarg fields updated"""
a = veryshort_wav_audio
a2 = a._spawn()
assert np.all(a2.samples == a.samples)
assert a2.sample_rate == a.sample_rate
# provide an updated value for the new object
a3 = a._spawn(sample_rate=20)
assert a3.sample_rate == 20
with pytest.raises(AssertionError):
# should not be able to pass non __slot__ kwargs
a._spawn(b=1)
def test_methods_retain_metadata(metadata_wav_str):
"""resolved issue 679 by implementing ._spawn
This should avoid future issues with improperly calling
Audio.__init__ ie if the arguments to __init__ change
"""
audio = Audio.from_file(metadata_wav_str)
a2 = audio.resample(22050)
assert audio.metadata == a2.metadata
a2 = audio.apply_gain(-2)
assert audio.metadata == a2.metadata
a2 = audio.normalize()
assert audio.metadata == a2.metadata
def test_resample_mp3_nonstandard_sr(silence_10s_mp3_str):
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
dur = audio.duration
resampled_audio = audio.resample(5000)
assert resampled_audio.duration == dur
assert resampled_audio.sample_rate == 5000
def test_resample_classmethod_vs_instancemethod(silence_10s_mp3_str):
a1 = Audio.from_file(silence_10s_mp3_str)
a1 = a1.resample(2000)
a2 = Audio.from_file(silence_10s_mp3_str, sample_rate=2000)
npt.assert_array_almost_equal(a1.samples, a2.samples, decimal=5)
def test_extend_to_length_is_correct(silence_10s_mp3_str):
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
duration = audio.duration
for _ in range(100):
extend_length = random.uniform(duration, duration * 10)
assert math.isclose(
audio.extend_to(extend_length).duration, extend_length, abs_tol=1e-4
)
def test_extend_to_correct_metadata(silence_10s_mp3_str):
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
a2 = audio.extend_to(12)
# duration in metadata should be updated:
assert math.isclose(a2.metadata["duration"], 12)
# other metadata should be retained:
assert a2.metadata["subtype"] == audio.metadata["subtype"]
def test_extend_to_shorter_duration(silence_10s_mp3_str):
# extending 10s to 6s should retain 10s
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
a2 = audio.extend_to(6)
assert math.isclose(a2.duration, 10)
# duration in metadata should be updated:
assert math.isclose(a2.metadata["duration"], 10)
# other metadata should be retained:
assert a2.metadata["subtype"] == audio.metadata["subtype"]
def test_extend_to_correct_duration_ok(silence_10s_mp3_str):
# extending 10s to 10 shouldn't raise error (#972)
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
a2 = audio.extend_to(10)
assert math.isclose(a2.duration, 10)
# duration in metadata should be updated:
assert math.isclose(a2.metadata["duration"], 10)
# other metadata should be retained:
assert a2.metadata["subtype"] == audio.metadata["subtype"]
def test_extend_by(silence_10s_mp3_str):
audio = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
a2 = audio.extend_by(1)
assert math.isclose(a2.duration, 11)
# duration in metadata should be updated:
assert math.isclose(a2.metadata["duration"], 11)
# other metadata should be retained:
assert a2.metadata["subtype"] == audio.metadata["subtype"]
# doesn't allow negative extend_by(duration)
with pytest.raises(AssertionError):
a2.extend_by(-1)
def test_bandpass(silence_10s_mp3_str):
s = Audio.from_file(silence_10s_mp3_str)
assert isinstance(s.bandpass(1, 100, 9), Audio)
def test_lowpass(silence_10s_mp3_str):
s = Audio.from_file(silence_10s_mp3_str)
assert isinstance(s.lowpass(100, 9), Audio)
def test_highpass(silence_10s_mp3_str):
s = Audio.from_file(silence_10s_mp3_str)
assert isinstance(s.highpass(100, 9), Audio)
def test_bandpass_sample_rate_10000(silence_10s_mp3_str):
s = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
assert isinstance(s.bandpass(0.001, 4999, 9), Audio)
def test_bandpass_low_error(silence_10s_mp3_str):
s = Audio.from_file(silence_10s_mp3_str)
with pytest.raises(ValueError):
s.bandpass(0, 100, 9)
def test_bandpass_high_error(silence_10s_mp3_str):
s = Audio.from_file(silence_10s_mp3_str, sample_rate=10000)
with pytest.raises(ValueError):
s.bandpass(100, 5000, 9)
def test_spectrum(silence_10s_mp3_str):
s = Audio.from_file(silence_10s_mp3_str)
assert len(s.spectrum()) == 2
def test_save(silence_10s_mp3_str, saved_wav):
Audio.from_file(silence_10s_mp3_str).save(saved_wav)
assert saved_wav.exists()
def test_save_mp3(silence_10s_mp3_str, saved_mp3):
try:
Audio.from_file(silence_10s_mp3_str).save(saved_mp3)
assert saved_mp3.exists()
Audio.from_file(saved_mp3) # make sure we can still load it as audio
except NotImplementedError:
# only supported by libsndfile>=1.1.0, which is not available yet
# on ubuntu as of Dec 2022. So, we just give the user a helpful error.
pass
def test_audio_constructor_should_fail_on_file(veryshort_wav_str):
with pytest.raises(ValueError):
Audio(veryshort_wav_str, 22050)
def test_audio_constructor_should_fail_on_non_integer_sample_rate():
with pytest.raises(ValueError):
Audio(np.zeros(10), "fail...")
def test_split_and_save_default(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib)
clip_df = audio.split_and_save("unnecessary", "unnecessary", 5.0, dry_run=True)
assert clip_df.shape[0] == 2
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert clip_df.iloc[1]["start_time"] == 5.0
assert clip_df.iloc[1]["end_time"] == 10.0
def test_split_and_save_default_overlay(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib)
clip_df = audio.split_and_save("unnecessary", "unnecessary", 5.0, 1.0, dry_run=True)
assert clip_df.shape[0] == 2
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert clip_df.iloc[1]["start_time"] == 4.0
assert clip_df.iloc[1]["end_time"] == 9.0
def test_split_and_save_default_full(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib)
clip_df = audio.split_and_save(
"unnecessary", "unnecessary", 5.0, 1.0, final_clip="full", dry_run=True
)
assert clip_df.shape[0] == 3
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert clip_df.iloc[1]["start_time"] == 4.0
assert clip_df.iloc[1]["end_time"] == 9.0
assert clip_df.iloc[2]["start_time"] == 5.0
assert clip_df.iloc[2]["end_time"] == 10.0
def test_split_and_save_default_extend(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib)
clip_df = audio.split_and_save(
"unnecessary", "unnecessary", 5.0, 1.0, final_clip="extend", dry_run=True
)
assert clip_df.shape[0] == 3
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert clip_df.iloc[1]["start_time"] == 4.0
assert clip_df.iloc[1]["end_time"] == 9.0
assert clip_df.iloc[2]["start_time"] == 8.0
assert clip_df.iloc[2]["end_time"] == 13.0
def test_non_integer_source_split_and_save_default(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib).trim(0, 8.2)
clip_df = audio.split_and_save("unnecessary", "unnecessary", 5, dry_run=True)
assert clip_df.shape[0] == 1
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
def test_non_integer_source_split_and_save_remainder(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib).trim(0, 8.2)
clip_df = audio.split_and_save(
"unnecessary", "unnecessary", 5, dry_run=True, final_clip="remainder"
)
assert clip_df.shape[0] == 2
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert clip_df.iloc[1]["start_time"] == 5.0
assert abs(clip_df.iloc[1]["end_time"] - 8.2) < 0.1
def test_non_integer_source_split_and_save_full(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib).trim(0, 8.2)
clip_df = audio.split_and_save(
"unnecessary", "unnecessary", 5, dry_run=True, final_clip="full"
)
assert clip_df.shape[0] == 2
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert abs(clip_df.iloc[1]["start_time"] - 3.2) < 0.1
assert abs(clip_df.iloc[1]["end_time"] - 8.2) < 0.1
def test_non_integer_source_split_and_save_extend(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib).trim(0, 8.2)
clip_df = audio.split_and_save(
"unnecessary", "unnecessary", 5, dry_run=True, final_clip="extend"
)
assert clip_df.shape[0] == 2
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert clip_df.iloc[1]["start_time"] == 5.0
assert (clip_df.iloc[1]["end_time"] - 10.0) < 0.1
def test_non_integer_cliplen_split_and_save(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib)
clip_df = audio.split_and_save("unnecessary", "unnecessary", 4.5, dry_run=True)
assert clip_df.shape[0] == 2
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 4.5
assert clip_df.iloc[1]["start_time"] == 4.5
assert clip_df.iloc[1]["end_time"] == 9.0
def test_non_integer_overlaplen_split_and_save(silence_10s_mp3_pathlib):
audio = Audio.from_file(silence_10s_mp3_pathlib)
clip_df = audio.split_and_save("unnecessary", "unnecessary", 5.0, 0.5, dry_run=True)
assert clip_df.shape[0] == 2
assert clip_df.iloc[0]["start_time"] == 0.0
assert clip_df.iloc[0]["end_time"] == 5.0
assert clip_df.iloc[1]["start_time"] == 4.5
assert clip_df.iloc[1]["end_time"] == 9.5
def test_skip_loading_metadata(metadata_wav_str):
a = Audio.from_file(metadata_wav_str, load_metadata=False)
assert a.metadata is None
def test_silent_classmethod():
a = Audio.silence(10, 200)
assert len(a.samples) == 2000
assert max(a.samples) == 0
def test_noise_classmethod():
for c in ["white", "blue", "violet", "brown", "pink"]:
a = Audio.noise(1, 200, color=c)
assert len(a.samples) == 200
def test_concat(veryshort_wav_audio):
a = audio.concat([veryshort_wav_audio, veryshort_wav_audio])
assert a.duration == 2 * veryshort_wav_audio.duration
def test_mix(veryshort_wav_audio):
m = audio.mix([veryshort_wav_audio, veryshort_wav_audio], gain=0)
assert math.isclose(
max(veryshort_wav_audio.samples) * 2, max(m.samples), abs_tol=1e-6
)
def test_mix_duration(veryshort_wav_audio):
m = audio.mix([veryshort_wav_audio, veryshort_wav_audio], duration=1)
assert math.isclose(m.duration, 1, abs_tol=1e-6)
def test_mix_duration_extends(veryshort_wav_audio):
m = audio.mix([veryshort_wav_audio, veryshort_wav_audio], duration=1)
assert math.isclose(m.duration, 1, abs_tol=1e-3)
def test_mix_duration_truncates(veryshort_wav_audio):
a = Audio.silence(10, veryshort_wav_audio.sample_rate)
m = audio.mix([a, veryshort_wav_audio], duration=1)
assert math.isclose(m.duration, 1, abs_tol=1e-3)
def test_mix_offsets(veryshort_wav_audio):
m = audio.mix([veryshort_wav_audio, veryshort_wav_audio], offsets=[0, 1])
# expected behavior: length will be offset + audio length
assert math.isclose(m.duration, 1 + veryshort_wav_audio.duration, abs_tol=1e-3)
def test_mix_clip(veryshort_wav_audio):
# should never have values outside of [-1,1]
m = audio.mix([veryshort_wav_audio, veryshort_wav_audio], gain=100)
assert max(abs(m.samples)) <= 1
def test_loop(veryshort_wav_audio):
veryshort_wav_audio.metadata = {"duration": veryshort_wav_audio.duration}
a2 = veryshort_wav_audio.loop(n=2)
assert math.isclose(a2.duration, veryshort_wav_audio.duration * 2, abs_tol=1e-5)
assert math.isclose(a2.duration, a2.metadata["duration"])
a3 = veryshort_wav_audio.loop(length=1)
assert math.isclose(a3.duration, 1.0, abs_tol=1e-5)
assert math.isclose(a3.metadata["duration"], 1.0, abs_tol=1e-5)
def test_extend_to_with_short(veryshort_wav_audio):
a = veryshort_wav_audio.extend_to(duration=1)
assert math.isclose(a.duration, 1.0, abs_tol=1e-5)
assert math.isclose(a.metadata["duration"], 1.0, abs_tol=1e-5)
# added samples should be zero
assert math.isclose(0.0, np.max(a.samples[-100:]), abs_tol=1e-7)
def test_bandpass_filter(veryshort_audio):
bandpassed = audio.bandpass_filter(
veryshort_audio.samples, 1000, 2000, veryshort_audio.sample_rate
)
assert len(bandpassed) == len(veryshort_audio.samples)
def test_reduce_noise():
from opensoundscape.utils import set_seed
set_seed(0)
noise = Audio.noise(1, sample_rate=8000, color="white")
reduced = noise.reduce_noise()
assert reduced.rms < noise.rms
def test_clipping_detector(veryshort_audio):
assert audio.clipping_detector(veryshort_audio.samples) > -1
def test_estimate_delay(veryshort_audio):
start_t = 0.03
end_t = 0.12
signal = veryshort_audio.trim(start_time=start_t, end_time=end_t)
delay = 0.02
max_delay = 0.03
ref_sig = veryshort_audio.trim(start_time=start_t + delay, end_time=end_t + delay)
# phat filter will fail on the below
# maybe because the signal is too short
assert math.isclose(
audio.estimate_delay(signal, ref_sig, max_delay=max_delay, cc_filter="cc_norm"),
delay,
abs_tol=1e-4,
)
def test_estimate_delay_real_audio(LOCA_array_3_str, LOCA_array_6_str):
max_delay = 0.1
start_time = 0.2
duration = 0.4
audio_3_reference = Audio.from_file(
LOCA_array_3_str,
offset=start_time,
duration=duration,
)
audio_6 = Audio.from_file(
LOCA_array_6_str,
offset=start_time,
duration=duration,
)
real_measured_delay = 0.03
delay = audio.estimate_delay(audio_6, audio_3_reference, max_delay=max_delay)
assert abs(delay) < max_delay
assert np.isclose(delay, real_measured_delay, atol=5e-3)
def test_estimate_delay_with_bandpass(veryshort_audio):
start_t = 0.03
end_t = 0.1
signal = veryshort_audio.trim(start_time=start_t, end_time=end_t)
delay = 0.01
max_delay = 0.02
ref_sig = veryshort_audio.trim(start_time=start_t + delay, end_time=end_t + delay)
dly = audio.estimate_delay(
signal,
ref_sig,
max_delay=max_delay,
bandpass_range=[100, 10000],
bandpass_order=5,
)
assert math.isclose(dly, delay, abs_tol=1e-4)
def test_estimate_delay_return_cc_max(veryshort_audio):
max_delay = 0.05
delay, ccmax = audio.estimate_delay(
veryshort_audio,
veryshort_audio,
max_delay=max_delay,
cc_filter="cc",
return_cc_max=True,
)
# will use only the central part of the signal
section_used = veryshort_audio.trim(
start_time=max_delay, end_time=veryshort_audio.duration - max_delay
)