import sounddevice as sd
import numpy as np
import re
import time

# Function to map a word to a frequency in the given range
def word_to_frequency(word, min_frequency, max_frequency):
    # Assign each letter a value within the range
    letter_values = {letter: min_frequency + i for i, letter in enumerate("abcdefghijklmnopqrstuvwxyz")}
    
    # Calculate the frequency of the word based on the values of its letters
    frequency = sum(letter_values.get(letter, 0) for letter in word.lower())
    
    # Normalize frequency to be within the given range
    normalized_frequency = ((frequency - min(letter_values.values())) / (max(letter_values.values()) - min(letter_values.values()))) * (max_frequency - min_frequency) + min_frequency
    
    return normalized_frequency

# Function to generate note
def generate_note(frequency, duration, sample_rate=44100):
    t = np.linspace(0, duration, int(duration * sample_rate), endpoint=False)
    waveform = np.sin(2 * np.pi * frequency * t)
    return waveform

# Function to play note
def play_note(frequency, duration, sample_rate=44100):
    waveform = generate_note(frequency, duration, sample_rate)
    sd.play(waveform, samplerate=sample_rate)
    time.sleep(duration)  # Wait for the note to finish playing

# Function to play music from text
def play_music_from_text(text, word_duration=0.2, space_duration=0.3, min_frequency=85, max_frequency=180):
    words = re.findall(r'\b\w+\b', text)  # Extract words from text
    for word in words:
        frequency = word_to_frequency(word, min_frequency, max_frequency)
        play_note(frequency, len(word) * word_duration)
        time.sleep(space_duration)  # Add space between words

# Example usage: Generate music from text (longer version of Bible Genesis)
bible_text = """
In the beginning God created the heavens and the earth. Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters.
And God said, “Let there be light,” and there was light. God saw that the light was good, and he separated the light from the darkness. God called the light “day,” and the darkness he called “night.” And there was evening, and there was morning—the first day.
And God said, “Let there be a vault between the waters to separate water from water.” So God made the vault and separated the water under the vault from the water above it. And it was so. God called the vault “sky.” And there was evening, and there was morning—the second day.
And God said, “Let the water under the sky be gathered to one place, and let dry ground appear.” And it was so. God called the dry ground “land,” and the gathered waters he called “seas.” And God saw that it was good.
Then God said, “Let the land produce vegetation: seed-bearing plants and trees on the land that bear fruit with seed in it, according to their various kinds.” And it was so. The land produced vegetation: plants bearing seed according to their kinds and trees bearing fruit with seed in it according to their kinds. And God saw that it was good. And there was evening, and there was morning—the third day.
"""
play_music_from_text(bible_text)
