Clean up Spectrograph test file.
[dictix.git] / src / dix-utils.vala
blob7594f1b24740ac4caeb9026fc6d87c0e7850bd17
1 /**
2 * Dictix / DixUtils - dix-utils.vala
4 * Copyright (C) Martin Blanchard 2011 <tinram@gmx.fr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 using GLib;
23 using Gtk;
25 namespace Utils {
26 public const string[] size_units = {
27 N_("bytes"),
28 N_("Kio"),
29 N_("Mio"),
30 N_("Gio")
33 public string codec_to_string (string codec) {
34 if (codec == "FLAC") {
35 return _("FLAC (lossless)");
36 } else if (codec == "Vorbis") {
37 return _("Vorbis (lossy)");
40 return _("n/a");
43 public string basename_from_uri (string uri) {
44 string path = null;
46 if (uri != null) {
48 try {
49 path = Filename.from_uri (uri);
50 } catch (Error error) {
51 warning (error.message);
53 return _("n/a");
56 return Path.get_basename (path);
59 return _("n/a");
62 public string size_to_string (uint64 bytes) {
63 float size = 0.0f;
64 short i = 0;
66 if (bytes > 0) {
67 size = (float) bytes;
69 while (size > 1000.0f) {
70 size /= 1024.0f;
72 i++;
75 size *= 10.0f;
76 size = Math.roundf (size);
77 size /= 10.0f;
79 return size.to_string ("%.01lf") + " " + size_units[i];
82 return size.to_string ("%.01lf") + " " + size_units[0];
85 public string duration_to_string (int64 duration) {
86 int secs, mins, hours;
88 if (duration > 0) {
89 /* Computation of secs / mins / hours : */
90 secs = (int) (duration % 60);
91 duration -= secs;
93 mins = (int) ((duration % (60 * 60)) / 60);
94 duration -= mins * 60;
96 if (duration == 0) {
97 return mins.to_string () + ":" + secs.to_string ("%02d");
100 hours = (int) (duration / (60 * 60));
102 return hours.to_string () + ":" + mins.to_string ("%02d") + ":" + secs.to_string ("%02d");
105 return "0:00";
108 public string channels_to_string (int channels) {
109 if (channels > 0) {
110 return channels.to_string ();
113 return _("n/a");
116 public string sample_rate_to_string (int sample_rate) {
117 float rate = 0.0f;
118 int hundreds;
120 rate = ((float) sample_rate) / 1000.0f;
122 if (rate > 0.0f) {
123 hundreds = (int) (sample_rate % 1000);
125 if ((hundreds % 100) == 0) {
126 return rate.to_string ("%.01lf") + " " + _("kHz");
129 if ((hundreds % 10) == 0) {
130 return rate.to_string ("%.02lf") + " " + _("kHz");
133 return rate.to_string ("%.03lf") + " " + _("kHz");
136 return _("n/a");
139 public string bit_rate_to_string (int bit_rate) {
140 int kbytes = 0;
142 if (bit_rate > 0) {
143 kbytes = (int) (bit_rate / 1000);
145 return kbytes.to_string () + " " + _("kbps");
148 return _("n/a");