| | 627 | class frequency_response(gtk.DrawingArea): |
| | 628 | def __init__(self): |
| | 629 | gtk.DrawingArea.__init__(self) |
| | 630 | |
| | 631 | self.connect("expose-event", self.on_expose) |
| | 632 | self.connect("size-request", self.on_size_request) |
| | 633 | self.connect("size_allocate", self.on_size_allocate) |
| | 634 | |
| | 635 | self.color_bg = gtk.gdk.Color(0,0,0) |
| | 636 | self.color_value = gtk.gdk.Color(int(65535 * 0.8), int(65535 * 0.7), 0) |
| | 637 | self.color_mark = gtk.gdk.Color(int(65535 * 0.3), int(65535 * 0.3), int(65535 * 0.3)) |
| | 638 | self.width = 0 |
| | 639 | self.height = 0 |
| | 640 | self.margin = 10 |
| | 641 | self.db_range = 30 |
| | 642 | |
| | 643 | self.points = [] |
| | 644 | |
| | 645 | def on_expose(self, widget, event): |
| | 646 | cairo_ctx = widget.window.cairo_create() |
| | 647 | |
| | 648 | # set a clip region for the expose event |
| | 649 | cairo_ctx.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) |
| | 650 | cairo_ctx.clip() |
| | 651 | |
| | 652 | self.draw(cairo_ctx) |
| | 653 | |
| | 654 | return False |
| | 655 | |
| | 656 | def on_size_allocate(self, widget, allocation): |
| | 657 | #print allocation.x, allocation.y, allocation.width, allocation.height |
| | 658 | self.width = float(allocation.width) |
| | 659 | self.height = float(allocation.height) |
| | 660 | self.font_size = 10 |
| | 661 | |
| | 662 | def on_size_request(self, widget, requisition): |
| | 663 | #print "size-request, %u x %u" % (requisition.width, requisition.height) |
| | 664 | requisition.width = 150 |
| | 665 | requisition.height = 150 |
| | 666 | return |
| | 667 | |
| | 668 | def invalidate_all(self): |
| | 669 | self.queue_draw_area(0, 0, int(self.width), int(self.height)) |
| | 670 | |
| | 671 | def get_x(self, hz): |
| | 672 | width = self.width - 3.5 * self.margin |
| | 673 | #x = self.margin + width * (hz - 20) / (20000 - 20) |
| | 674 | x = 2.5 * self.margin + width * log(hz / 20.0, 1000.0) |
| | 675 | #print x |
| | 676 | return x |
| | 677 | |
| | 678 | def get_y(self, db): |
| | 679 | height = self.height - 2.5 * self.margin |
| | 680 | y = self.margin + height * (self.db_range - db) / (self.db_range * 2) |
| | 681 | #print y |
| | 682 | return y |
| | 683 | |
| | 684 | def draw_db_grid(self, cairo_ctx, db): |
| | 685 | x = self.get_x(20) |
| | 686 | y = self.get_y(db) |
| | 687 | cairo_ctx.move_to(x, y) |
| | 688 | cairo_ctx.line_to(self.get_x(20000), y) |
| | 689 | |
| | 690 | if db % 10 == 0: |
| | 691 | x -= 23 |
| | 692 | y += 3 |
| | 693 | cairo_ctx.move_to(x, y) |
| | 694 | label = "%+d" % db |
| | 695 | if db == 0: |
| | 696 | label = " " + label |
| | 697 | cairo_ctx.show_text(label) |
| | 698 | |
| | 699 | cairo_ctx.stroke() |
| | 700 | |
| | 701 | def draw(self, cairo_ctx): |
| | 702 | cairo_ctx.set_source_color(self.color_bg) |
| | 703 | cairo_ctx.rectangle(0, 0, self.width, self.height) |
| | 704 | cairo_ctx.fill() |
| | 705 | |
| | 706 | cairo_ctx.set_source_color(self.color_mark) |
| | 707 | cairo_ctx.set_line_width(1); |
| | 708 | |
| | 709 | #for hz in 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000: |
| | 710 | for hz in range(20, 101, 10) + range(100, 1001, 100) + range(1000, 10001, 1000) + range(10000, 20001, 10000): |
| | 711 | if hz >= 10000: |
| | 712 | label = "%dk" % int(hz / 1000) |
| | 713 | elif hz >= 1000: |
| | 714 | label = "%dk" % int(hz / 1000) |
| | 715 | else: |
| | 716 | label = "%d" % int(hz) |
| | 717 | first_digit = int(label[0]) |
| | 718 | if first_digit > 5 or (first_digit > 3 and (len(label) == 3)): |
| | 719 | label = None |
| | 720 | |
| | 721 | x = self.get_x(hz) |
| | 722 | cairo_ctx.move_to(x, self.get_y(self.db_range)) |
| | 723 | y = self.get_y(-self.db_range) |
| | 724 | cairo_ctx.line_to(x, y) |
| | 725 | if label: |
| | 726 | y += 10 |
| | 727 | if hz == 20000: |
| | 728 | x -= 15 |
| | 729 | else: |
| | 730 | x -= 5 |
| | 731 | cairo_ctx.move_to(x, y) |
| | 732 | cairo_ctx.show_text(label) |
| | 733 | cairo_ctx.stroke() |
| | 734 | |
| | 735 | for db in range(0, self.db_range + 1, 5): |
| | 736 | self.draw_db_grid(cairo_ctx, db) |
| | 737 | |
| | 738 | if db != 0: |
| | 739 | self.draw_db_grid(cairo_ctx, -db) |
| | 740 | |