spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit a741fb0d22ec730b6f673bff5e0604153c01935a
parent bf139c96a9858c198dc2668edb554e564e20efde
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Mon, 28 Mar 2011 15:45:41 +0800

Right and top rulers

Diffstat:
Msrc/spek-ruler.vala | 45+++++++++++++++++++++++++++++++++------------
Msrc/spek-spectrogram.vala | 6++++--
2 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/src/spek-ruler.vala b/src/spek-ruler.vala @@ -21,7 +21,14 @@ using Pango; namespace Spek { class Ruler : GLib.Object { + public enum Position { + TOP, + RIGHT, + BOTTOM, + LEFT + } + private Position pos; private string sample_label; private int[] factors; private int units; @@ -33,8 +40,10 @@ namespace Spek { public delegate string FormatTick (int unit); public Ruler ( - string sample_label, int[] factors, int units, double spacing, + Position pos, string sample_label, + int[] factors, int units, double spacing, UnitToPixel unit_to_pixel, FormatTick format_tick) { + this.pos = pos; this.sample_label = sample_label; this.factors = factors; this.units = units; @@ -43,12 +52,12 @@ namespace Spek { this.format_tick = format_tick; } - public void draw (Cairo.Context cr, Pango.Layout layout, bool horizontal) { + public void draw (Cairo.Context cr, Pango.Layout layout) { // Mesure the sample label. int w, h; layout.set_text (sample_label, -1); layout.get_pixel_size (out w, out h); - var size = horizontal ? w : h; + var size = pos == Position.TOP || pos == Position.BOTTOM ? w : h; // Select the factor to use, we want some space between the labels. int factor = 0; @@ -76,20 +85,32 @@ namespace Spek { double TICK_LEN = 4; foreach (var tick in ticks) { var label = format_tick (tick); - var pos = unit_to_pixel (horizontal ? tick : units - tick); + var p = unit_to_pixel ( + pos == Position.TOP || pos == Position.BOTTOM + ? tick : units - tick); layout.set_text (label, -1); layout.get_pixel_size (out w, out h); - if (horizontal) { - cr.move_to (pos - w / 2, GAP + h); - } else { - cr.move_to (-w - GAP, pos + h / 2); + if (pos == Position.TOP) { + cr.move_to (p - w / 2, -GAP - h); + } else if (pos == Position.RIGHT){ + cr.move_to (w + GAP, p + h / 2); + } else if (pos == Position.BOTTOM) { + cr.move_to (p - w / 2, GAP + h); + } else if (pos == Position.LEFT){ + cr.move_to (-w - GAP, p + h / 2); } cairo_show_layout_line (cr, layout.get_line (0)); - if (horizontal) { - cr.move_to (pos, 0); + if (pos == Position.TOP) { + cr.move_to (p, 0); + cr.rel_line_to (0, -TICK_LEN); + } else if (pos == Position.RIGHT) { + cr.move_to (0, p); + cr.rel_line_to (TICK_LEN, 0); + } else if (pos == Position.BOTTOM) { + cr.move_to (p, 0); cr.rel_line_to (0, TICK_LEN); - } else { - cr.move_to (0, pos); + } else if (pos == Position.LEFT) { + cr.move_to (0, p); cr.rel_line_to (-TICK_LEN, 0); } cr.stroke (); diff --git a/src/spek-spectrogram.vala b/src/spek-spectrogram.vala @@ -180,6 +180,7 @@ namespace Spek { // Time ruler. var duration_seconds = (int) pipeline.duration; var time_ruler = new Ruler ( + Ruler.Position.BOTTOM, "00:00", {1, 2, 5, 10, 20, 30, 1*60, 2*60, 5*60, 10*60, 20*60, 30*60}, duration_seconds, @@ -187,12 +188,13 @@ namespace Spek { unit => (w - LPAD - RPAD) * unit / duration_seconds, unit => "%d:%02d".printf (unit / 60, unit % 60)); cr.translate (LPAD, h - BPAD); - time_ruler.draw (cr, layout, true); + time_ruler.draw (cr, layout); cr.identity_matrix (); // Frequency ruler. var freq = pipeline.sample_rate / 2; var rate_ruler = new Ruler ( + Ruler.Position.LEFT, "00 kHz", {1000, 2000, 5000, 10000, 20000}, freq, @@ -200,7 +202,7 @@ namespace Spek { unit => (h - TPAD - BPAD) * unit / freq, unit => _("%d kHz").printf (unit / 1000)); cr.translate (LPAD, TPAD); - rate_ruler.draw (cr, layout, false); + rate_ruler.draw (cr, layout); cr.identity_matrix (); // File properties.