File size: 1,589 Bytes
78cb487
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import soundfile as sf
import sounddevice as sd
from datetime import datetime
from pathlib import Path
from typing import Tuple, Optional


def save_audio_file(
    audio_data: np.ndarray, output_dir: Path, sample_rate: int = 24000
) -> Path:
    """
    Save audio data to a WAV file with a timestamp in the filename.

    Args:
        audio_data (np.ndarray): The audio data to save. Can be a single array or a list of arrays.
        output_dir (Path): The directory to save the audio file in.
        sample_rate (int, optional): The sample rate of the audio data. Defaults to 24000.

    Returns:
        Path: The path to the saved audio file.
    """
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    output_path = output_dir / f"output_{timestamp}.wav"

    if isinstance(audio_data, list):
        audio_data = np.concatenate(audio_data)

    sf.write(str(output_path), audio_data, sample_rate)
    return output_path


def play_audio(
    audio_data: np.ndarray, sample_rate: int = 24000
) -> Tuple[bool, Optional[np.ndarray]]:
    """
    Play audio data using sounddevice.

    Args:
        audio_data (np.ndarray): The audio data to play.
        sample_rate (int, optional): The sample rate of the audio data. Defaults to 24000.

    Returns:
        Tuple[bool, Optional[np.ndarray]]: A tuple containing a boolean indicating if the playback was interrupted (always False here) and an optional numpy array representing the interrupted audio (always None here).
    """
    sd.play(audio_data, sample_rate)
    sd.wait()
    return False, None