Spaces:
Sleeping
Sleeping
import pygame | |
import mido | |
from gtts import gTTS | |
from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_RETURN | |
import time | |
# Initialize the click count and the time of the last click | |
click_count = 0 | |
last_click_time = 0 | |
# Initialize Pygame and create a window for the MIDI editor | |
pygame.init() | |
screen = pygame.display.set_mode((640, 480)) | |
class MidiNote: | |
def __init__(self, note, velocity, time, x, y, width, height): | |
self.note = note | |
self.velocity = velocity | |
self.time = time | |
self.x = x | |
self.y = y | |
self.width = width | |
self.height = height | |
# Create a new MIDI file | |
mid = mido.MidiFile() | |
# Create a new MIDI track | |
track = mido.MidiTrack() | |
# Add the MIDI track to the file | |
mid.tracks.append(track) | |
# Create a function to add lyrics to a specific MIDI note | |
def add_lyrics(note, lyrics): | |
# Add the lyrics to the MIDI note | |
note.text = lyrics | |
# Update the MIDI file with the new lyrics | |
mid.save("song.mid") | |
# Create a function to get the MIDI note that was clicked on | |
def get_clicked_note(pos): | |
# Iterate through the MIDI notes in the file | |
for track in mid.tracks: | |
for note in track: | |
if isinstance(note, mido.Message): | |
# Check if the mouse position is within the bounds of the MIDI note | |
if pos[0] > note.x and pos[0] < note.x + note.width: | |
if pos[1] > note.y and pos[1] < note.y + note.height: | |
return note | |
return None | |
# Create a function to convert the lyrics from Japanese to speech | |
def speak_lyrics(lyrics): | |
tts = gTTS(lyrics, lang='ja') | |
tts.save('lyrics.mp3') | |
pygame.mixer.music.load('lyrics.mp3') | |
pygame.mixer.music.play() | |
# Main loop | |
while True: | |
for event in pygame.event.get(): | |
if event.type == MOUSEBUTTONDOWN: | |
# Increment the click count | |
click_count += 1 | |
# Check if the user double-clicked on a MIDI note | |
if time.time() - last_click_time < 0.5: | |
# Get the MIDI note that was clicked on | |
note = get_clicked_note(event.pos) | |
# Add the lyrics to the MIDI note | |
add_lyrics(note, lyrics) | |
# Reset the click count | |
click_count = 0 | |
# Update the time of the last click | |
last_click_time = time.time() | |
if event.type == KEYDOWN: | |
if event.key == K_RETURN: | |
# Get the lyrics from the input field | |
lyrics = input_field.get_text() | |
# Convert the lyrics to speech and play them | |
speak_lyrics(lyrics) | |
# If the click count is not reset, it means that the user has single-clicked | |
if click_count == 1: | |
# Get the position of the single click | |
pos = pygame.mouse.get_pos() | |
# Create a new MIDI note with the specified position and length | |
note = MidiNote(60, 64, 0, event.pos[0], event.pos[1], 100, 100) | |
note.x = pos[0] | |
note.y = pos[1] | |
note.width = 100 | |
note.height = 100 | |
# Add the MIDI note to the track | |
track.append(note) | |
mid.save("song.mid") | |
# Reset the click count | |
click_count = 0 | |
lyrics = "" | |
input_field = pygame.font.Font(None, 32).render(lyrics, True, (0, 0, 0)) | |
# Display the input field on the window | |
screen.blit(input_field, (10, 10)) | |
pygame.display.flip() | |