[bcacddc] | 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
|
---|
[d2f9280] | 6 | import bitstring
|
---|
[bcacddc] | 7 |
|
---|
[577ce87] | 8 | from ammosreader.AmmosAudioReader import AmmosAudioReader
|
---|
| 9 |
|
---|
[d2f9280] | 10 |
|
---|
[bcacddc] | 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"
|
---|
[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()
|
---|
| 26 | self.cache.set(500000000)
|
---|
[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()
|
---|
[09b290d] | 41 | pil_image = Image.frombytes("L", 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 |
|
---|
| 52 | tk.Label(button_frame, text='Cache in bytes').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):
|
---|
[d2f9280] | 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)
|
---|
[0033a40] | 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())])
|
---|
[d2f9280] | 87 |
|
---|
[09b290d] | 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 |
|
---|
[bcacddc] | 92 | def load_file(self):
|
---|
| 93 | pass
|
---|
| 94 |
|
---|
| 95 | def quit(self):
|
---|
| 96 | self.parent.destroy()
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | if __name__ == '__main__':
|
---|
| 100 | root = tk.Tk()
|
---|
| 101 | root.geometry("1920x1200")
|
---|
| 102 |
|
---|
| 103 | app = MainApplication(root)
|
---|
| 104 |
|
---|
| 105 | root.mainloop()
|
---|