1 | """I provide a simple bit viewer specialized for AMMOS files."""
|
---|
2 |
|
---|
3 | import tkinter as tk
|
---|
4 | from PIL import Image, ImageTk
|
---|
5 | # from random import randbytes
|
---|
6 | import bitstring
|
---|
7 |
|
---|
8 | from ammosreader.AmmosAudioReader import AmmosAudioReader
|
---|
9 |
|
---|
10 |
|
---|
11 | class 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"
|
---|
20 | self.ammos_container = AmmosAudioReader(self.file_name).read_all_frames_left()
|
---|
21 | self.file = open(self.file_name, 'rb')
|
---|
22 | self.bytes = self.file.read(-1)
|
---|
23 | self.cycle = tk.IntVar()
|
---|
24 | self.cycle.set(self.ammos_container.unique_frame_sizes()[0])
|
---|
25 | self.cache = tk.IntVar()
|
---|
26 | self.cache.set(500)
|
---|
27 | self.canvas_size = (800, 600)
|
---|
28 | self.offset = tk.IntVar()
|
---|
29 | self.offset.set(0)
|
---|
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)
|
---|
39 | self.canvas = tk.Canvas(image_frame, bg="#000000", width=self.canvas_size[0], height=self.canvas_size[1])
|
---|
40 | self.canvas.pack()
|
---|
41 | pil_image = Image.frombytes("1", self.canvas_size, self.current_bytes())
|
---|
42 | self.image = ImageTk.PhotoImage(pil_image)
|
---|
43 | # pil_image.show()
|
---|
44 | button_frame = tk.Frame(self)
|
---|
45 | self.image_id = self.canvas.create_image(0, 0, anchor='nw', image=self.image)
|
---|
46 | button_frame.pack(side="right")
|
---|
47 |
|
---|
48 | tk.Label(button_frame, text='Cycle in bytes').pack(side=tk.TOP)
|
---|
49 | self.cycle_entry = tk.Entry(button_frame, text=self.cycle)
|
---|
50 | self.cycle_entry.pack(side=tk.TOP)
|
---|
51 |
|
---|
52 | tk.Label(button_frame, text='Cache in Megabytes').pack(side=tk.TOP)
|
---|
53 | self.cache_entry = tk.Entry(button_frame, text=self.cache)
|
---|
54 | self.cache_entry.pack(side=tk.TOP)
|
---|
55 |
|
---|
56 | tk.Label(button_frame, text='Offset in bytes').pack(side=tk.TOP)
|
---|
57 | self.offset_entry = tk.Entry(button_frame, text=self.offset)
|
---|
58 | self.offset_entry.pack(side=tk.TOP)
|
---|
59 |
|
---|
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 |
|
---|
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 |
|
---|
77 | def current_bytes(self):
|
---|
78 | """I return the current bytes to display in the canvas."""
|
---|
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)
|
---|
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())])
|
---|
88 |
|
---|
89 | def update_canvas(self):
|
---|
90 | """I update the canvas."""
|
---|
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 |
|
---|
94 | def load_file(self):
|
---|
95 | """I load a binary ammos file."""
|
---|
96 | pass
|
---|
97 |
|
---|
98 | def quit(self):
|
---|
99 | """I quit the application."""
|
---|
100 | self.parent.destroy()
|
---|
101 |
|
---|
102 |
|
---|
103 | if __name__ == '__main__':
|
---|
104 | root = tk.Tk()
|
---|
105 | root.geometry("1920x1200")
|
---|
106 |
|
---|
107 | app = MainApplication(root)
|
---|
108 |
|
---|
109 | root.mainloop()
|
---|