Advertisement
EvilSupahFly

Python version of the GRUB2 Music Maker

Apr 6th, 2024 (edited)
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | Source Code | 0 0
  1. # grub2_music_maker.py
  2.  
  3. import sys
  4.  
  5. def midi_to_notes(file_name):
  6.     with open(file_name, "rb") as file:
  7.         midi_data = file.read()
  8.  
  9.     notes = []
  10.     for i in range(len(midi_data)):
  11.         notes.append(str(midi_data[i]))
  12.  
  13.     return " ".join(notes)
  14.  
  15. def play_notes(notes):
  16.     for note in notes.split():
  17.         sys.stdout.write("\x07")  # Beep through the PC speaker
  18.         sys.stdout.flush()
  19.         time.sleep(0.1)
  20.  
  21. def save_to_grub(notes):
  22.     with open("/etc/default/grub", "r") as file:
  23.         lines = file.readlines()
  24.    
  25.     for i in range(len(lines)):
  26.         if "GRUB_INIT_TUNE=" in lines[i]:
  27.             lines[i] = "GRUB_INIT_TUNE=\"" + notes + "\"\n"
  28.    
  29.     with open("/etc/default/grub", "w") as file:
  30.         file.writelines(lines)
  31.  
  32. if __name__ == "__main__":
  33.     file_name = sys.argv[1]
  34.     notes = midi_to_notes(file_name)
  35.    
  36.     choice = input("Do you want to play the notes (p) or save them to GRUB (g)? ")
  37.    
  38.     if choice == "p":
  39.         play_notes(notes)
  40.     elif choice == "g":
  41.         save_to_grub(notes)
  42.         print("Notes saved to GRUB. Please run 'sudo update-grub' to apply changes.")
  43.     else:
  44.         print("Invalid choice.")
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement