1 /*
2 * Sweep, a sound wave editor.
3 *
4 * Copyright (C) 2000 Conrad Parker
5 *
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.
10 *
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.
15 *
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
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <math.h>
28
29 #include <sweep/sweep_types.h>
30
31 /*
32 * Print a number of bytes to 3 significant figures
33 * using standard abbreviations (GB, MB, kB, byte[s])
34 */
35 int
snprint_bytes(gchar * s,gint n,glong nr_bytes)36 snprint_bytes (gchar * s, gint n, glong nr_bytes)
37 {
38 if (nr_bytes > (1L<<30)) {
39 return snprintf (s, n, "%0.3f GB",
40 (gfloat)nr_bytes / (1024.0 * 1024.0 * 1024.0));
41 } else if (nr_bytes > (1L<<20)) {
42 return snprintf (s, n, "%0.3f MB",
43 (gfloat)nr_bytes / (1024.0 * 1024.0));
44 } else if (nr_bytes > (1L<<10)) {
45 return snprintf (s, n, "%0.3f kB",
46 (gfloat)nr_bytes / (1024.0));
47 } else if (nr_bytes == 1) {
48 return snprintf (s, n, "1 byte");
49 } else {
50 return snprintf (s, n, "%ld bytes", nr_bytes);
51 }
52 }
53
54 /*
55 * Print a time in the format HH:MM:SS.sss
56 */
57 int
snprint_time(gchar * s,gint n,sw_time_t time)58 snprint_time (gchar * s, gint n, sw_time_t time)
59 {
60 int hrs, min;
61 sw_time_t sec;
62 char * sign;
63
64 sign = (time < 0.0) ? "-" : "";
65
66 if (time < 0.0) time = -time;
67
68 hrs = (int) (time/3600.0);
69 min = (int) ((time - ((sw_time_t)hrs * 3600.0)) / 60.0);
70 sec = time - ((sw_time_t)hrs * 3600.0)- ((sw_time_t)min * 60.0);
71
72 /* XXX: %02.3f workaround */
73 if (sec < 10.0) {
74 return snprintf (s, n, "%s%02d:%02d:0%2.3f", sign, hrs, min, sec);
75 } else {
76 return snprintf (s, n, "%s%02d:%02d:%02.3f", sign, hrs, min, sec);
77 }
78 }
79
80 /*
81 * Print a time in SMPTE format
82 */
83 int
snprint_time_smpte(gchar * s,gint n,sw_time_t time,gint F)84 snprint_time_smpte (gchar * s, gint n, sw_time_t time, gint F)
85 {
86 double hrs, min, sec, N;
87 double dtime = (double)time;
88
89 /* round dtime to resolution */
90 dtime = rint(dtime * (double)F) / (double)F;
91 hrs = floor(dtime/3600.0);
92 min = floor((dtime - (hrs * 3600.0)) / 60.0);
93 sec = floor(dtime - (hrs * 3600.0) - (min * 60.0));
94 N = rint((dtime - (hrs * 3600.0) - (min * 60.0) - sec) * (double)F);
95
96 if (hrs > 0.0)
97 return snprintf (s, n, "PT%.0fH%.0fM%.0fS%.0fN%dF", hrs, min, sec, N, F);
98 else if (min > 0.0)
99 return snprintf (s, n, "PT%.0fM%.0fS%.0fN%dF", min, sec, N, F);
100 else if (sec > 0.0)
101 return snprintf (s, n, "PT%.0fS%.0fN%dF", sec, N, F);
102 else if (N > 0.0)
103 return snprintf (s, n, "PT%.0fN%dF", N, F);
104 else
105 return snprintf (s, n, "P0S");
106 }
107
108 double
strtime_to_seconds(char * str)109 strtime_to_seconds (char * str)
110 {
111 int h=0,m=0, n;
112 float s;
113 double result;
114
115 n = sscanf (str, "%d:%d:%f", &h, &m, &s);
116 if (n == 3) {
117 goto done;
118 }
119
120 n = sscanf (str, "%d:%f", &m, &s);
121 if (n == 2) {
122 h = 0;
123 goto done;
124 }
125
126 n = sscanf (str, "%f", &s);
127 if (n == 1) {
128 h = 0; m = 0;
129 goto done;
130 }
131
132 return -1.0;
133
134 done:
135
136 result = ((h * 3600.0) + (m * 60.0) + s);
137
138 return result;
139 }
140