Switch debug messages from ShellBuffer to use 'logging'
[reinteract/rox.git] / lib / replay.py
blobb7f3525a0bb8c21ae5af5062f0607ab053d9f4e1
1 import gtk
2 import os
4 from numpy import float32, float64
6 import reinteract.custom_result as custom_result
8 class PlayResult(custom_result.CustomResult):
9 def __init__(self, data):
10 self.__data = data
12 def create_widget(self):
13 widget = gtk.Button("Play")
14 widget.connect('clicked', self.play)
16 widget.connect('button_press_event', self.on_button_press)
17 widget.connect('realize', self.on_realize)
19 return widget
21 def play(self, *args):
22 if self.__data.dtype == float32:
23 command = "play -t raw -r 44100 -f -4 -L -q -"
24 else:
25 command = "play -t raw -r 44100 -f -8 -L -q -"
27 f = os.popen(command, 'w')
28 self.__data.tofile(f)
29 f.close()
31 def __save(self, filename):
32 escaped = filename.replace("'", r"'\''")
34 if self.__data.dtype == float32:
35 command = "sox -t raw -r 44100 -f -4 -L -q - '%s'" % escaped
36 else:
37 command = "sox -t raw -r 44100 -f -8 -L -q - '%s'" % escaped
39 f = os.popen(command, 'w')
40 self.__data.tofile(f)
41 f.close()
43 def on_button_press(self, button, event):
44 if event.button == 3:
45 custom_result.show_menu(button, event, save_callback=self.__save)
46 return True
47 return False
49 def on_realize(self, button):
50 # Hack to get the right cursor over the button, since the button
51 # doesn't set a cursor itself. button.window is the text view's
52 # window, we have to search to find button.event_window, since
53 # its not bound
54 for c in button.window.get_children():
55 if c.get_user_data() == button:
56 cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
57 c.set_cursor(cursor)
59 def play(data):
60 if data.dtype != float32 and data.dtype != float64:
61 raise TypeError("Data must be float32 or float64, not %s", data.dtype)
63 return PlayResult(data)