|
import pygame |
|
import mido |
|
from gtts import gTTS |
|
from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_RETURN |
|
import time |
|
|
|
|
|
click_count = 0 |
|
last_click_time = 0 |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
mid = mido.MidiFile() |
|
|
|
|
|
track = mido.MidiTrack() |
|
|
|
|
|
mid.tracks.append(track) |
|
|
|
|
|
def add_lyrics(note, lyrics): |
|
|
|
note.text = lyrics |
|
|
|
mid.save("song.mid") |
|
|
|
|
|
def get_clicked_note(pos): |
|
|
|
for track in mid.tracks: |
|
for note in track: |
|
if isinstance(note, mido.Message): |
|
|
|
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 |
|
|
|
|
|
def speak_lyrics(lyrics): |
|
tts = gTTS(lyrics, lang='ja') |
|
tts.save('lyrics.mp3') |
|
pygame.mixer.music.load('lyrics.mp3') |
|
pygame.mixer.music.play() |
|
|
|
|
|
|
|
while True: |
|
for event in pygame.event.get(): |
|
if event.type == MOUSEBUTTONDOWN: |
|
|
|
click_count += 1 |
|
|
|
if time.time() - last_click_time < 0.5: |
|
|
|
note = get_clicked_note(event.pos) |
|
|
|
add_lyrics(note, lyrics) |
|
|
|
click_count = 0 |
|
|
|
last_click_time = time.time() |
|
if event.type == KEYDOWN: |
|
if event.key == K_RETURN: |
|
|
|
lyrics = input_field.get_text() |
|
|
|
speak_lyrics(lyrics) |
|
|
|
if click_count == 1: |
|
|
|
pos = pygame.mouse.get_pos() |
|
|
|
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 |
|
|
|
track.append(note) |
|
mid.save("song.mid") |
|
|
|
click_count = 0 |
|
lyrics = "" |
|
|
|
input_field = pygame.font.Font(None, 32).render(lyrics, True, (0, 0, 0)) |
|
|
|
|
|
screen.blit(input_field, (10, 10)) |
|
pygame.display.flip() |
|
|
|
|
|
|