segment_finder.py 1.78 KB
Newer Older
Matteo's avatar
update  
Matteo committed
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
from uuid import uuid4
import numpy as np

from mpai_cae_arp.audio import AudioWave, Noise
from mpai_cae_arp.types.irregularity import Irregularity, IrregularityFile, Source
from mpai_cae_arp.time import frames_to_seconds


def calculate_offset(audio: AudioWave, video: AudioWave) -> float:
    """
    Calculates the offset between two audio files based on their cross-correlation.

    Parameters
    ----------
    audio : AudioWave
        The audio file to be used as reference.
    video : AudioWave
        The audio file to be used as target.
    
    Returns
    -------
    float
    """

    corr = np.correlate(audio.array, video.array, mode="full")
    lags = np.arange(-len(audio.array) + 1, len(video.array))
    lag_idx = np.argmax(np.abs(corr))

    return lags[lag_idx] / audio.samplerate


def foo(audio_src) -> list[Irregularity]:
    input = AudioWave.from_file(audio_src)
    input_channels: list[AudioWave] = []
    for channel in input.channels:
        input_channels.append(input.get_channel(channel))

    irreg_list: list[Irregularity] = []
    for audio in input_channels:
        for _, noise_list in audio.get_silence_slices([
            Noise("A", -50, -63),
            Noise("B", -63, -69),
            Noise("C", -69, -72)],
            length=500).items():
            for start, _ in noise_list:
                irreg_list.append(
                    Irregularity(
                        uuid=uuid4(),
                        source=Source.AUDIO,
                        time_label=frames_to_seconds(start, audio.samplerate)
                    )
                )

    return irreg_list

def create_irreg_file(audio_src, video_src) -> IrregularityFile:
    offset = calculate_offset(AudioWave.from_file(audio_src), video_src)
    return IrregularityFile(foo(audio_src), offset=offset)