spek

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

commit f01ec84becf5f9d189e6031ac043fa4f54a5c5e8
parent 80b2a25f0253aecf64b549f1db4685fa90d60997
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Sun,  9 May 2010 13:09:52 +1000

Save spectrogram as a PNG image

Diffstat:
Msrc/spek-spectrogram.vala | 21+++++++++++++++------
Msrc/spek-window.vala | 18++++++++++++++++++
2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/spek-spectrogram.vala b/src/spek-spectrogram.vala @@ -30,7 +30,7 @@ namespace Spek { private ImageSurface image; private ImageSurface palette; - private const int PADDING = 60; + private const int PADDING = 40; private const int GAP = 10; private const int RULER = 10; @@ -51,6 +51,12 @@ namespace Spek { start (); } + public void save (string file_name) { + var surface = new ImageSurface (Format.RGB24, allocation.width, allocation.height); + draw (new Context (surface)); + surface.write_to_png (file_name); + } + private void start () { // The number of samples is the number of pixels available for the image. // The number of bands is fixed, FFT results are very different for @@ -84,15 +90,20 @@ namespace Spek { } private override bool expose_event (EventExpose event) { - double w = allocation.width; - double h = allocation.height; - var cr = cairo_create (this.window); // Clip to the exposed area. cr.rectangle (event.area.x, event.area.y, event.area.width, event.area.height); cr.clip (); + draw (cr); + return true; + } + + private void draw (Context cr) { + double w = allocation.width; + double h = allocation.height; + // Clean the background. cr.set_source_rgb (0, 0, 0); cr.paint (); @@ -119,8 +130,6 @@ namespace Spek { cr.set_source_surface (palette, 0, 0); cr.paint (); cr.identity_matrix (); - - return true; } private void put_pixel (ImageSurface surface, int x, int y, uint32 color) { diff --git a/src/spek-window.vala b/src/spek-window.vala @@ -43,6 +43,13 @@ namespace Spek { open.clicked.connect (on_open_clicked); toolbar.insert (open, -1); + var save = new ToolButton.from_stock (STOCK_SAVE); + save.is_important = true; + save.add_accelerator ( + "clicked", group, 'S', ModifierType.CONTROL_MASK, AccelFlags.VISIBLE); + save.clicked.connect (on_save_clicked); + toolbar.insert (save, -1); + toolbar.insert (new SeparatorToolItem (), -1); var quit = new ToolButton.from_stock (STOCK_QUIT); @@ -84,6 +91,17 @@ namespace Spek { chooser.destroy (); } + private void on_save_clicked () { + var chooser = new FileChooserDialog ( + _("Save File"), this, FileChooserAction.SAVE, + STOCK_CANCEL, ResponseType.CANCEL, + STOCK_SAVE, ResponseType.ACCEPT, null); + if (chooser.run () == ResponseType.ACCEPT) { + spectrogram.save (chooser.get_filename ()); + } + chooser.destroy (); + } + private void on_about_clicked () { string[] authors = { "Alexander Kojevnikov <alexander@kojevnikov.com>"