4 from numpy
import float32
, float64
6 from reinteract
.custom_result
import CustomResult
8 class PlayResult(CustomResult
):
9 def __init__(self
, 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
)
21 def play(self
, *args
):
22 if self
.__data
.dtype
== float32
:
23 command
= "play -t raw -r 44100 -f -4 -L -q -"
25 command
= "play -t raw -r 44100 -f -8 -L -q -"
27 f
= os
.popen(command
, 'w')
31 def __save(self
, filename
):
32 escaped
= filename
.replace("'", r
"'\''")
35 if self
.__data
.dtype
== float32
:
36 command
= "sox -t raw -r 44100 -f -4 -L -q - '%s'" % escaped
38 command
= "sox -t raw -r 44100 -f -8 -L -q - '%s'" % escaped
40 f
= os
.popen(command
, 'w')
44 def on_button_press(self
, button
, event
):
46 toplevel
= button
.get_toplevel()
49 menu_item
= gtk
.ImageMenuItem(stock_id
=gtk
.STOCK_SAVE_AS
)
53 def on_selection_done(menu
):
55 menu
.connect('selection-done', on_selection_done
)
57 def on_activate(menu
):
58 chooser
= gtk
.FileChooserDialog("Save As...", toplevel
, gtk
.FILE_CHOOSER_ACTION_SAVE
,
59 (gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
,
60 gtk
.STOCK_SAVE
, gtk
.RESPONSE_OK
))
61 chooser
.set_default_response(gtk
.RESPONSE_OK
)
62 response
= chooser
.run()
64 if response
== gtk
.RESPONSE_OK
:
65 filename
= chooser
.get_filename()
72 menu_item
.connect('activate', on_activate
)
74 menu
.popup(None, None, None, event
.button
, event
.time
)
79 def on_realize(self
, button
):
80 # Hack to get the right cursor over the button, since the button
81 # doesn't set a cursor itself. button.window is the text view's
82 # window, we have to search to find button.event_window, since
84 for c
in button
.window
.get_children():
85 if c
.get_user_data() == button
:
86 cursor
= gtk
.gdk
.Cursor(gtk
.gdk
.LEFT_PTR
)
90 if data
.dtype
!= float32
and data
.dtype
!= float64
:
91 raise TypeError("Data must be float32 or float64, not %s", data
.dtype
)
93 return PlayResult(data
)