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