main_m.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import sys
  2. sys.path.append('/home/lida/Downloads/lilypond-2.24.1/python')
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtCore import Qt, QThread, pyqtSignal
  5. from PyQt5.QtGui import QPixmap
  6. from PyQt5 import uic
  7. import keyboard
  8. from ly import *
  9. import ly.document
  10. import ly.cli.command
  11. import ly.cli.main
  12. #from ly import ly.transposition ?
  13. from abjad import LilyPondFile, Pitch, Note
  14. import re
  15. class SheetMusicEditor(QDialog):
  16. class KeyboardListener(QThread):
  17. note_added = pyqtSignal(str)
  18. def __init__(self, parent=None):
  19. super().__init__(parent=parent)
  20. self.key_to_note = {
  21. "c": Pitch("c'"),
  22. "d": Pitch("d'"),
  23. "e": Pitch("e'"),
  24. "f": Pitch("f'"),
  25. "g": Pitch("g'"),
  26. "a": Pitch("a'"),
  27. "b": Pitch("b'"),
  28. }
  29. def run(self):
  30. while True:
  31. key = keyboard.read_event().name
  32. if key in self.key_to_note:
  33. note = Note.from_pitch(self.key_to_note[key])
  34. self.note_added.emit(note.to_lilypond() + "")
  35. def __init__(self):
  36. super(SheetMusicEditor, self).__init__()
  37. uic.loadUi("qtdesigner_zkouska.ui", self)
  38. self.keyboard_listener = self.KeyboardListener()
  39. self.keyboard_listener.note_added.connect(self.add_note_to_file)
  40. self.keyboard_listener.start()
  41. self.show()
  42. self.pushButton.clicked.connect(self.transpose_up)
  43. self.pushButton_2.clicked.connect(self.transpose_down)
  44. self.krizek_button.clicked.connect(self.add_krizek_to_file)
  45. self.becko_button.clicked.connect(self.add_becko_to_file)
  46. self.treble_button.setChecked(True)
  47. self.treble_button.toggled.connect(self.clef_changed)
  48. self.alto_button.toggled.connect(self.clef_changed)
  49. self.bass_button.toggled.connect(self.clef_changed)
  50. #self.pushButton_3.clicked.connect(self.make_big)
  51. #self.pushButton_4.clicked.connect(self.make_small)
  52. self.save_button.clicked.connect(self.saveFile)
  53. self.openFile_button.clicked.connect(self.openFile)
  54. self.new_file_button.clicked.connect(self.create_new_file)
  55. self.refresh_button.clicked.connect(self.refresh_sheet)
  56. self.c_button.clicked.connect(lambda: self.add_note_to_file("c'"))
  57. self.d_button.clicked.connect(lambda: self.add_note_to_file("d'"))
  58. self.e_button.clicked.connect(lambda: self.add_note_to_file("e'"))
  59. self.f_button.clicked.connect(lambda: self.add_note_to_file("f'"))
  60. self.g_button.clicked.connect(lambda: self.add_note_to_file("g'"))
  61. self.a_button.clicked.connect(lambda: self.add_note_to_file("a'"))
  62. self.b_button.clicked.connect(lambda: self.add_note_to_file("b'"))
  63. #self.lineEdit(self.change_title)
  64. line_edit = QLineEdit()
  65. line_edit.textChanged.connect(lambda text: change_title(title=text))
  66. #tady takové divné, ten první a třetí řádek spolu neinteragují hezky
  67. self.bpm_slider = QSlider(Qt.Horizontal)
  68. self.bpm_slider.setMinimum(1)
  69. self.bpm_slider.setMaximum(10000)
  70. self.bpm_slider.setSingleStep(5)
  71. self.bpm_slider.setValue(120)
  72. self.bpm_slider.setTickInterval(30)
  73. self.bpm_slider.setTickPosition(QSlider.TicksBelow)
  74. self.bpm_slider.valueChanged.connect(self.bpm_changed)
  75. self.horizontalLayout_2.addWidget(self.bpm_slider)
  76. self.actionClose.triggered.connect(exit)
  77. def transpose_up(self):
  78. pass
  79. #self.pitch = transposition.transpose_pitch(self.pitch, "1")
  80. def transpose_down(self):
  81. pass
  82. #self.pitch = transposition.transpose_pitch(self.pitch, "-1")
  83. def add_becko_to_file(self):
  84. # Get current text in music editor
  85. current_text = self.musicEdit.toPlainText()
  86. # Add znaminko to music editor
  87. new_text = current_text.rstrip() + "{ \flat }" + " "
  88. # Set new text in music editor
  89. self.musicEdit.setPlainText(new_text)
  90. def add_krizek_to_file(self):
  91. # Get current text in music editor
  92. current_text = self.musicEdit.toPlainText()
  93. # Add znaminko to music editor
  94. new_text = current_text.rstrip() + "{ \sharp }" + " "
  95. # Set new text in music editor
  96. self.musicEdit.setPlainText(new_text)
  97. def bpm_changed(slider_val, self):
  98. bpm = slider_val
  99. with open("test.ly", "r+") as file:
  100. data = file.read()
  101. file.seek(0)
  102. file.truncate()
  103. parameter = f"\\override Score.MetronomeMark #'stencil = ##f \\override Score.MetronomeMark #'break-visibility = ##(#f #f #f) \\tempo {bpm}"
  104. data = re.sub(r'\\override Score\.MetronomeMark #\'stencil = ##f \\override Score\.MetronomeMark #\'break-visibility = ##\(#f #f #f\) \\tempo \d+', parameter, data)
  105. file.write(data)
  106. def clef_changed(self):
  107. if self.alto_button.isChecked():
  108. clef = 'alto'
  109. elif self.bass_button.isChecked():
  110. clef = 'bass'
  111. else:
  112. clef = 'treble'
  113. with open("new_file.ly", "r") as f:
  114. code = f.read()
  115. # Replace the clef in the LilyPond code
  116. code = re.sub(r'\\clef\s+\w+', f'\\clef {clef}', code)
  117. # Write the modified LilyPond code to a new file
  118. with open("new_file.ly", "w") as f:
  119. f.write(code)
  120. def add_note_to_file(self, note):
  121. # Get current text in music editor
  122. current_text = self.musicEdit.toPlainText()
  123. # Add note to music editor
  124. new_text = current_text + note + " "
  125. # Set new text in music editor
  126. self.musicEdit.setPlainText(new_text)
  127. def create_new_file(self):
  128. with open("new_file.ly", "w") as f:
  129. f.write("\\version \"2.18.2\"\n\n")
  130. f.write(f'\\clef {self.clef}\n')
  131. f.write("\\header {\n")
  132. f.write("\ttitle = \"Untitled\"\n")
  133. f.write("}\n\n")
  134. f.write("\\score {\n")
  135. f.write("\t\\new Staff { }\n")
  136. f.write("\t\\layout { }\n")
  137. f.write("}\n")
  138. self.pitch = Pitch("c'")
  139. def change_title(self, new_title):
  140. new_title = self.lineEdit.text()
  141. with open("new_file.ly", "r") as f:
  142. code = f.read()
  143. # Replace the title in the LilyPond code
  144. code = re.sub("\ttitle = \"Untitled\"\n", f"\ttitle = \"{new_title}\"\n", code)
  145. # Write the modified LilyPond code to a new file
  146. with open("new_file.ly", "w") as f:
  147. f.write(code)
  148. def refresh_sheet(self):
  149. # Get current text in music editor
  150. current_text = self.musicEdit.toPlainText()
  151. # Create LilyPond file
  152. lilypond_file = LilyPondFile()
  153. lilypond_file.add_item(current_text)
  154. # Create PNG file
  155. png_filename = "sheet_music.png"
  156. lilypond_file.to_pdf(png_filename)
  157. # Load PNG file and display it in the graphics view
  158. pixmap = QPixmap(png_filename)
  159. self.graphicsView.setScene(QGraphicsScene(self))
  160. self.graphicsView.scene().addPixmap(pixmap)
  161. self.graphicsView.fitInView(self.graphicsView.scene().sceneRect(), Qt.KeepAspectRatio)
  162. def saveFile(self):
  163. # Get filename from user
  164. filename, _ = QFileDialog.getSaveFileName(self, "Save Sheet Music", "", "Lilypond Files (*.ly)")
  165. if not filename:
  166. return
  167. # Create LilyPond file
  168. lilypond_file = LilyPondFile()
  169. lilypond_file.header_title = self.titleEdit.toPlainText()
  170. lilypond_file.add_item(self.musicEdit.toPlainText())
  171. # Save LilyPond file
  172. with open(filename, 'w') as f:
  173. f.write(lilypond_file)
  174. def openFile(self):
  175. # Get filename from user
  176. filename, _ = QFileDialog.getOpenFileName(self, "Open Sheet Music", "", "Lilypond Files (*.ly)")
  177. if not filename:
  178. return
  179. # Load LilyPond file
  180. with open(filename, 'r') as f:
  181. lilypond_text = f.read()
  182. # Parse LilyPond file
  183. lilypond_file = LilyPondFile()
  184. lilypond_file.parse(lilypond_text)
  185. # Set widget values
  186. self.titleEdit.setPlainText(lilypond_file.header_title)
  187. self.musicEdit.setPlainText(lilypond_file.items()[0].to_lilypond())
  188. if __name__ == '__main__':
  189. app = QApplication(sys.argv)
  190. editor = SheetMusicEditor()
  191. editor.show()
  192. sys.exit(app.exec_())