WordPress手机站插件,成都网站建设公司有哪些,一个网站建设大概需要多少费用,ps做网站大小用python制作88键赛博钢琴
前言 恭喜这位博主终于想起了自己的账号密码#xff01; 时光荏苒#xff0c;转眼间已逾一年未曾在此留下墨香。尽管这一年间#xff0c;博主投身于无尽的忙碌与挑战之中#xff0c;但令人欣慰的是#xff0c;那份初心与热情似乎并未因岁月的流…用python制作88键赛博钢琴
前言 恭喜这位博主终于想起了自己的账号密码 时光荏苒转眼间已逾一年未曾在此留下墨香。尽管这一年间博主投身于无尽的忙碌与挑战之中但令人欣慰的是那份初心与热情似乎并未因岁月的流转而有丝毫减退依旧保持着与往昔相同的热情与活力。 提及趣事前不久博主精心筹备欲在女友生辰之际以一份特别的礼物——一台37键的童趣钢琴为她编织一段温馨的记忆。怎料这份心意与紧随其后的七夕佳节完美邂逅却因工作的突然召唤让博主不得不带着遗憾踏上异乡的征途错过了亲自弹奏《两只老虎》的温馨时刻。 望着视频中女友指尖跳跃旋律悠扬那份未能亲临现场的遗憾化作了创新的火花。博主灵机一动决定跨越千山万水用指尖下的键盘在数字世界中续写音乐的浪漫。说干就干经过一番不懈的努力与探索几个小时后一个别出心裁的“键盘钢琴”奇迹般地诞生了 请允许我们一同见证这创意的结晶。
效果图 功能实现 使用一个JSON文件作为核心来控制整体界面布局、每个键对应的mp3文件、简谱标识、键盘映射等。 使用PyQt5实现界面绘制。 使用pygame库播放音乐会更加流畅、连贯。 使用keyboard实现键盘监控。 使用Thread多线程防止pygame和PyQt5线程冲突 最终实现的功能很简单鼠标点击或键盘敲击对应的键即可进行弹奏。
源代码
import sys
import keyboard
import pygame
import jsonfrom threading import Thread
from PyQt5.QtWidgets import QMainWindow, QWidget, QPushButton, QApplication, QLabel# 读取数据文件
piano_key json.load(open(JSON/piano_key.json, r, encodingutf8))# 主窗口
class MainWindow(QMainWindow):def __init__(self, parentNone):super(MainWindow, self).__init__(parent)# 获取桌面尺寸desktop QApplication.desktop()screen_rect desktop.screenGeometry()# 设置主窗口比例main_width int(screen_rect.width() * 0.9)main_height int(screen_rect.height() * 0.4)self.resize(main_width, main_height)# 固定窗口大小self.setFixedSize(self.width(), self.height())# 窗口居中self.move((screen_rect.width() - main_width) // 2, (screen_rect.height() - main_height) // 2)# 状态栏和标题self.status self.statusBar()self.status.showMessage(不是88键买不起而是赛博钢琴更有性价比)self.setWindowTitle(赛博钢琴)# 创建容器存放琴键container QWidget(self)self.setCentralWidget(container)# 遍历查询黑白键的数量用于计算每个键宽度black_key_num sum(1 for key in piano_key if s in key[sound])white_key_num len(piano_key) - black_key_numself.buttons []button_width main_width / white_key_numwhite_key_index 0for index, key in enumerate(piano_key):button QPushButton(container)button.setObjectName(key[sound])button.clicked.connect(self.on_button_clicked)self.set_button_style(button, s in key[sound])if s in key[sound]:button.resize(button_width * 0.8, main_height * 0.6)button.move((white_key_index - 1) * button_width button_width * 0.6, 0)button.raise_()else:button.resize(button_width, main_height)button.move(white_key_index * button_width, 0)button.lower()white_key_index 1self.add_label(container, key, white_key_index, button_width, main_height)self.buttons.append(button)# 匹配并添加labeldef add_label(self, container, key, white_key_index, button_width, main_height):label_map {a: 6, A: 6,b: 7, B: 7,c: 1, C: 1,d: 2, D: 2,e: 3, E: 3,f: 4, F: 4,g: 5, G: 5}label_text label_map.get(key[sound][0], ) f\n{key[key]}label QLabel(label_text, container)label.move(white_key_index * button_width - button_width * 0.5, main_height - 60)# 初始化黑白键样式def set_button_style(self, button, is_black_key):if is_black_key:button.setStyleSheet(QPushButton {background-color: black;color: white;border: 1px solid black;padding: 0;margin: 0;text-align: center;}QPushButton::hover {background-color: lightgray;}QPushButton:pressed {background-color: gray;})else:button.setStyleSheet(QPushButton {background-color: white;color: black;border: 1px solid black;padding: 0;margin: 0;text-align: center;}QPushButton::hover {background-color: lightgray;}QPushButton:pressed {background-color: gray;})# 键盘按下改变样式def change_button_color(self, index):self.buttons[index].setStyleSheet(background-color: gray;)# 抬起后恢复样式def release_button_color(self, index, is_black_key):self.set_button_style(self.buttons[index], is_black_key)# 鼠标点击播放def on_button_clicked(self):button self.sender()pygame.mixer.Sound(MP3/ button.objectName()).play()# 初始化 PyQt 应用
app QApplication(sys.argv)
# 实例化窗口
form MainWindow()# 键盘按下触发
def on_action(event):try:sound next(item[sound] for item in piano_key if item[key] event.name)index next(index for index, item in enumerate(piano_key) if item[key] event.name)if event.event_type keyboard.KEY_DOWN:pygame.mixer.Sound(MP3/ sound).play()form.change_button_color(index)elif event.event_type keyboard.KEY_UP:form.release_button_color(index, s in sound)except StopIteration:print(fNo sound file found for key: {event.name})# 键盘监听
def start_keyboard_listener():keyboard.hook(on_action)keyboard.wait()def main():# 显示窗口form.show()# 初始化 Pygame 混音器pygame.mixer.init()# 启动键盘监听线程listener_thread Thread(targetstart_keyboard_listener)listener_thread.daemon Truelistener_thread.start()# 进入事件循环sys.exit(app.exec_())if __name__ __main__:main()
未来功能扩展
1.自定义功能用户可以自定义每个琴键对应的键盘值并保存这也是我使用JSON文件控制整体的原因。
2.丰富标识目前琴键上只有简谱的标识后续会添加Do、Ra、C4、D4等标识。
3.自动弹奏用户可以以某种方式将谱子录入或导入程序根据谱子自动弹奏。
4.边弹边记开启后接下来的一段弹奏会以乐谱的形式保存下来。
5.趣味玩法可能会像节奏大师那样
6.有奇思妙想的兄弟可以评论区或私信告诉我我可能会将它实现并放到下一篇博客中。
结束语 在创意与爱的交织下这不仅仅是一段代码的展现更是一次心灵的触碰与跨越。期待未来能在CSDN这片沃土上继续播种灵感收获更多温馨与惊喜。每一次的回归都是新旅程的开始愿与每一位读者共享知识的盛宴。