File: guitar-tuner.py

package info (click to toggle)
gnome-devel-docs 3.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,808 kB
  • sloc: xml: 101,979; sh: 625; makefile: 380; ansic: 340; cpp: 131; python: 80
file content (64 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python

from gi.repository import Gtk, Gst, GObject
import os, sys

#Comment the first line and uncomment the second before installing
#or making the tarball (alternatively, use project variables)
UI_FILE = "src/guitar_tuner.ui"
#UI_FILE = "/usr/local/share/guitar_tuner_py/ui/guitar_tuner.ui"

class GUI:
	LENGTH = 500
	# Frequencies of the strings
	frequencies = {
		'E': 369.23,
		'A': 440,
		'D': 587.33,
		'G': 783.99,
		'B': 987.77,
		'e': 1318.5
	}

	def __init__(self):
		self.builder = Gtk.Builder()
		self.builder.add_from_file(UI_FILE)
		self.builder.connect_signals(self)

		window = self.builder.get_object('window')
		window.show_all()

	def on_button_clicked (self, button):
		label = button.get_child()
		text = label.get_label()
		self.play_sound (self.frequencies[text])

	def destroy(window, self):
		Gtk.main_quit()

	def pipeline_stop(self, pipeline):
		pipeline.set_state(Gst.State.PAUSED)
		return False

	def play_sound(self, frequency):
		pipeline = Gst.Pipeline(name='note')
		source = Gst.ElementFactory.make('audiotestsrc', 'src')
		sink = Gst.ElementFactory.make('autoaudiosink', 'output')

		source.set_property('freq', frequency)
		pipeline.add(source)
		pipeline.add(sink)
		source.link(sink)

		pipeline.set_state(Gst.State.PLAYING)

		GObject.timeout_add(self.LENGTH, self.pipeline_stop, pipeline)

def main():
	Gst.init_check(sys.argv)
	app = GUI()
	Gtk.main()

if __name__ == "__main__":
    sys.exit(main())