CoolFace
Datasetpublic

Lyte/DarijaTTS-v0.2

How to Use the DarijaTTS-v0.2 Dataset Code: import IPython.display as ipd import io import numpy as np import tempfile import wave import os from datasets import load_dataset from IPython.display import Audio # Load the DarijaTTS-v0.2 dataset with streaming streaming_dataset = load_dataset("Lyte/DarijaTTS-v0.2", streaming=True) print("Dataset loaded with streaming:") print(streaming_dataset) # Function to play audio from a streaming dataset element def… See the full description on the dataset page: https://huggingface.co/datasets/Lyte/DarijaTTS-v0.2.

sourceHugging Faceupdated 11mo agoView on Hugging Face
1likes93downloads
Dataset Card

How to Use the DarijaTTS-v0.2 Dataset

Code:

python
import IPython.display as ipd
import io
import numpy as np
import tempfile
import wave
import os

from datasets import load_dataset
from IPython.display import Audio

# Load the DarijaTTS-v0.2 dataset with streaming
streaming_dataset = load_dataset("Lyte/DarijaTTS-v0.2", streaming=True)

print("Dataset loaded with streaming:")
print(streaming_dataset)

# Function to play audio from a streaming dataset element
def play_streaming_audio(element, sampling_rate=22050):
    """
    Plays audio bytes from a streaming dataset element by processing the bytes and creating a temporary WAV file.

    Args:
      element: The dataset element containing the audio.
      sampling_rate: The sampling rate to use for the audio playback.
    """
    audio_bytes = element['audio']['bytes']
    temp_filename = None

    try:
        # Convert the bytes to a NumPy array, assuming float64 data based on previous success
        audio_data = np.frombuffer(audio_bytes, dtype=np.float64)

        # Convert float64 to int16 (standard for WAV) and scale
        int16_data = np.int16(audio_data * 32767)

        # Create a temporary WAV file
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_file:
            temp_filename = temp_file.name

            with wave.open(temp_filename, 'wb') as wf:
                wf.setnchannels(1)  # Mono
                wf.setsampwidth(2)  # 16-bit (2 bytes)
                wf.setframerate(sampling_rate)  # Sample rate
                wf.writeframes(int16_data.tobytes())

        # Play the temporary WAV file
        display(Audio(temp_filename, rate=sampling_rate))

    except Exception as e:
        print(f"Error processing and playing audio: {e}")
        print("Could not play audio.")
    finally:
        # Clean up the temporary file if it was created
        if temp_filename and os.path.exists(temp_filename):
            os.remove(temp_filename)

# Function to iterate through and play a number of streaming audio examples
def play_streaming_examples(dataset, num_examples_to_play=5, sampling_rate=22050):
    """
    Iterates through and plays a specified number of audio examples from a streaming dataset.

    Args:
      dataset: The loaded streaming dataset object.
      num_examples_to_play: The number of examples to play.
      sampling_rate: The sampling rate to use for the audio playback.
    """
    print(f"\nPlaying the first {num_examples_to_play} audio examples...")

    for i, element in enumerate(dataset['train']):
        if i >= num_examples_to_play:
            break

        print(f"\nPlaying example {i+1}")
        play_streaming_audio(element, sampling_rate=sampling_rate)

# Example of how to use the play_streaming_examples function
play_streaming_examples(streaming_dataset, num_examples_to_play=3, sampling_rate=22050)