[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
|
---|
| 6 |
|
---|
[577ce87] | 7 | from ammosreader.AmmosAudioReader import AmmosAudioReader
|
---|
| 8 |
|
---|
[bcacddc] | 9 | class MainApplication(tk.Frame):
|
---|
| 10 | """I implement a simple bit viewer specialized for AMMOS files."""
|
---|
| 11 |
|
---|
| 12 | def __init__(self, parent):
|
---|
| 13 | """I create the application window."""
|
---|
| 14 | super().__init__(parent)
|
---|
| 15 |
|
---|
| 16 | self.parent = parent
|
---|
| 17 | self.file_name = "../sample_data/audio_data_sample.bin"
|
---|
[577ce87] | 18 | self.ammos_container = AmmosAudioReader(self.file_name).read_all_frames_left()
|
---|
[bcacddc] | 19 | self.file = open(self.file_name, 'rb')
|
---|
[577ce87] | 20 | self.bytes = self.file.read(-1)
|
---|
[09b290d] | 21 | self.cycle = tk.IntVar()
|
---|
| 22 | self.cycle.set(self.ammos_container.unique_frame_sizes()[0])
|
---|
| 23 | self.cache = tk.IntVar()
|
---|
| 24 | self.cache.set(500000000)
|
---|
[577ce87] | 25 | self.canvas_size = (800, 600)
|
---|
[09b290d] | 26 | self.offset = tk.IntVar()
|
---|
| 27 | self.offset.set(0)
|
---|
[bcacddc] | 28 | self.buildup()
|
---|
| 29 |
|
---|
| 30 | def buildup(self):
|
---|
| 31 | """I build up the initial user interface."""
|
---|
| 32 | self.parent.title("File name:" + self.file_name + " Size:" + str(len(self.bytes)))
|
---|
| 33 | self.pack(fill="both", expand=True)
|
---|
| 34 | self.default_button_width = 10
|
---|
| 35 | image_frame = tk.Frame(self)
|
---|
| 36 | image_frame.pack(side=tk.LEFT)
|
---|
[577ce87] | 37 | self.canvas = tk.Canvas(image_frame, bg="#000000", width=self.canvas_size[0], height=self.canvas_size[1])
|
---|
[bcacddc] | 38 | self.canvas.pack()
|
---|
[09b290d] | 39 | pil_image = Image.frombytes("L", self.canvas_size, self.current_bytes())
|
---|
[577ce87] | 40 | self.image = ImageTk.PhotoImage(pil_image)
|
---|
| 41 | # pil_image.show()
|
---|
[bcacddc] | 42 | button_frame = tk.Frame(self)
|
---|
[09b290d] | 43 | self.image_id = self.canvas.create_image(0, 0, anchor='nw', image=self.image)
|
---|
[bcacddc] | 44 | button_frame.pack(side="right")
|
---|
| 45 |
|
---|
[09b290d] | 46 | tk.Label(button_frame, text='Cycle in bytes').pack(side=tk.TOP)
|
---|
| 47 | self.cycle_entry = tk.Entry(button_frame, text=self.cycle)
|
---|
[bcacddc] | 48 | self.cycle_entry.pack(side=tk.TOP)
|
---|
| 49 |
|
---|
| 50 | tk.Label(button_frame, text='Cache in bytes').pack(side=tk.TOP)
|
---|
[09b290d] | 51 | self.cache_entry = tk.Entry(button_frame, text=self.cache)
|
---|
[bcacddc] | 52 | self.cache_entry.pack(side=tk.TOP)
|
---|
| 53 |
|
---|
[09b290d] | 54 | tk.Label(button_frame, text='Offset in bytes').pack(side=tk.TOP)
|
---|
| 55 | self.offset_entry = tk.Entry(button_frame, text=self.offset)
|
---|
[bcacddc] | 56 | self.offset_entry.pack(side=tk.TOP)
|
---|
| 57 |
|
---|
[09b290d] | 58 | tk.Button(button_frame,
|
---|
| 59 | text="Update",
|
---|
| 60 | width=self.default_button_width,
|
---|
| 61 | command=self.update_canvas).pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
---|
| 62 |
|
---|
[bcacddc] | 63 | tk.Button(
|
---|
| 64 | button_frame,
|
---|
| 65 | text="Load file",
|
---|
| 66 | width=self.default_button_width,
|
---|
| 67 | command=self.load_file).pack(side="top", fill="both", expand=True)
|
---|
| 68 |
|
---|
| 69 | tk.Button(
|
---|
| 70 | button_frame,
|
---|
| 71 | text="Quit",
|
---|
| 72 | width=self.default_button_width,
|
---|
| 73 | command=self.quit).pack(side="top", fill="both", expand=True)
|
---|
| 74 |
|
---|
[09b290d] | 75 | def current_bytes(self):
|
---|
| 76 | return b"".join([self.bytes[each: each+self.canvas_size[0]] for each in range(self.offset.get(),
|
---|
| 77 | len(self.bytes),
|
---|
| 78 | self.cycle.get())])
|
---|
| 79 | def update_canvas(self):
|
---|
| 80 | self.image = ImageTk.PhotoImage(Image.frombytes("L", self.canvas_size, self.current_bytes()))
|
---|
| 81 | self.canvas.itemconfig(self.image_id, image=self.image)
|
---|
| 82 |
|
---|
[bcacddc] | 83 | def load_file(self):
|
---|
| 84 | pass
|
---|
| 85 |
|
---|
| 86 | def quit(self):
|
---|
| 87 | self.parent.destroy()
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | if __name__ == '__main__':
|
---|
| 91 | root = tk.Tk()
|
---|
| 92 | root.geometry("1920x1200")
|
---|
| 93 |
|
---|
| 94 | app = MainApplication(root)
|
---|
| 95 |
|
---|
| 96 | root.mainloop()
|
---|