source: ammosreader/sample_scripts/ammos_viewer.py@ 0033a40

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

allow offsets other than multiples of 8

  • Property mode set to 100644
File size: 4.1 KB
Line 
1"""I provide a simple bit viewer specialized for AMMOS files."""
2
3import tkinter as tk
4from PIL import Image, ImageTk
5from random import randbytes
6import bitstring
7
8from ammosreader.AmmosAudioReader import AmmosAudioReader
9
10
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"
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(500000000)
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("L", 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 bytes').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 if self.offset.get() % 8 == 0:
79 return b"".join([self.bytes[each: each+self.canvas_size[0]] for each in range(self.offset.get(),
80 len(self.bytes),
81 self.cycle.get())])
82 bits = bitstring.BitArray(self.bytes)
83 new_bytes = bits[self.offset.get()*8:].tobytes()
84 return b"".join([new_bytes[each: each+self.canvas_size[0]] for each in range(0,
85 len(new_bytes),
86 self.cycle.get())])
87
88 def update_canvas(self):
89 self.image = ImageTk.PhotoImage(Image.frombytes("L", self.canvas_size, self.current_bytes()))
90 self.canvas.itemconfig(self.image_id, image=self.image)
91
92 def load_file(self):
93 pass
94
95 def quit(self):
96 self.parent.destroy()
97
98
99if __name__ == '__main__':
100 root = tk.Tk()
101 root.geometry("1920x1200")
102
103 app = MainApplication(root)
104
105 root.mainloop()
Note: See TracBrowser for help on using the repository browser.