source: ammosreader/sample_scripts/ammos_viewer.py@ d8483f7

AmmosSource guix
Last change on this file since d8483f7 was dbcb255, checked in by Enrico Schwass <ennoausberlin@…>, 3 years ago

ammosviewer minor changes

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[bcacddc]1"""I provide a simple bit viewer specialized for AMMOS files."""
2
3import tkinter as tk
4from PIL import Image, ImageTk
[dbcb255]5# from random import randbytes
[d2f9280]6import bitstring
[bcacddc]7
[577ce87]8from ammosreader.AmmosAudioReader import AmmosAudioReader
9
[d2f9280]10
[bcacddc]11class MainApplication(tk.Frame):
12 """I implement a simple bit viewer specialized for AMMOS files."""
13
14 def __init__(self, parent):
15 """I create the application window."""
16 super().__init__(parent)
17
18 self.parent = parent
19 self.file_name = "../sample_data/audio_data_sample.bin"
[577ce87]20 self.ammos_container = AmmosAudioReader(self.file_name).read_all_frames_left()
[bcacddc]21 self.file = open(self.file_name, 'rb')
[577ce87]22 self.bytes = self.file.read(-1)
[09b290d]23 self.cycle = tk.IntVar()
24 self.cycle.set(self.ammos_container.unique_frame_sizes()[0])
25 self.cache = tk.IntVar()
[dbcb255]26 self.cache.set(500)
[577ce87]27 self.canvas_size = (800, 600)
[09b290d]28 self.offset = tk.IntVar()
29 self.offset.set(0)
[bcacddc]30 self.buildup()
31
32 def buildup(self):
33 """I build up the initial user interface."""
34 self.parent.title("File name:" + self.file_name + " Size:" + str(len(self.bytes)))
35 self.pack(fill="both", expand=True)
36 self.default_button_width = 10
37 image_frame = tk.Frame(self)
38 image_frame.pack(side=tk.LEFT)
[577ce87]39 self.canvas = tk.Canvas(image_frame, bg="#000000", width=self.canvas_size[0], height=self.canvas_size[1])
[bcacddc]40 self.canvas.pack()
[dbcb255]41 pil_image = Image.frombytes("1", self.canvas_size, self.current_bytes())
[577ce87]42 self.image = ImageTk.PhotoImage(pil_image)
43 # pil_image.show()
[bcacddc]44 button_frame = tk.Frame(self)
[09b290d]45 self.image_id = self.canvas.create_image(0, 0, anchor='nw', image=self.image)
[bcacddc]46 button_frame.pack(side="right")
47
[09b290d]48 tk.Label(button_frame, text='Cycle in bytes').pack(side=tk.TOP)
49 self.cycle_entry = tk.Entry(button_frame, text=self.cycle)
[bcacddc]50 self.cycle_entry.pack(side=tk.TOP)
51
[dbcb255]52 tk.Label(button_frame, text='Cache in Megabytes').pack(side=tk.TOP)
[09b290d]53 self.cache_entry = tk.Entry(button_frame, text=self.cache)
[bcacddc]54 self.cache_entry.pack(side=tk.TOP)
55
[09b290d]56 tk.Label(button_frame, text='Offset in bytes').pack(side=tk.TOP)
57 self.offset_entry = tk.Entry(button_frame, text=self.offset)
[bcacddc]58 self.offset_entry.pack(side=tk.TOP)
59
[09b290d]60 tk.Button(button_frame,
61 text="Update",
62 width=self.default_button_width,
63 command=self.update_canvas).pack(side=tk.TOP, fill=tk.BOTH, expand=True)
64
[bcacddc]65 tk.Button(
66 button_frame,
67 text="Load file",
68 width=self.default_button_width,
69 command=self.load_file).pack(side="top", fill="both", expand=True)
70
71 tk.Button(
72 button_frame,
73 text="Quit",
74 width=self.default_button_width,
75 command=self.quit).pack(side="top", fill="both", expand=True)
76
[09b290d]77 def current_bytes(self):
[dbcb255]78 """I return the current bytes to display in the canvas."""
[d2f9280]79 if self.offset.get() % 8 == 0:
80 return b"".join([self.bytes[each: each+self.canvas_size[0]] for each in range(self.offset.get(),
81 len(self.bytes),
82 self.cycle.get())])
83 bits = bitstring.BitArray(self.bytes)
[0033a40]84 new_bytes = bits[self.offset.get()*8:].tobytes()
85 return b"".join([new_bytes[each: each+self.canvas_size[0]] for each in range(0,
86 len(new_bytes),
87 self.cycle.get())])
[d2f9280]88
[09b290d]89 def update_canvas(self):
[dbcb255]90 """I update the canvas."""
[09b290d]91 self.image = ImageTk.PhotoImage(Image.frombytes("L", self.canvas_size, self.current_bytes()))
92 self.canvas.itemconfig(self.image_id, image=self.image)
93
[bcacddc]94 def load_file(self):
[dbcb255]95 """I load a binary ammos file."""
[bcacddc]96 pass
97
98 def quit(self):
[dbcb255]99 """I quit the application."""
[bcacddc]100 self.parent.destroy()
101
102
103if __name__ == '__main__':
104 root = tk.Tk()
105 root.geometry("1920x1200")
106
107 app = MainApplication(root)
108
109 root.mainloop()
Note: See TracBrowser for help on using the repository browser.